// Flash the blue LED for 500 ms every 10 seconds
// If the button has been pressed for at least 1 s, light the red LED for 5s
// (Not fussed what happens to the blue LED during that time, but ideally
// would prefer to use millis for that 5s, rather than delay)
// Report events in the serial monitor during testing
// (It's a simplified learning sketch. When I have it working I'll adapt for
// a more complex servo and motion sensor program.)
// Button via 10k to 5V; input to D6 goes low when pressed
// Triggers red LED when input to D6 from button has remained low for >= 1s
// Report timestamps for testing
// Constants
const byte triggerPin = 6; // Low input from button to D6
const byte blueLEDPin = 7; // Blue LED flashes at 10s intervals
const byte redLEDPin = 8; // Large red LED, high for 5s when triggered
const unsigned long int interval = 500; // 10s
// Variables
int triggerCounter = 0; // Counts number of triggers
int FlashCounter = 0; // Counts number of flashes
unsigned long previousMillis = 0; // Effectively the program start time
unsigned long triggerStart; // Time when D6 goes low
void setup()
{
pinMode(triggerPin, INPUT); // D6
pinMode(blueLEDPin, OUTPUT); // D7
pinMode(redLEDPin, OUTPUT); // D8
Serial.begin(9600);
Serial.println("RegularBlinks Minus ButtonTrigger");
Serial.println("");
}
# define blueOffTime 850
# define blueOnTime 150
unsigned long previousBlueOffTime;
unsigned long previousBlueOnTime;
void loop()
{
unsigned long currentMillis = millis(); // Get current time
// Check to see if it's time to flash the blue LED
if (digitalRead(blueLEDPin)) { // blue light is on
if (currentMillis - blueOnTime > previousBlueOnTime) {
digitalWrite(blueLEDPin, LOW);
previousBlueOffTime = currentMillis;
}
}
else { // blue light is off
if (currentMillis - blueOffTime > previousBlueOffTime) {
digitalWrite(blueLEDPin, HIGH);
previousBlueOnTime = currentMillis;
}
} // End of regular flash code
} // End of Void Loop
// delay(80); // Brief flash. BUT SHOULD I USE MILLIS?