Pages: [1]   Go Down
Author Topic: gearmotor test doesn't work  (Read 473 times)
0 Members and 1 Guest are viewing this topic.
South East USA
Offline Offline
God Member
*****
Karma: 4
Posts: 636
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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);
  }
 
}
Logged

0
Offline Offline
Faraday Member
**
Karma: 13
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try removing the ; on this line

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

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

South East USA
Offline Offline
God Member
*****
Karma: 4
Posts: 636
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Shannon Member
****
Karma: 162
Posts: 10496
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There ain't no pedant like a compiler!
Logged

[ I won't respond to messages, use the forum please ]

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16548
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Pages: [1]   Go Up
Jump to: