#include <Bounce2.h> // See https://github.com/thomasfredericks/Bounce2/wiki
#define number_of_programs 5
const unsigned long time_debounce = 4; // time needed for debounce the push button + optocoupler
const int time_off = 1000;
/*
* Array that contains the brightness-values for all LED at all program states
*
* Structure:
* Program | LED channel 1 | LED channel 2 | LED Spots
* ---------+--------------------------------------------
* 0 | 0 1 2
* 1 | 3 4 5
* 2 | 6 7 8
* n | (3 * n) (3 * n + 1) (3 * n + 2)
* Values in table-content above represent the index of the array.
* You SHALL define every value! The first 3 values shall be zero for switching
* the lamp completely of by pressing the push button for time_off time.
*/
const byte LED[number_of_programs * 3] =
{
0, 0,0,
4, 4,0,
255, 10,0,
255,255,0,
255,255,1
};
/*
* Pin-Configuration:
* Important to use pin 3 and 11. In setup, the neccessary CPU registers
* for phase correct PWM were set for these 2 pins. For details:
* https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
*
*/
const int input = 12; // Input from the optocoupler
const int SpotRelaisPin = 8; // Controll output to 230V AC relay for turning LED spots on/off
const int transformer = 7; // Controll output to 230V AC relay for turning LED-stripe transformer on/off
const int LEDchannel1 = 3; // First channel of LED stripe
const int LEDchannel2 = 11; // Second channel of LED stripe
// Definition globaler Variablen
byte program = 0; // counter for active light program
unsigned long time_pushed = 0; // Point of time, when push button was closed last time
unsigned long time_released = 0;
Bounce deb_input = Bounce(); // Constructor of deb_input
// Initialisierung
void setup()
{
pinMode(input, INPUT); //-------------
pinMode(SpotRelaisPin, OUTPUT);
pinMode(transformer, OUTPUT); // Set all needed Pins to In or out
pinMode(LEDchannel1, OUTPUT);
pinMode(LEDchannel2, OUTPUT); //-------------
deb_input.interval(time_debounce);
deb_input.attach(input);
/*
* The following two lines will preset the registers in Atmega 328 to
* phase correct PWM at pins 3 and 11.
*
* Just modifying OCR2A (0-255) will configure duty cycle of pin 3
* Just modifying OCR2B (0-255) will configure duty cycle of pin 11
*
* For details: https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
*
*/
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20); // Initialising of phase correct PWM
TCCR2B = _BV(CS22); // for pins 3 and 11
digitalWrite(transformer, HIGH); // Make sure that both channels to the relay are HIGH
digitalWrite(SpotRelaisPin, HIGH); // The relay will switch, if the output is LOW
}
/*
* set_led();
*
* The function wich set all the outputs and manipulates the CPU-PWM registers.
*
* It reads the actual program and actual brightnes values from
* LED[program+[1,2,3]]
*
*/
void set_led()
{
if(LED[(program * 3) + 2] > 0)
digitalWrite(SpotRelaisPin, LOW);
else
digitalWrite(SpotRelaisPin, HIGH);
if(LED[(program * 3) + 0] > 0 || LED[(program * 3) + 1] > 0)
digitalWrite(transformer, LOW);
else
digitalWrite(transformer, HIGH);
OCR2A = LED[(program * 3) + 0];
OCR2B = LED[(program * 3) + 1];
}
/*
* void loop()
*
* Main function of all arduino sketches.
* It checks the input continuously and increasy program counter by 1 if
* Push button is pushed, released and waited for end of debounce time.
*
* If button is pushed and hold for time_setup milliseconds, setup_led() will
* edit the actual program.
*/
void loop()
{
deb_input.update();
if (deb_input.rose())
time_pushed = millis();
if (deb_input.fell())
time_released = millis();
if (deb_input.fell() && time_released - time_pushed < time_off)
{
if(program < (number_of_programs - 1))
program++;
else
program = 0;
}
else
if(deb_input.fell() && time_released - time_pushed >= time_off)
program = 0;
set_led();
}