Go Down

Topic: gearmotor test doesn't work (Read 623 times) previous topic - next topic

SouthernAtHeart

This simple test sketch gives me an else without if error, but it's right there before it.  If I remove the else code it compiles.  Is this a bug?
Code: [Select]
  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 2;    // H-bridge leg 2 (pin 7, 2A)
  const int ledPin = 13;      // LED
  const int button1Pin = 8;  //Forward button
  const int button2Pin = 9;  //Reverse button
 
    // variables will change:
  int buttonState1 = 0;         // variable for reading the pushbutton status
  int buttonState2 = 0;         // variable for reading the pushbutton status 
 
 
  void setup() {

    // set outputs:
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(ledPin, OUTPUT);
    pinMode(button1Pin, INPUT);
    pinMode(button2Pin, INPUT);
  }

  void loop() {
  // read the state of the pushbutton1 value:
  buttonState1 = digitalRead(button1Pin);
  // read the state of the pushbutton1 value:
  buttonState2 = digitalRead(button2Pin);

  if (buttonState1 == HIGH)    // check if a pushbutton is pressed.   
  {
    // turn LED on:   
    digitalWrite(ledPin, HIGH);     
    //turn motor on forward
    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
  }
  else if (buttonState2 == HIGH);     // check if a pushbutton is pressed.
  {
    // turn LED on:   
    digitalWrite(ledPin, HIGH);     
    //turn motor on backward
    digitalWrite(motor1Pin, HIGH);   // set leg 1 of the H-bridge low
    digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
  }
 
  else {
    // turn LED off
    digitalWrite(ledPin, LOW);
    //turn motor off
    digitalWrite(motor1Pin, LOW);
    digitalWrite(motor2Pin, LOW);
  }
 
}

RuggedCircuits

Extra semicolon:

Quote
else if (buttonState2 == HIGH);     // check if a pushbutton is pressed


--
The Arduino Drum Machine: 14-track MIDI drum machine sequencer / groove-box

Graynomad

Try removing the ; on this line

Code: [Select]
else if (buttonState2 == HIGH);     // check if a pushbutton is pressed.
 {


______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

SouthernAtHeart

Great!  I was beginning to think it was a bug!

MarkT

There ain't no pedant like a compiler!
[ I won't respond to messages, use the forum please ]

retrolefty

Yea, never argue with your mother-in-law or a compiler, there is no upside, you can not win.  ;)


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy