Circuit not working properly button doesn't work properly, help needed!


i have created traffic light. needs stay green 20 seconds yellow 5 seconds , red 30 seconds. need have switch allow me have yellow led blink until hit switch again making traffic light work again.

the problem blinking yellow led blinks when button pushed , held program run. if button pushed right away blinks until released if pushed again nothing happens, traffic light runs. if not pressed right away , pressed after traffic light running, nothing occurs, traffic light continues run.

please me figure out why button not working time.

i have following code:

int red = 13;
const int yellow = 12;
int green = 11;
const int buttonpin = 2;


int buttonstate = 0;

void setup() {
pinmode(red,output);
pinmode(yellow,output);
pinmode(green,output);
pinmode(buttonpin,input);
}
void changelights() {
  //green off, yellow 5 seconds
  digitalwrite(green,low);
  digitalwrite(yellow,high);
  delay(5000);
 
  //turn off yellow, turn red on 30 seconds
  digitalwrite(yellow,low);
  digitalwrite(red,high);
  delay(30000);

 
  //turn off red , yellow, turn on green 20 seconds
  digitalwrite(yellow,low);
  digitalwrite(red,low);
  digitalwrite(green,high);
  delay(20000);
}

void loop() {
  // read value of switch
  buttonstate = digitalread(buttonpin);
  // if switch high, ie. pushed down - change lights
  if (buttonstate == high) {
  changelights();
  }
  if (buttonstate == low) {
     digitalwrite(yellow, high);   // turn led on (high voltage level)
  delay(200);               // wait second
  digitalwrite(yellow, low);    // turn led off making voltage low
  delay(200);}
}

you need check current button state against previous button state. if button not pressed, , pressed, something. long stays pressed, nothing. similarly, button pressed, , not pressed, else. long remains unpressed, nothing.

nick gammon has written excellent tutorial @ http://gammon.com.au/switches


Arduino Forum > Using Arduino > Project Guidance > Circuit not working properly button doesn't work properly, help needed!


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support