samedi 1 mai 2021

Why does my program keep skipping out an if statement?

My program is designed to change gears of a bike using DC motors with encoders. I have a few if statements so far basically saying if a button is pressed then run the motor until the position is met then turn off the motor. For example this first statement would be moving from gear 1 to gear 2. Then I have another statement saying if the button is pressed again then the motor will move to the next position. But when I run my program it skips the first statement because it doesn't stop at the position in the first if statement and keeps running until it gets to the position of the next if statement. I can't figure out why it does this. This is the code:

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

Aucun commentaire:

Enregistrer un commentaire