Go Down

Topic: help with my program (Read 240 times) previous topic - next topic

rod322

Oct 03, 2015, 11:29 pm Last Edit: Oct 03, 2015, 11:30 pm by rod322
I would like to know if there is anything wrong in my code. My programm works but not well, for example when the state changes from buttonPin2==HIGH to buttonPin3== HIGH the led1 does not turn off inmediatly, it starts blinking for a few second and then turns off.



 
const int buttonPin2 = 7; 
const int led1 =  1;
const int led2 =  10;
const int led3 = 9;
const int buttonPin3 = 6;

void setup() {

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(buttonPin2, INPUT);
   pinMode(buttonPin3, INPUT);
   }
void loop() {

if (digitalRead(buttonPin2) == HIGH) {
   
 digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
 
  }
else if (digitalRead(buttonPin3) == HIGH ) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(ledPin3, LOW);
     
delay(1000);

digitalWrite(led2, LOW);
 digitalWrite(led1, LOW);
    digitalWrite(led3, HIGH);
    delay(1000);

    }
    else{
      digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
}
}

CrossRoads

You didn't say how pins 2 & 3 are wired up.

Try changing these
  pinMode(buttonPin2, INPUT);
   pinMode(buttonPin3, INPUT);
to
  pinMode(buttonPin2, INPUT_PULLUP); // input with internal pullup resistor enabled
   pinMode(buttonPin3, INPUT_PULLUP);



and wire your buttons to connect them to GND when pressed.
Then look for a low at these lines:
if (digitalRead(buttonPin2) == HIGH) { // change to LOW

else if (digitalRead(buttonPin3) == HIGH ) { // change to LOW








Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

rod322

thanks!! The problem was solved with a pull up resistor

Go Up