// a set of variable of type unsigned long with 3 elements
// accessing of each variable is done through the index-number in brackets
// index-numbers start with ZERO
// LEDTimer[0] = ...
// LEDTimer[1] = ...
// LEDTimer[2] = ...
unsigned long LEDTimer[3] = {0, 0, 0}; // variables needed for non-blocking timing
// the shortest way to assign values is to use curly braces
// LED_pin[0] = 6;
// LED_pin[1] = 7;
// LED_pin[2] = 12;
// is shortest assigned with
const byte LED_Pin[3] = {10, 11, 12};
// same principle for the relay-pins
const byte relay_Pin[3] = {7, 8, 9};
// same principle for the button-pins
//const byte button_Pin[4] = {2, 3, 4, 5};
const byte button_Pin[4] = {A0, A1, A2, A3};
const byte OnBoard_LED = 13;
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
}
}
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
PrintFileNameDateTime();
configure_IO_Pins();
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED, 250);
check_Buttons_Switches_LEDs_Relays();
}
void configure_IO_Pins() {
// count variable Index from 0 to 2
for (byte Index = 0; Index < 3; Index++) {
pinMode(LED_Pin[Index], OUTPUT);
digitalWrite(LED_Pin[Index], LOW);
pinMode(relay_Pin[Index], OUTPUT);
digitalWrite(relay_Pin[Index], LOW);
}
// count variable Index from 0 to 3
for (byte Index = 0; Index < 4; Index++) {
pinMode(button_Pin[Index], INPUT_PULLUP);
}
}
void printButtonPressMessage(byte p_Idx) {
Serial.print("button connected to button_pin[");
Serial.print(p_Idx);
Serial.println("] is pressed");
Serial.print("which means button connected to IO-pin number ");
Serial.println(button_Pin[p_Idx]);
}
void check_Buttons_Switches_LEDs_Relays() {
// iterate through buttons 0,1,2
for (byte Index = 0; Index < 3; Index++) {
// check if a button is pressed
if ( digitalRead(button_Pin[Index]) == LOW) {
// if a button is pressed
printButtonPressMessage(Index);
// switch on LED and relay
digitalWrite(LED_Pin[Index], HIGH);
digitalWrite(relay_Pin[Index], HIGH);
LEDTimer[Index] = millis(); // store snapshot of time
}
// check if a timer-variable is UN-equal to zero
// which indicates LED is switched on timing is ACTIVE
if (LEDTimer[Index] != 0) {
// check how much time has passed by since switching on LED
if ( TimePeriodIsOver(LEDTimer[Index], 5000) ) {
Serial.print("LEDTimer[");
Serial.print(Index);
Serial.println("] expired switch LED off");
// if more than 5000 milliseconds have passed by
digitalWrite(LED_Pin[Index], LOW); // switch LED OFF
LEDTimer[Index] = 0; // assign value zero to indicate timer IN-active
}
}
}
// check if button 4 is pressed
if ( digitalRead(button_Pin[3]) == LOW) {
// if button 4 IS pressed switch off all LEDs and all relays
for (byte Index = 0; Index < 3; Index++) {
digitalWrite(LED_Pin[Index], LOW);
digitalWrite(relay_Pin[Index], LOW);
}
}
}
void PrintFileNameDateTime() {
Serial.println( F("Code running comes from file ") );
Serial.println( F(__FILE__) );
Serial.print( F(" compiled ") );
Serial.print( F(__DATE__) );
Serial.print( F(" ") );
Serial.println( F(__TIME__) );
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long & startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}