为什么我的程序总是跳过 if 语句?

Why does my program keep skipping out an if statement?

我的程序旨在使用带编码器的直流电机为自行车换档。到目前为止,我有一些 if 语句,基本上是说如果按下按钮,然后 运行 电机直到达到位置,然后关闭电机。例如,第一个语句是从齿轮 1 移动到齿轮 2。然后我有另一个语句说如果再次按下按钮,那么电机将移动到下一个位置。但是当我 运行 我的程序时,它会跳过第一条语句,因为它不会停在第一个 if 语句的位置,而是保持 运行ning 直到它到达下一个 if 语句的位置。我不明白为什么会这样。这是代码:

#define ENCA 2 // PINK
#define ENCB 3 // YELLOW

// this constant won't change:
//const int buttonPin2 = 3;
const int  buttonPin = 4;    // the pin that the pushbutton is attached to
//const int ledPin = 13;       // the pin that the LED is attached to

//Motor A

int PWM = 5;
int in2 = 6;
int in1 = 7;

// Variables will change:
int pos = 0;
int buttonPushCounter = 1;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
//int buttonState2 = 0;
int lastButtonState = 0;     // previous state of the button
//int lastButtonState2 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ENCA, INPUT);
  pinMode(ENCB, INPUT);
  pinMode(PWM, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(ENCA), readEncoder, RISING);
  Serial.println("target pos");
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  //  pinMode(buttonPin2, INPUT);


  // initialize motor pins
  pinMode(PWM, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
}


void loop() {

  if (buttonPushCounter == 1) {
  GearOneTwo();
  }
  if (buttonPushCounter == 2) {
  GearTwoThree();
  }
  PrintData();

}
void GearOneTwo() {

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  //  buttonState2 = digitalRead(buttonPin2);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    //if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.print("Current Gear: ");
      Serial.println(buttonPushCounter);
      //Turn on motor A
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      analogWrite(PWM, 255);
     }
  }
  if (pos >= 807) {
  MotorOff();
  }
  //    delay(50);
  lastButtonState = buttonState;
}

void GearTwoThree() {

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  //  buttonState2 = digitalRead(buttonPin2);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    //if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.print("Current Gear: ");
      Serial.println(buttonPushCounter);
      //Turn on motor A
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      analogWrite(PWM, 255);
    }
  }
  if (pos >= 2000) {
  MotorOff();
  }
  //    delay(50);
  lastButtonState = buttonState;
}

void PrintData() {
    int target = 888;
    Serial.print(target);
    Serial.print(" ");
    Serial.print(pos);
    Serial.println();
}

void MotorOff() {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
}

void readEncoder() {
    int b = digitalRead(ENCB);
    if (b > 0) {
        pos++;
        }
    else {
        pos--;
    }
}

试试这个:如果电机到达所需位置,则 buttonPushCounter++

#define ENCA 2 // PINK
#define ENCB 3 // YELLOW

// this constant won't change:
//const int buttonPin2 = 3;
const int  buttonPin = 4;    // the pin that the pushbutton is attached to
//const int ledPin = 13;       // the pin that the LED is attached to

//Motor A

int PWM = 5;
int in2 = 6;
int in1 = 7;

// Variables will change:
int pos = 0;
int buttonPushCounter = 1;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
//int buttonState2 = 0;
int lastButtonState = 0;     // previous state of the button
//int lastButtonState2 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ENCA, INPUT);
  pinMode(ENCB, INPUT);
  pinMode(PWM, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(ENCA), readEncoder, RISING);
  Serial.println("target pos");
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  //  pinMode(buttonPin2, INPUT);


  // initialize motor pins
  pinMode(PWM, OUTPUT);
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
}


void loop() {

  if (buttonPushCounter == 1) {
  GearOneTwo();
  }
  if (buttonPushCounter == 2) {
  GearTwoThree();
  }
  PrintData();

}
void GearOneTwo() {

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  //  buttonState2 = digitalRead(buttonPin2);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    //if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      Serial.print("Current Gear: ");
      Serial.println(buttonPushCounter);
      //Turn on motor A
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      analogWrite(PWM, 255);
     }
  }
  if (pos >= 807) {
  MotorOff();
  buttonPushCounter++;
  }
  //    delay(50);
  lastButtonState = buttonState;
}

void GearTwoThree() {

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  //  buttonState2 = digitalRead(buttonPin2);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    //if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      Serial.print("Current Gear: ");
      Serial.println(buttonPushCounter);
      //Turn on motor A
      digitalWrite(in1, LOW);
      digitalWrite(in2, HIGH);
      analogWrite(PWM, 255);
    }
  }
  if (pos >= 2000) {
  MotorOff();
  buttonPushCounter++;
  }
  //    delay(50);
  lastButtonState = buttonState;
}

void PrintData() {
    int target = 888;
    Serial.print(target);
    Serial.print(" ");
    Serial.print(pos);
    Serial.println();
}

void MotorOff() {
    digitalWrite(in1, LOW);
    digitalWrite(in2, LOW);
}

void readEncoder() {
    int b = digitalRead(ENCB);
    if (b > 0) {
        pos++;
        }
    else {
        pos--;
    }
}

如果这不起作用,请检查您的 pos