///////////////////////////////////////////////////////////////////////////
// Name Task to understand Switch Statement
// Author: Jamed
// Version: 0.1
// Description: Program with flash lamps
//////////////////////////////////////////////////////////////////////////
// definitions
/////////////////////////////////////////////////////////////////////////////
int USER_BUTTON_1 = 2;
int LAMP_1 = 3;
int LAMP_2 = 4;
int SeqNo = 0;
int SeqNoNext = 10;
int FlashCount = 0;
int userbuttonStatus;
void setup(){
/////////////////////////////////////////////////////////////////////////
// IO Assignment
/////////////////////////////////////////////////////////////////////////
pinMode(USER_BUTTON_1,INPUT);
pinMode(LAMP_1, OUTPUT);
pinMode(LAMP_2, OUTPUT);
//////////////////////////////////////////////////////////////////////////
//Serial setup
//////////////////////////////////////////////////////////////////////////
Serial.begin(9600);
}
void loop(){
//////////////////////////////////////////////////////////////////////////
// Get Input Statuses
/////////////////////////////////////////////////////////////////////////
userbuttonStatus = digitalRead(USER_BUTTON_1);
////////////////////////////////////////////////////////////////////////////
// Update Sequence Numbers
//////////////////////////////////////////////////////////////////////////
if(SeqNo!= SeqNoNext)
{
SeqNo = SeqNoNext;
}
switch(SeqNo)
{
/////////////////////////////////////////////////////////////////////////////
// case 10 - Wait User Button
///////////////////////////////////////////////////////////////////////////
case 10:
Serial.println("wait User Button");
if(userbuttonStatus == 1)
{
FlashCount = 0;
SeqNoNext = 20;
}
break;
/////////////////////////////////////////////////////////////////////////////
//case 20 - Flash Lamp 1
/////////////////////////////////////////////////////////////////////////////
case 20:
Serial.println("FlashLamp:");
FlashCount++;
if((FlashCount%2) == 0) // modulus divide by 2 and give you the rmainder
{
digitalWrite(LAMP_1, LOW);
}
else
{
digitalWrite(LAMP_1, HIGH);
}
if(FlashCount >= 10)
{
SeqNoNext = 30;
}
break;
//////////////////////////////////////////////////////////////////////////////
//case 30 - Turn Lamp 2 ON and Turn Lamp1 OFF
/////////////////////////////////////////////////////////////////////////////
case 30:
Serial.println("Turn Lamp 2 ON and Turn Lamp1 OFF");
digitalWrite(LAMP_2, HIGH);
digitalWrite(LAMP_1, LOW);
SeqNoNext = 40;
break;
//////////////////////////////////////////////////////////////////////////////
//case 40: - wait for user to turn Button OFF
//////////////////////////////////////////////////////////////////////////////
case 40:
Serial.println("wait for user to turn Button OFF:");
if(userbuttonStatus == 0)
{
SeqNoNext = 50;
}
break;
/////////////////////////////////////////////////////////////////////////////
//case 50- TURN lAMP2 off
/////////////////////////////////////////////////////////////////////////////
case 50:
Serial.println("TURN lAMP2 off");
digitalWrite(LAMP_2, LOW);
SeqNoNext = 10;
break;
}
delay(1000);
}