Part 1 Write a program to run on your Arduino and PortMaster board that will seq
ID: 3750744 • Letter: P
Question
Part 1 Write a program to run on your Arduino and PortMaster board that will sequentially turn on and off the LED segments, so it resembles Cylon eyes (https://youtu.be/UajcgzK2shQ). Use the millis() construct rather than delay(). The traverse from one side to the other should take about 1 second.
Part 2 Modify your code from Problem 1, so that pressing and holding SW10 will cause the speed of traverse to increase continuously; pressing and holding SW11 will cause the speed of traverse to continually decrease; and pressing and holding both button switches will cause LED segments 9 and 0 to blink on and off continuously at a rate of two on-off cycles per second.
Please make sure to provide comments explaing the code!
Explanation / Answer
/*
This pattern will:
Sequentially light 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 one at a time
Sequentially light 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
Stay on for 1.5 seconds
Sequentially turn off 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
Repeat
*/
//Declarations
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; //Sets up output pin array
int pinCount = 10; //Declares number of pins in array
int timer = 500;
void setup(){
//Sets pins 2 through 11 as output pins
for (int thisPin = 0; thisPin < pinCount; thisPin++)
{pinMode(ledPins[thisPin], OUTPUT);}
delay(timer);}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
delay(timer / 10);}
delay(timer);
// Sequential light
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer / 5);}
delay(timer * 3);
// Sequential off
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], LOW);
delay(timer / 5);}
delay(timer);
}
const byte LED1 = 2;
const byte LED2 = 3;
const byte LED3 = 4;
const byte heartBeat = 13;
boolean blockingFlag = true;
unsigned long Millis13;
unsigned long lastMillis;
unsigned long wait = 3000;
enum States {
StateStart, State2, State3, State4, State5, State6, State7
};
States mState = StateStart;
void setup()
{
pinMode(heartBeat, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
delay(3000);
} //END of setup()
void loop()
{
//**********************************
//some code to check for blocking sketches
if (millis() - Millis13 >= 200)
{
digitalWrite(heartBeat, !digitalRead(heartBeat)); //toggle D13
Millis13 = millis(); //re-initalize
}
//********************************** delay() being used here
if (blockingFlag == true)
{
blockingFlag = false;
digitalWrite(13, LOW);
blockingMethod();
}
//********************************** BWD being used here
if (millis() - lastMillis >= wait)
{
nonBlockingMethod();
}
//Other nonblocking code goes here
} //END of loop()
// ******************* Functions *******************
void blockingMethod()
{
//LEDs ON
digitalWrite(LED3, HIGH); // turn on LED3
delay(450); // wait for 450ms
digitalWrite(LED2, HIGH); // turn on LED2
delay(450); // wait for 450ms
digitalWrite(LED1, HIGH); // turn on LED1
delay(3000); // wait for 3000ms
//LEDs OFF
digitalWrite(LED3, LOW); // turn off LED3
delay(450); // wait for 450ms
digitalWrite(LED2, LOW); // turn off LED2
delay(450); // wait for 450ms
digitalWrite(LED1, LOW); // turn off LED1
delay(3000); // wait for 3000ms
} //END of blockingMethod()
void nonBlockingMethod()
{
switch (mState)
{
//*************************** //LEDs ON
case StateStart:
{
digitalWrite(LED3, HIGH); // turn on LED3
wait = 450; // wait for 450ms
lastMillis = millis();
mState = State2;
}
break;
//***************************
case State2:
{
digitalWrite(LED2, HIGH); // turn on LED2
wait = 450; // wait for 450ms
lastMillis = millis();
mState = State3;
}
break;
//***************************
case State3:
{
digitalWrite(LED1, HIGH); // turn on LED1
wait = 3000; // wait for 3000ms
lastMillis = millis();
mState = State4;
}
break;
//*************************** //LEDs OFF
case State4:
{
digitalWrite(LED3, LOW); // turn off LED3
wait = 450; // wait for 450ms
lastMillis = millis();
mState = State5;
}
break;
//***************************
case State5:
{
digitalWrite(LED2, LOW); // turn off LED2
wait = 450; // wait for 450ms
lastMillis = millis();
mState = State6;
}
break;
//***************************
case State6:
{
digitalWrite(LED1, LOW); // turn off LED1
wait = 3000; // wait for 3000ms
lastMillis = millis();
mState = State7;
}
break;
//***************************
case State7:
{
blockingFlag = true;
wait = 3000; // wait for 3000ms
mState = StateStart;
}
break;
//***************************
default:
{
}
break;
} //END of switch case
} //END of nonBlockingMethod()
// ************** END of sketch ****************
//***************************
case State2:
{
digitalWrite(LED2, HIGH); // turn on LED2
nbmWait = 450; // wait for 450ms
nbmLastMillis = millis();
nbmState = State3;
}
break;
//***************************
case State3:
{
digitalWrite(LED1, HIGH); // turn on LED1
nbmWait = 3000; // wait for 3000ms
nbmLastMillis = millis();
nbmState = State4;
}
break;
//*************************** //LEDs OFF
case State4:
{
digitalWrite(LED3, LOW); // turn off LED3
nbmWait = 450; // wait for 450ms
nbmLastMillis = millis();
nbmState = State5;
}
break;
//***************************
case State5:
{
digitalWrite(LED2, LOW); // turn off LED2
nbmWait = 450; // wait for 450ms
nbmLastMillis = millis();
nbmState = State6;
}
break;
//***************************
case State6:
{
digitalWrite(LED1, LOW); // turn off LED1
nbmWait = 3000; // wait for 3000ms
nbmLastMillis = millis();
nbmState = State7;
}
break;
//***************************
case State7:
{
blockingFlag = true;
nbmWait = 3000; // wait for 3000ms
nbmState = StateStart;
}
break;
//***************************
default:
{
}
break;
} //END of switch case
} //END of nonBlockingMethod()
// ************** END of sketch ****************
// our delays
#define DELAY_ACTION_1 2000 // delay after action A has been done
#define DELAY_ACTION_2 3000 // delay after action B has been done
// current state; iinitialised to WAIT_1 so program starts with that
byte currentState = WAIT_1;
// current time; global so it can be access from everywhere; updated in loop()
unsigned long currentTime;
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loop()
{
// remember the previous time
static unsigned long previousTime = 0;
// get current time
currentTime = millis();
switch (currentState)
{
// wait till first delay has lapsed
case WAIT_1:
// if delay has lapsed
if (currentTime - previousTime >= DELAY_ACTION_1)
{
// change state; next iteration of loop case 'ACTION_1' will be executed
currentState = ACTION_1;
}
break;
// switch led 13 on
case ACTION_1:
// action for step 1
digitalWrite(13, HIGH);
// remember time that this step was done
previousTime = currentTime;
// change state; next iteration of loop case 'WAIT_2' will be executed
currentState = WAIT_2;
break;
// wait till second delay has lapsed
case WAIT_2:
// if delay has lapsed
if (currentTime - previousTime >= DELAY_ACTION_2)
{
// change state; next iteration of loop case 'ACTION_2' will be executed
currentState = ACTION_2;
}
break;
// switch led 13 on
case ACTION_2:
// action for step 2
digitalWrite(13, LOW);
// remember time that this step was done
previousTime = currentTime;
// change state; next iteration of loop case 'WAIT_1' will be executed (again)
currentState = WAIT_1;
break;
}
}
void setup() {
//use a for loop to initialize each pin as an output:
for (int thisPin = 7; thisPin < 14; thisPin++) {
pinMode(thisPin, OUTPUT);
}
pinMode(buttonPin2, INPUT);
}
void loop() {
buttonState2 = digitalRead(buttonPin2);
// when pushbutton is high all related leds light on, in sequence
if (digitalRead(buttonPin2) == HIGH) {
// loop from the lowest pin to the highest:
for (int thisPin = 7; thisPin < 14; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, HIGH);
}
}
// when push button is low all related leds are turned off in sequence
else {//(digitalRead(buttonPin2) == LOW)
// loop from the highest pin to the lowest:
for (int thisPin = 13; thisPin > 6; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, LOW);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}
}