Go Down

Topic: Arduino skipping part of "loop()" (Read 546 times) previous topic - next topic

arduino.poet

Jul 24, 2008, 03:25 pm Last Edit: Jul 24, 2008, 06:49 pm by arduino.poet Reason: 1
Hi everybody,

I don't know why Arduino keeps on skipping a instruction (highlighted in code)...   :-/
...Can someone help please?

Thanks so much!


Code: [Select]

//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);
 }
}

Lee

Hi

I think that a int can only use positive numbers, i think you must use a long int?

Check the reference page for more info.

Lee

arduino.poet

Quote
Hi
I think that a int can only use positive numbers, i think you must use a long int?


Thanks for your reply, Lee; I checked on the reference and int do actually store negative numbers.

http://www.arduino.cc/en/Reference/Int

mem

#3
Jul 24, 2008, 07:15 pm Last Edit: Jul 24, 2008, 07:17 pm by mem Reason: 1
Woops, posted a reply without thinking....

edit: Why not put some serial print statements to see if the function is actually being skipped

bens

I can pretty much guarantee you that your code is not being skipped.  Instead you need to ask yourself why it might seem like it's getting skipped.  The only reason would be that the delay in your function isn't doing what you think it is:

while(pos<theta) delay(1);

Note that you initialize pos to zero, and then call a function that only delays if pos is less than -360 (which is immediately false).  This will cause your function to immediately exit and proceed to one that drives the motors in the other direction until pos is greater than 360 (in this direction the while loop is not true immediately and you actually get a delay).  When you switch directions you need to switch the less-than operator to a greater-than operator.

- Ben

arduino.poet

Quote

while(pos<theta) delay(1);

Note that you initialize pos to zero, and then call a function that only delays if pos is less than -360 (which is immediately false).  This will cause your function to immediately exit and proceed to one that drives the motors in the other direction until pos is greater than 360 (in this direction the while loop is not true immediately and you actually get a delay).  When you switch directions you need to switch the less-than operator to a greater-than operator.


I'm so sorry it was such a stupid mistake!
Thanks so much for your help!

I fixed it and it now works.

Go Up