Yes Sorry.. I didn't set the button wire as in "pullup resistor", because I wanted it trigger by +5V instead. However, after I swap the "HIGH" to "LOW" and add "delay", it work so fine as what I wanted. Thank you so much.
Then I play a bit more of it. I put more buttons but the output still 2 pins. I was thinking if I can make different output action by selecting each button. I set 6 button input. The result was fine and it worked, even though the sketch is too long. I don't know how to compact it become simple sketch. So, I just add multiple process with different action on each. You may look at below...
by the way, how to make a page for source like you did?
anyway, I just copy it to here then... sorry if it make long text..
const byte in1 = 2;
const byte in2 = 3;
const byte in3 = 4;
const byte in4 = 5;
const byte in5 = 6;
const byte in6 = 7;
const byte outE1 = 8;
const byte outE2 = 9;
const int led = 10;
unsigned long timeStarted;
byte state1 = 0;
byte currentButtonState1;
byte previousButtonState1 = HIGH;
byte state2 = 0;
byte currentButtonState2;
byte previousButtonState2 = HIGH;
byte state3 = 0;
byte currentButtonState3;
byte previousButtonState3 = HIGH;
byte state4 = 0;
byte currentButtonState4;
byte previousButtonState4 = HIGH;
byte state5 = 0;
byte currentButtonState5;
byte previousButtonState5 = HIGH;
byte state6 = 0;
byte currentButtonState6;
byte previousButtonState6 = HIGH;
//For LED Standby Pin 10
int ledMODE0 = 1;
//For LED Blinking Pin 10
int ledMODE1 = 0;
int ledState = HIGH;
long previousMillis = 0;
long interval = 100;
void setup()
{
Serial.begin(115200);
pinMode(outE1, OUTPUT);
pinMode(outE2, OUTPUT);
pinMode(led, OUTPUT);
pinMode(in1, INPUT);
pinMode(in2, INPUT);
pinMode(in3, INPUT);
pinMode(in4, INPUT);
pinMode(in5, INPUT);
pinMode(in6, INPUT);
}
void loop()
{
//------------------Switch Select 1 - START
switch (state1) //only execute the code for the current state 1
{
case 0: //waiting for first button press
currentButtonState1 = digitalRead(in1);
if (currentButtonState1 == HIGH && previousButtonState1 == LOW) //button1 has become pressed
{
digitalWrite(outE1, HIGH);
ledMODE1 = 1;
timeStarted = millis();
state1 = 1;
}
break;
case 1: //wait 1000ms to Turn OFF
if (millis() - timeStarted >= 300)
{
digitalWrite(outE1, LOW);
timeStarted = millis();
state1 = 2;
}
break;
case 2: //wait 1000ms to Turn OFF
if (millis() - timeStarted >= 200)
{
digitalWrite(outE1, HIGH);
timeStarted = millis();
state1 = 3;
}
break;
case 3: //wait 1000ms to Turn OFF
if (millis() - timeStarted >= 500)
{
digitalWrite(outE1, LOW);
ledMODE1 = 0;
timeStarted = millis();
state1 = 0;
}
break;
}
delay(20);
previousButtonState1 = currentButtonState1;
//--------------------------------------------//----------------------------------------------
//------------------Switch Select 2 - START---------------------------------------------------
switch (state2) //only execute the code for the current state 2
{
case 0: //waiting for first button press
currentButtonState2 = digitalRead(in2);
if (currentButtonState2 == HIGH && previousButtonState2 == LOW) //button2 has become pressed
{
digitalWrite(outE1, HIGH);
ledMODE1 = 1;
timeStarted = millis();
state2 = 1;
}
break;
case 1: //wait 1500ms to Turn OFF
if (millis() - timeStarted >= 1500)
{
digitalWrite(outE1, LOW);
ledMODE1 = 0;
timeStarted = millis();
state2 = 0;
}
break;
}
delay(20);
previousButtonState2 = currentButtonState2;
//--------------------------------------------//----------------------------------------------
//------------------Switch Select 3 - START---------------------------------------------------
switch (state3) //only execute the code for the current state 3
{
case 0: //waiting for first button3 press
currentButtonState3 = digitalRead(in3);
if (currentButtonState3 == HIGH && previousButtonState3 == LOW) //button3 has become pressed
{
digitalWrite(outE1, HIGH);
ledMODE1 = 1;
timeStarted = millis();
state3 = 1;
}
break;
case 1: //wait 2000ms to Turn OFF
if (millis() - timeStarted >= 2000)
{
digitalWrite(outE1, LOW);
ledMODE1 = 0;
timeStarted = millis();
state3 = 0;
}
break;
}
delay(20);
previousButtonState3 = currentButtonState3;
//--------------------------------------------//----------------------------------------------
//------------------Switch Select 4 - START---------------------------------------------------
switch (state4) //only execute the code for the current state 4
{
case 0: //waiting for first button4 press
currentButtonState4 = digitalRead(in4);
if (currentButtonState4 == HIGH && previousButtonState4 == LOW) //button4 has become pressed
{
digitalWrite(outE1, HIGH);
ledMODE1 = 1;
timeStarted = millis();
state4 = 1;
}
break;
case 1: //wait 2500ms to Turn OFF
if (millis() - timeStarted >= 2500)
{
digitalWrite(outE1, LOW);
ledMODE1 = 0;
timeStarted = millis();
state4 = 0;
}
break;
}
delay(20);
previousButtonState4 = currentButtonState4;
//--------------------------------------------//----------------------------------------------
//------------------Switch Select 5 - START---------------------------------------------------
switch (state5) //only execute the code for the current state 5
{
case 0: //waiting for first button5 press to Turn ON
currentButtonState5 = digitalRead(in5);
if (currentButtonState5 == HIGH && previousButtonState5 == LOW) //button5 has become pressed
{
digitalWrite(outE1, HIGH);
ledMODE1 = 1;
timeStarted = millis();
state5 = 1;
}
break;
case 1: //waiting for first button5 press to Turn OFF
currentButtonState5 = digitalRead(in5);
if (currentButtonState5 == HIGH && previousButtonState5 == LOW) //button5 has become pressed
{
digitalWrite(outE1, LOW);
ledMODE1 = 0;
timeStarted = millis();
state5 = 0;
}
break;
}
delay(20);
previousButtonState5 = currentButtonState5;
//--------------------------------------------//----------------------------------------------
//------------------Switch Select 6 - START---------------------------------------------------
switch (state6) //only execute the code for the current state 6
{
case 0: //waiting for first button6 press
currentButtonState6 = digitalRead(in6);
if (currentButtonState6 == HIGH && previousButtonState6 == LOW) //button6 has become pressed
{
digitalWrite(outE1, HIGH);
ledMODE1 = 1;
timeStarted = millis();
state6 = 1;
}
break;
case 1: //wait 1000ms with pin 2 HIGH then turn on out2
if (millis() - timeStarted >= 1000)
{
digitalWrite(outE2, HIGH);
timeStarted = millis();
state6 = 2;
}
break;
case 2: //wait 1000ms with both outs on then turn off out2
if (millis() - timeStarted >= 1000)
{
digitalWrite(outE2, LOW);
state6 = 3;
}
break;
case 3: //wait for button to be pressed then turn off out1 and go back to the start
currentButtonState6 = digitalRead(in6);
if (currentButtonState6 == HIGH && previousButtonState6 == LOW) //button6 has become pressed
{
digitalWrite(outE1, LOW);
ledMODE1 = 0;
state6 = 0;
}
break;
}
delay(20);
previousButtonState6 = currentButtonState6;
//--------------------------------------------//----------------------------------------------
//LED Indicator Status------------------------------------------------------------------------
// StandBy LED ON
if(ledMODE0 == 1)
{
digitalWrite(led, HIGH);
}
// Blinking LED State Led Pin 10
unsigned long currentMillis = millis();
if(ledMODE1 == 1)
{
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(led, ledState);
}
}
}
Any idea?
Thanks.