Hi,
I am trying to calibrate 2 motors with 2 grove line sensors using interrupts, trying to count the number of white stripes from the wheel.
Demo code:
//Arduino PWM Speed Control:
int E1 = 6;
int M1 = 7;
int E2 = 5;
int M2 = 4;
int leftSensor = 0; // PIN 2 on arduino
volatile int count_left = 0;
void setup()
{
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
attachInterrupt(leftSensor, CountLeft, RISING );
Serial.begin(9600);
}
void loop()
{
digitalWrite(M1,HIGH);
digitalWrite(M2, HIGH);
analogWrite(E1, 80); //PWM Speed Control
analogWrite(E2, 80); //PWM Speed Control
Serial.println(count_left);
}
void CountLeft()
{
count_left++;
}
In this case when the count_left variable is very high (for example the wheel is rotating 2 white strips => count_left = 80…100) , but when i move the wheel manually (deactivating the motors) the count_left is correct. 1 white stripe => count_left=1; 2 white stripes = count_left=2;
My question is why when i am using the motors the count_left is going crazy?