ติดลูปในบอร์ดโปรเเกรม Arduino ช่วยที

#define IN1 5
#define IN2 6
#define IN3 9
#define IN4 10
#define ENA 3
#define ENB 11
int k;
int bluetooth;

void setup(){
  Serial.begin(9600);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
  
}

void loop(){
  motorA(3,100);
  motorB(3,100);
  bluetooth = Serial.read();
  Serial.println(Serial.read());
  if(bluetooth == 49){
    motorA(1,100);
    motorB(1,100);
    delay(1000);
    motorA(3,100);
    motorB(1,100);
    delay(500);
}

        
}
void motorA(int mode, int percent)
{
  
  //change the percentage range of 0 -> 100 into the PWM
  //range of 0 -> 255 using the map function
  int duty = map(percent, 0, 100, 0, 255);
  
  switch(mode)
  {
    case 0:  //disable/coast
      digitalWrite(ENA, LOW);  //set enable low to disable A
      break;
      
    case 1:  //turn clockwise
      //setting IN1 high connects motor lead 1 to +voltage
      digitalWrite(IN1, HIGH);  
      
      //setting IN2 low connects motor lead 2 to ground
      digitalWrite(IN2, LOW);  
      
      //use pwm to control motor speed through enable pin
      analogWrite(ENA, duty);  
      
      break;
      
    case 2:  //turn counter-clockwise
      //setting IN1 low connects motor lead 1 to ground
      digitalWrite(IN1, LOW);  
      
      //setting IN2 high connects motor lead 2 to +voltage
      digitalWrite(IN2, HIGH);  
      
      //use pwm to control motor speed through enable pin
      analogWrite(ENA, duty);  
      
      break;
      
    case 3:  //brake motor
      //setting IN1 low connects motor lead 1 to ground
      digitalWrite(IN1, LOW);  
      
      //setting IN2 high connects motor lead 2 to ground
      digitalWrite(IN2, LOW);  
      
      //use pwm to control motor braking power
      //through enable pin
      analogWrite(ENA, duty);  
      
      break;
  }
}
//**********************************************************


//******************   Motor B control   *******************
  void motorB(int mode, int percent)
{
  
  //change the percentage range of 0 -> 100 into the PWM
  //range of 0 -> 255 using the map function
  int duty = map(percent, 0, 100, 0, 255);
  
  switch(mode)
  {
    case 0:  //disable/coast
      digitalWrite(ENB, LOW);  //set enable low to disable B
      break;
      
    case 1:  //turn clockwise
      //setting IN3 high connects motor lead 1 to +voltage
      digitalWrite(IN3, HIGH);  
      
      //setting IN4 low connects motor lead 2 to ground
      digitalWrite(IN4, LOW);  
      
      //use pwm to control motor speed through enable pin
      analogWrite(ENB, duty);  
      
      break;
      
    case 2:  //turn counter-clockwise
      //setting IN3 low connects motor lead 1 to ground
      digitalWrite(IN3, LOW);  
      
      //setting IN4 high connects motor lead 2 to +voltage
      digitalWrite(IN4, HIGH);  
      
      //use pwm to control motor speed through enable pin
      analogWrite(ENB, duty);  
      
      break;
      
    case 3:  //brake motor
      //setting IN3 low connects motor lead 1 to ground
      digitalWrite(IN3, LOW);  
      
      //setting IN4 high connects motor lead 2 to ground
      digitalWrite(IN4, LOW);  
      
      //use pwm to control motor braking power
      //through enable pin
      analogWrite(ENB, duty);  
      
      break;
  }
}


ชี้เเจงก่อนนะครับ
motorA(1,100)
motorB(1,100)  
delay(1000)
คือ ตรง void loop ชุดคำสั่งมอเตอร์ขับให้เดินหน้าเป็นเวลา 1วิใช่ไหมครับ

เเล้วทำต่อคือ
motorA(3,100);
motorB(1,100);
delay(500);
คือตรงนี้จะเป็นคำสั่งให้มอเตอร์ขับของผมเลี้ยวเป็นเวลา 0.5 วิ

ที่ผมเขียนเเบบนี้คือต้องการให้เมื่อรับคำสั่งมาจากเเอนดรอยเป็นรหัส 49 เเล้วมอเตอรืขับจะสั่งให้ตรงไปเป้นเวลา 1วิ เเล้วเลี้ยว 0.5 วิ
เเต่พอรันโค้ดจริงเมื่อได้รับคำสั่งจากเเอนดรอยมาเเล้ว มันก็เข้า if ครับ เเต่ว่ามันเข้าไปทำเป็นลุปเเบบนี้ครับ ตรงไป เป็นเวลา ไม่ถึง 1วิ เเล้วก็เลี้ยวไม่ถึง 0.5 วิ เเล้วก็กลับไปตรงใหม่เป็นเวลาไม่ถึง 1 วิ เเล้วก็เลี้ยวอีกทีไม่ถึง 0.5 วิครับ
คือมันวนลูปคำสั่งบังคับของผม2ครั้ง ไม่รู้ว่ามาจากไหน

พยายามเเยกคำสั่งเเล้วทั้งสองอันเเล้ว ด้วย การ if อีกครั้งเเล้วนำตัวเเปรมาเพิ่มก็ยังไม่หลุด ก็ยังจะทำ 2 ลูปสั้น เหมือนเดิม
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่