Go Down

Topic: sequencing leds with pushbutton. Need Help! (Read 534 times) previous topic - next topic

jwhite5678

Sep 16, 2015, 10:50 pm Last Edit: Sep 16, 2015, 11:00 pm by jwhite5678
hi,

im trying to program a sequence of things to happen when i push a button. i have spent hours trying to work out why my code dosnt work. when uploaded it does the sequence perfectly but isnt controled by the button. it just runs without any inputs. Please could someone help me as to why it dosnt work?

Heres my code:


Code: [Select]
int val = 0;
int button = 2;
void setup() {
 pinMode(2, INPUT);   //Push Button
 pinMode(29, OUTPUT);  //warning and explosion sound card
 pinMode(24, OUTPUT);  //smoke machine
 pinMode(25, OUTPUT);  //fake flame
 pinMode(26, OUTPUT);  //water sound card
 pinMode(27, OUTPUT);  //warning beacon
 pinMode(28, OUTPUT);  //Finsihed led
}


void loop() {
val = digitalRead(button);       //Is Button pressed?
 if(val == HIGH)  {      //Yes! its pressed, excute these things...
   digitalWrite(28, LOW);   //Finshed led off
   digitalWrite(26, HIGH);  //sound card press
   delay(1000);             // wait for a second
   digitalWrite(26, LOW);   // sound card stop pressing
   delay(14000);            // wait till sound finished
   digitalWrite(27, HIGH);  //warning beacon on
   digitalWrite(29, HIGH);  //warning explosion sound card press
   delay(1000);             // wait 1 sec
   digitalWrite(29, LOW);   //sound card press off
   delay(6000);             //wait 6 secs
   digitalWrite(24, HIGH);  //smoke on
   delay(4000);             // 4 secs smoke
   digitalWrite(24, LOW);   //smoke off
   digitalWrite(25, HIGH);  //fake flame on
   delay(6000);             //wait 6 sec
   digitalWrite(29, LOW);   //ALL OFF
   digitalWrite(24, LOW);
   digitalWrite(25, LOW);
   digitalWrite(26, LOW);
   digitalWrite(27, LOW);                    
   digitalWrite(28, HIGH); // Finshed led
 }
  else  {
   digitalWrite(29, LOW);   //ALL OFF
   digitalWrite(24, LOW);
   digitalWrite(25, LOW);
   digitalWrite(26, LOW);
   digitalWrite(27, LOW);                    
   digitalWrite(28, HIGH);
  }
}




Thanks in advance

Grumpy_Mike

#1
Sep 16, 2015, 10:57 pm Last Edit: Sep 16, 2015, 10:59 pm by Grumpy_Mike
The button is only looked at once per cycle. If you want the button to control anything when you press it then you have to program without using the delay function call.
The blink without delay shows the technique to use.

Please do not post any more code like that. Read the how to use this forum sticky post.

How is your button wired? Using an external resistor?

jwhite5678

my button is just a standard push button wired from pin 2 to the ground

Grumpy_Mike

Then enable the internal pull up resistor in the pinMode function call.

jwhite5678

i shall have to try that out tommorow...

Go Up