Stepperlarni servo bilan ishlating

Avtomatik quti tashuvchi bo'lgan loyihani qilaman. U joydan bir quti tutib, uning yoniga ko'chiradi. Cnc ish turi. Joylar aniqlangan, faqat buyurtmalarni bajarish kerak. Men asosiy raqamlar qatorida bir qator integerlarni yuborish va ularni boshqarishni istayman. misol uchun

  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;

mening buyurtmalarim qatori bo'ladi va bu erda ishlashni istagan kod. Kodni arduino steppersga va servolarga buyurtmalarga qadar jarima solganimda [8]. Bu tartibga kelganda; birinchi servo 30 gradusga o'tadi va keyin 0 ga o'tib, ovozni boshlashni boshlaydi. Keyin qadamlar qolgan qadamlarni qilmaydi.

#include 
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;

*/



}

Rahmat.

0
thisStep ning kattaligiga qarab emasligiga ishonchingiz komilmi? if (thisStep> 10) thisStep = 0; ni loopga qo'shib ko'ring.
qo'shib qo'ydi muallif Al., manba

1 javoblar

Sizning muammolaringiz 10 baytdan iborat bo'lib, u yaxshi ishlaydi, faqat loopingizda siz to'xtamaysiz yoki u qatorda ekanligingizni tekshirishingiz mumkin.

Agar bu abadiy ishlashi kerak bo'lsa, sizning loopingizning oxirida bunday qatorni qo'shishingiz mumkin:

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

(-1, chunki siz 0 dan 9 gacha bo'lgan o'lchovlar qatoriga ega bo'lsangiz, sizeof 10 ga qaytadi)

bu yana boshidan boshlanadi.

1
qo'shib qo'ydi