This is code to make a led blink 5 times then stay on till the button is pushed. --button1 pushed led blinks 5 times, led stays on, button2 pushed led goes off-- There are three different examples doing the exact same thing using loop function, nested for loop and switch case. It would be great if somebody else would post code doing the exact same thing with the same board set up to show the different and possibly better ways to do it. Possibly using simplefsm.h or other libraries. It would be better to only add one more library pure code post, or if you like to add two libraries post one code using only one library, a second code using the only the other library and a third code using both libraries. etc This may really help!
loop function
int ledPin = 8;//attach led to pin 8
int button1 = 5;//attach button1 to pin 5
int button2 = 6;//attach button2 to pin 6
void setup()
{
pinMode(ledPin, OUTPUT);//led pin is now an output
pinMode(button1, INPUT_PULLUP);//button1 pullup resistor
pinMode(button2, INPUT_PULLUP);//button2 pullup resistor
}
void loop()
{
if (digitalRead(button1) == LOW)//reads the state of the button
{
digitalWrite(ledPin, HIGH);//led on
delay(250);
digitalWrite(ledPin, LOW);//led off
delay(250);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
digitalWrite(ledPin, HIGH);
delay(250);
digitalWrite(ledPin, LOW);
delay(250);
digitalWrite(ledPin, HIGH);//turns led on after the lase blink
}
if (digitalRead(button2) == LOW)//reads the state of the button
{
digitalWrite(ledPin, LOW);// led off
}
}
nested for loop
int ledPin = 8;//attach led to pin 8
int button1 = 5;//attach button1 to pin 5
int button2 = 6;//attach button2 to pin 6
void setup()
{
pinMode(ledPin, OUTPUT);//led pin is now and output
pinMode(button1, INPUT_PULLUP);//button1 pullup resistor
pinMode(button2, INPUT_PULLUP);//button2 pullup resistor
}
void loop()
{
if (digitalRead(button1) == LOW)//reads the state of the button
{
for (int x=0; x<5; x++)//"nested for loop" makes the led blink 5 times
{
digitalWrite(ledPin, HIGH);//led on
delay(250);
digitalWrite(ledPin, LOW);//led off
delay(250);
}
digitalWrite(ledPin, HIGH);//turns led on after nested for loop
}
if (digitalRead(button2) == LOW)//reads the state of the button
{
digitalWrite(ledPin, LOW);//led off
}
}
switch case
int ledPin = 8;// attach led to pin 8
int button1 = 5;// attach button1 to pin 5
int button2 = 6;// attach button2 to pin 6
enum {off, blink, on} currentState;// the three states of the led
void setup()
{
pinMode(ledPin, OUTPUT);//led pin 8 is now an output
pinMode(button1, INPUT_PULLUP);//button1 pullup resistor
pinMode(button2, INPUT_PULLUP);//button2 pullup resistor
}
void loop()
{
switch(currentState)// currentState as the switch varible
{
case off://first state of the switch case
if (digitalRead(button1) == LOW)//reads the state of button1
{
for (int x=0; x<5; x++)//"nested for loop" makes led blink 5 times
{
digitalWrite(ledPin, HIGH);//led on
delay(250);
digitalWrite(ledPin, LOW);//led off
delay(250);
}
{
digitalWrite(ledPin, HIGH);//turns led on after nested loop is finished
}
currentState = blink;//the state the switch case is in right now
}
break;
case blink://second state of the switch case
{
digitalWrite(ledPin, HIGH);//the led is on in this state
currentState = on;//the state the switch case is in right now
}
break;
case on://third state of the switch case
if (digitalRead(button2) == LOW)//reads the state of the button
{
digitalWrite(ledPin, LOW);//led off
currentState = off;//the state the switch case is in right now
}
break;
}
}