0
I have a project for the college where I have to perform a measuring wheel. In this case I used a rotatory Ncoder to perform the measurements, but I’m having problems at the time of the code. In case, at the moment I do not know how to program a rotating meeting, I am doing a search to find more information. The code I have is:
volatile unsigned int temp, counter = 0;
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
attachInterrupt(0, ai0, RISING);
attachInterrupt(1, ai1, RISING);
}
void loop()
{
if (counter != temp)
{
Serial.println(counter);
temp = counter;
}
}
void ai0()
{
if (digitalRead(3) == LOW)
counter++;
else
counter--;
}
void ai1()
{
if (digitalRead(2) == LOW)
counter--;
else
counter++;
}
PROBLEM: In the code when I turn the wheel to the counter decrease (-1) and it is in the 0 position it jumps to +65874 in the positive value, it would need that when it was in the 0 position and rotated to decrease (-1) He wouldn’t leave 0, because the wheel won’t count a negative value. In the project I am using an LCD for the positions.
It would not be enough, then, before decreasing the value, to check if it is zero?
– Woss
I just tested it here inside the code but it still keeps jumping to +8345. When it is at 0 and the command of the Encoder and subtract it jumps to +8345.
– Nilton F
You have to put the condition both in the ai0() function, and in the ai1() function. See the example for the ai0() function in my answer.
– user148747
I put it in both, but then when I go to Serial, it doesn’t come out the same turning the Ncoder. void ai0() { if(digitalRead(3)==LOW){ counter++; }Else if (counter != 0){ counter-; } } void ai1() { (digitalRead(2)=LOW){ counter+; }Else if (counter!= 0){ counter-; } }
– Nilton F