const int led1 = 10;
const int led2 = 11;
const int led3 = 12;
const int led4 = 13; // the pin numbers for the LEDs
int Reset = 4;
int Received = 0;
const int led_Interval = 2300; // number of millisecs Motor is ON
const int blinkDuration = 2500; // number of millisecs that Motor is OFF
const int led_Interval2 = 2100;
const int blinkDuration2 = 2700;
const int led_Interval3 = 1800;
const int blinkDuration3 = 3000;
//------- VARIABLES (will change)
byte led1_State = LOW; // used to record whether the LEDs are on or off
byte led2_State = HIGH; // LOW = off
byte led3_State = HIGH; //
byte led4_State = HIGH;
//------- Time
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousled1Millis = 0; // will store last time the LED was updated
unsigned long previousled2Millis = 0;
unsigned long previousled3Millis = 10;
void setup() {
Serial.begin(9600);
//Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino"); // so we know what sketch is running
//------- set the Led pins as output:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
digitalWrite(led4, LOW);
digitalWrite(Reset, HIGH);
delay(200);
pinMode(Reset, OUTPUT);
Serial.println("wait");
}
void loop() {
if (Serial.available()>0){
Received = Serial.read();
}
// Notice that none of the action happens in loop() apart from reading millis()
// it just calls the functions that have the action code
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
// use the same time for all LED flashes to keep them synchronized
// call the functions that do the work
updateled1_State();
updateled2_State();
switchLeds();
}
//========
void updateled1_State() {
if (led1_State == LOW) {
if (currentMillis - previousled1Millis >= blinkDuration) {
led1_State = HIGH;
previousled1Millis += blinkDuration;
}
}
else {
if (currentMillis - previousled3Millis >= led_Interval) {
led1_State = LOW;
previousled3Millis += led_Interval;
}
}
if (Received == 'a' || Received == 'b'){
led1_State = HIGH;
led2_State = HIGH;
led4_State = HIGH;
}
}
//=========
void updateled2_State(){
if (led2_State == HIGH) {
if (currentMillis - previousled3Millis >= led_Interval) {
led2_State = LOW;
previousled3Millis += led_Interval;
}
}
else {
if (currentMillis - previousled2Millis >= blinkDuration) {
led2_State = HIGH;
previousled2Millis += blinkDuration;
}
}
if (Received == 'a' || Received == 'b'){
led1_State = HIGH;
led2_State = HIGH;
led4_State = HIGH;
}
}
//=======
void switchLeds() {
if (Received == '1' || Received == 'c'){
// this is the code that actually switches the LEDs on and off
digitalWrite(led1, led1_State);
digitalWrite(led2, led2_State);
}
else{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led4, HIGH);
}
//-------- Program Reset both app and arduino program
if (Received == '9'){
Serial.println("wait");
digitalWrite(Reset, LOW);
digitalWrite(Reset, HIGH);
delay(200);
}
}