I am having trouble writing code that adds or subtracks from the value of a variable with the turn of an encoder on my teensy 3.2. I want to be able to assign what variable is to be increased/decreased by calling a function. Here is the code I have so far.
The code in this form works just fine. The problem occurs when I try to call encGrab() from outside the main loop(), the teensy just crashes or doesnt run at all (for instance if i call it from setup() ). This is a problem for me as I want to be able to toggle which variable is being manipulated by the encoder with the press of a button. I am sure there is something wrong with the way i'm setting up my pointers, but im not seeing it. Thanks!
Code:
#include "Encoder.h"
Encoder enc(16,15);
int number1 = 100;
int number2 = 25;
int **ptrActive;
int encCount;
int encCountOld;
int encMin;
int encMax;
void encGrab(int min, int max, int *ptr1){ //assigns what variable is to be manipulated by encoder
ptrActive = &ptr1;
enc.write(*ptr1);
encCount = *ptr1;
encCountOld = encCount;
encMin = min;
encMax = max;
}
void encScan(){ //scan for change in encoder, increase/decrease accordingly
encCount = enc.read();
if (encCount != encCountOld){
if (encCount < encMin){
encCount = encMin;
}
if (encCount > encMax){
encCount = encMax;
}
enc.write(encCount);
encCountOld = encCount;
**ptrActive = encCount;
Serial.println(**ptrActive);
}
}
void setup() {
Serial.begin(9600);
}
void loop() {
encGrab(0, 127, &number1); //placed outside while() for testing. encGrab() doesnt work from setup()
while(1){
encScan();
}
}
The code in this form works just fine. The problem occurs when I try to call encGrab() from outside the main loop(), the teensy just crashes or doesnt run at all (for instance if i call it from setup() ). This is a problem for me as I want to be able to toggle which variable is being manipulated by the encoder with the press of a button. I am sure there is something wrong with the way i'm setting up my pointers, but im not seeing it. Thanks!