0

I'm doing a project which is automatic box carrier. It will hold a box from a location and move it next to it. Kind of cnc work. Locations are fixed it just has to run the orders. I want to send an array of integers and manage them in the switch under main loop. for example

  orders[0]=2;
  orders[1]=2000;

  orders[2]=3;
  orders[3]=60;

  orders[4]=1;
  orders[5]=7000;

  orders[6]=2;
  orders[7]=2000;

  orders[8]=3;
  orders[9]=90;

  orders[10]=1;
  orders[11]=2000;

will be my orders array and here is the code i want to run. When I upload code to arduino steppers and servo doing fine until orders[8]. When came to that order; first servo goes to 30 degrees then goes to 0 and click sound starts. After that steppers are not doing remaining steps.

#include <Servo.h>
Servo myservo;

int distance=0;
int code =0;
int orders[10]={};
int thisStep=0;
int thisVal=1;
int pos = 0;
int currpos=90;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  myservo.attach(9);

  orders[0]=2;
  orders[1]=7000;

  orders[2]=3;
  orders[3]=60;

  orders[4]=1;
  orders[5]=2000;

  orders[6]=2;
  orders[7]=2000;

  orders[8]=3;
  orders[9]=30;

  orders[10]=1;
  orders[11]=2000;

 /* orders[12]=2;
  orders[13]=5000;*/
}

void loop() {

  code = orders[thisStep];
  switch (code){
    case 1:

    go(orders[thisVal]);
    thisStep = thisStep + 2;
  thisVal = thisVal + 2;

    break;
    case 2:
     Serial.println(thisVal);
    goReverse(orders[thisVal]);
    thisStep = thisStep + 2;
    thisVal = thisVal + 2;

    break;

    case 3:
    goServo(orders[thisVal]);
    thisStep = thisStep + 2;
    thisVal = thisVal + 2;
    break;
  }

}
void go(int dist){
  digitalWrite(2,LOW);
  digitalWrite(4,LOW);
  do{
  digitalWrite(3,HIGH);
  digitalWrite(5,HIGH);
  delayMicroseconds(10);
  digitalWrite(3,LOW);
  digitalWrite(5,LOW);
  delayMicroseconds(10);
  distance = distance+1;
  Serial.println(distance);
  }while (dist>distance);
 distance=0;
 delay(500);
}
void goReverse(int dist){
  digitalWrite(2,HIGH);
  digitalWrite(4,HIGH);
  do{
  digitalWrite(3,HIGH);
  digitalWrite(5,HIGH);
  delayMicroseconds(300);
  digitalWrite(3,LOW);
  digitalWrite(5,LOW);
  delayMicroseconds(300);
  distance = distance+1;

  }while (dist>distance);
  distance=0;
  delay(500);
}

void goServo (int degree){
  myservo.attach(9);
  myservo.write(degree);

  delay(300);
   myservo.detach();
  /*if (currpos > degree)
  {
    for (pos = currpos; pos >= degree; pos -= 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
       myservo.write(pos);              // tell servo to go to position in variable 'pos'
       delay(15);  
       // waits 15ms for the servo to reach the position
      }
      delay(500); 
  }
  else if (currpos < degree)
  {
    for (pos = currpos; pos <= degree; pos += 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
       myservo.write(pos);              // tell servo to go to position in variable 'pos'
       delay(15);  
       // waits 15ms for the servo to reach the position
      }

  }
  currpos=degree;

*/



}

Thanks.

  • Are you sure it's not due to thisStep being larger that the length of the array? Try adding if(thisStep>10)thisStep=0; to the loop. – Gerben Mar 17 '16 at 19:03
1

Your problem here is that you have an array of 10 bytes, which works fine, only that in your loop you don't stop or check that you are within that array.

If this is supposed to run forever, you could add a line like this at the end of your loop:

if (thisStep > sizeof(orders)-1) thisStep = 0;

(-1 because you have an array of 10, which are from 0-9, sizeof will return 10)

this will make it start from the beginning again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.