I connected the output from an encoder motor to int0. this is used to increment a counter at every pulse. in the loop(), I check this counter and stops the motor if it is above certain value. But this "if statement" works erratically. The while loop should break at 380, but sometimes it breaks at 256.
Serial.print(cnt);
cnt=0;
Serial.print(" ");
Serial.print(cnt);
motor1_up(255);
motor2_up(255);
while(1)
{
if(cnt>380)
{
Serial.print(" break ");
Serial.print(cnt);
break;
}
}
motor1_down(255);
motor2_down(255);
Serial.print(" ");
delay(40);
Serial.println(cnt);
motor1_down(0);
motor2_down(0);
delay(2000);
Here is the serial output
1 0 break 381 389
396 0 break 256 264
271 0 break 381 389
396 0 break 381 389
396 0 break 256 264
270 0 break 381 389
397 0 break 381 390
396 0 break 381 389
397 0 break 381 389
396 0 break 256 264
271 0 break 381 389
while(1)
on it looks awful, separate it onto several lines.motor1_down()
for? It's fine to remove snippets that aren't useful (i.e. polling from a light sensor in this scenario), but it's generally helpful to have all the libraries/methods/etc. Can you please edit your code? Thanks!ISR
? If so, you've go a lockingwhile(1)
in there. If it's not, then when does this get called? Please post the full codeloop()
.