Pages: [1]   Go Down
Author Topic: Arduino skipping part of "loop()"  (Read 513 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 Hi everybody,

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

Thanks so much!


Code:
//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);
  }
}
« Last Edit: July 24, 2008, 11:49:46 am by arduino.poet » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 31
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6255
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Woops, posted a reply without thinking....

edit: Why not put some serial print statements to see if the function is actually being skipped
« Last Edit: July 24, 2008, 12:17:45 pm by mem » Logged

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


0
Offline Offline
Newbie
*
Karma: 1
Posts: 27
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: