#define PUSHED LOW //+5V----INPUT_PULLUP(20k)----Input Pin----[N.O. Switch]----GND
#define LEDon HIGH //Pin---->|----[220R]----GND -->|-- LED
#define LEDoff !LEDon
const byte button = 2;
const byte led1 = 8;
const byte led2 = 9;
const byte led3 = 10;
const byte led4 = 11;
const byte led5 = 12;
const byte led6 = 14;
const byte led7 = 15;
const byte led8 = 16;
const byte led9 = 17;
const byte led10 = 18;
const byte led11 = 19;
const byte led12 = 20;
const byte led13 = 21;
const byte led14 = 22;
const byte led15 = 23;
const byte heartbeatLED = 13;
const byte heartbeat1LED = 7;
byte mState = 0; //states 0-5 are valid
byte lastState;
//timing stuff
const unsigned long period = 1000; //time for "delay"
unsigned long commonMillis;
unsigned long currentMillis;
unsigned long switchMillis;
unsigned long heartbeatMillis;
unsigned long heartbeat1Millis;
void setup() {
setup1();
setup2();
}
void loop() {
loop1();
loop2();
}
//**********************************************************************************
void setup1()
{
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
pinMode (led5, OUTPUT);
pinMode (led6, OUTPUT);
pinMode (led7, OUTPUT);
pinMode (led8, OUTPUT);
pinMode (led9, OUTPUT);
pinMode (led10, OUTPUT);
pinMode (led11, OUTPUT);
pinMode (led12, OUTPUT);
pinMode (led13, OUTPUT);
pinMode (led14, OUTPUT);
pinMode (led15, OUTPUT);
pinMode (heartbeatLED, OUTPUT);
pinMode (heartbeat1LED, OUTPUT);
pinMode (button, INPUT_PULLUP);
lastState = digitalRead(button);
} //END of setup()
//**********************************************************************************
void loop1()
{
currentMillis = millis();
//********************************
//to help show if there is any blocking code, this LED toggles every 500ms
if (currentMillis - heartbeatMillis >= 500)
{
//start this TIMER
heartbeatMillis = currentMillis;
digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
}
currentMillis = millis();
//********************************
//to help show if there is any blocking code, this LED toggles every 500ms
if (currentMillis - heartbeat1Millis >= 1000)
{
//start this TIMER
heartbeat1Millis = currentMillis;
digitalWrite(heartbeat1LED, !digitalRead(heartbeat1LED));
}
checkSwitches();
stateMachine();
//********************************
// Other none blocking code goes here
//********************************
} //END of loop()
void stateMachine()
{
switch (mState)
{
//*************
case 0:
//do nothing
break;
//*************
case 1:
digitalWrite (led1, LEDon);
//start the TIMER
commonMillis = currentMillis;
//next state
mState = 2;
break;
//*************
case 2:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led2, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 3;
}
break;
//*************
case 3:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led3, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 4;
}
break;
//*************
case 4:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led4, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 5;
}
break;
//*************
case 5:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led5, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 6;
}
break;
//*************
case 6:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led6, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 7;
}
break;
//*************
case 7:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led7, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 8;
}
break;
//*************
//*************
case 8:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led8, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 9;
}
break;
//*************
case 9:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led9, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 10;
}
break;
//*************
case 10:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led10, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 11;
}
break;
//*************
case 11:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led11, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 12;
}
break;
//*************
case 12:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led12, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 13;
}
break;
//*************
case 13:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led13, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 14;
}
break;
//*************
case 14:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led14, LEDon);
//restart the TIMER
commonMillis = currentMillis;
//next state
mState = 15;
}
break;
//*************
case 15:
if (currentMillis - commonMillis >= period)
{
digitalWrite (led15, LEDon);
}
break;
} //END switch/case
} //END of stateMachine()
void checkSwitches()
{
//*************************************
//is it time to check the switch ?
if (currentMillis - switchMillis < 100)
{
//not time to read the switch
return;
}
//restart the TIMER
switchMillis = currentMillis;
//*************************************
//was there a change in switch state ?
byte currentState = digitalRead(button);
if (lastState == currentState)
{
//there was no switch change
return;
}
//update to the current state
lastState = currentState;
//*************************************
//did the switch go closed ?
if (currentState == PUSHED)
{
mState = 1;
}
//is it time to check the switch ?
if (currentMillis - switchMillis < 100)
{
//not time to read the switch
return;
}
} //END of checkSwtiches()
//********************************************
//phasers
int buttonState = 0;
const int ledPin_ph = 26; // the number of the LED pin
const int ledPin_ph1 = 25;
int buttonPin_ph = 24;
// Variables will change:
int ledState = LOW; // ledState used to set the LED
int ledState1 = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long previousMillis1 = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1550; // interval at which to blink (milliseconds)
const long interval1 = 1150; // interval at which to blink (milliseconds)
void setup2() {
// set the digital pin as output:
pinMode(ledPin_ph, OUTPUT);
pinMode(ledPin_ph1, OUTPUT);
pinMode(buttonPin_ph, INPUT);
}
void loop2() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
buttonState = digitalRead(24);
if (buttonState == HIGH) {
digitalWrite(25, HIGH);
delay(45);
digitalWrite(25, LOW);
delay(45);
}
else if (buttonState == LOW) {
digitalWrite(25, LOW);
}
buttonState = digitalRead(24);
if (buttonState == HIGH) {
digitalWrite(26, HIGH);
delay(45);
digitalWrite(26, LOW);
delay(45);
}
else if (buttonState == LOW) {
digitalWrite(26, LOW);
}
unsigned long currentMillis = millis();
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;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin_ph, ledState);
}
unsigned long currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= interval1) {
// save the last time you blinked the LED
previousMillis1 = currentMillis1;
// if the LED is off turn it on and vice-versa:
if (ledState1 == LOW) {
ledState1 = HIGH;
} else {
ledState1 = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin_ph1, ledState1);
}
}