I am building a walking robot for one of my classes and using a photo interrupter sensor (GP1A57HRJ00F) to count motor cycles.
I have attached an interrupt to increment a counter every time an object passes through the sensor (ie wire attached to motor shaft).
attachInterrupt(0, Check, RISING);
void Check()
{
counter1++;
}
Anyways, I need the robot to walk a certain number of steps, which corresponds to a certain number of motor cycles. To do this I have a while loop (as seen below) which simply delays, until the counter reaches a certain value.
while(counter1 <=9){ //delay until desired number of revolutions
}
The problem is even when the counter reaches that certain value, it will not exit out of the while loop. However, when I put in a print statement for the counter inside the loop, suddenly everything works as it should.
I have tested the photo interrupter thoroughly to make sure it is working. I have put delays before the while loop to ensure the counter is 10+ when it hits the while loop and verified it bypasses it properly. Therefore, I think this is an oddity in the structure of the while loop
I'm really new to microcontrollers and Arduino, so any suggestions/comments would be really appreciated.
Thanks,
Mitch
Also I can post the remaining code I have if need be.