Hi everybody,
I don't know why Arduino keeps on skipping a instruction (highlighted in code)... :-/
...Can someone help please?
Thanks so much!
//variables
int volatile pos = 0;
int sensepin = 3;
void setup() {
pinMode(13, OUTPUT);
attachInterrupt(sensepin-2, out, CHANGE); /handles encoder interrupt and update position
pos=0;
delay(500);
}
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(2000);
//drive motor in one direction until encoder says "-360"
drivebothTo(120,0,-360); // <<<<<<< SKIPPED <<<<<<<<<
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(2000);
drivebothTo(0,120,360); //not skipped
delay(1000);
for(int i=0; i<5; i++) {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
digitalWrite(13, LOW);
while(1);
}
void stopandwait(int time) {
//STOP
//motor 1
analogWrite(10, 0);
analogWrite(11, 0);
//motor 2
analogWrite(6, 0);
analogWrite(9, 0);
if(time>0) delay(time);
}
void drivebothTo(int one, int two, int theta) {
//motor 1
analogWrite(10, one);
analogWrite(11, two);
//motor 2
analogWrite(6, one);
analogWrite(9, two);
while(pos<theta) delay(1);
stopandwait(0);
}
void out() { //handles encoder interrupt and update position
int val2 = digitalRead(2);
int val3 = digitalRead(3);
if ((val2==LOW && val3==HIGH) || (val2==HIGH && val3==LOW)) {
pos++;
//digitalWrite(13, HIGH);
}
else {
pos--;
//digitalWrite(13, LOW);
}
}