// Aubergine electrique farcie aux de puces 1.0
// Aubergine Plug is a plug to switch on and off objects that are plug in it.
#define SWITCH 5
typedef struct type_plug {
unsigned int switch_pin;
unsigned int pot_pin;
unsigned int triac_pin;
boolean switch_val;
unsigned int pot_val;
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long interval;
boolean triacState;
unsigned int master;
};
unsigned int switch_sum = 0;
unsigned long masterCurrentMillis = 0;
unsigned long masterPreviousMillis = 0;
unsigned long masterTempo = 2000;
boolean masterTriacState = 0;
type_plug plug[SWITCH] = {
{2, 0, 7, 0, 0, 0, 0, 0, 0, 0},
{3, 1, 8, 0, 0, 0, 0, 0, 0, 0},
{4, 2, 9, 0, 0, 0, 0, 0, 0, 0},
{5, 3, 10, 0, 0, 0, 0, 0, 0, 0},
{6, 4, 11, 0, 0, 0, 0, 0, 0, 0},
};
boolean DEBUG_pot = false; // print pot val to the serial port
boolean DEBUG_switch = false; // print switch val to the serial port
boolean DEBUG_triac = false; // print triac val to the serial port
void setup(){
if(DEBUG_pot || DEBUG_switch || DEBUG_triac) Serial.begin(9600); // set the boad rate of the communication ligne
for(int i=0; i<SWITCH; i++){
pinMode(plug[i].switch_pin, INPUT);
pinMode(plug[i].triac_pin, OUTPUT);
}
}
void loop(){
switch_sum = 0; // reset switch_sum
for(int i=0; i<SWITCH; i++){
plug[i].switch_val = (digitalRead(plug[i].switch_pin) == 0) ? 1 : 0; // Read Arduino input switch_pin, 1 is switch ON, 0 is switch OFF
switch_sum += plug[i].switch_val; // make sum of all switchs value to alow first presed one selection
if(plug[i].switch_val == 1 && switch_sum == 1){ // is master ?
plug[i].master = 1;
}
else {
plug[i].master= 0;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////
for(int i=0; i<SWITCH; i++){
if(plug[i].switch_val == 0 || plug[i].master == 1){ // if the switch is not activeted or the plug is the master
plug[i].pot_val = analogRead(plug[i].pot_pin); // read Arduino pot_pin
plug[i].interval = plug[i].pot_val * 6; // mapping between pot_val(0-1024) and delay(0-16384) ms
plug[i].currentMillis = millis(); // set time plug counter
}
if(plug[i].switch_val == 1 && plug[i].master == 0){ // si le switch est activé et que le plug n'est pas le master
plug[i].interval = masterTempo; // set plug tempo to masterTempo
plug[i].previousMillis = masterPreviousMillis; // synchronise plug time
plug[i].currentMillis = masterCurrentMillis; // synchronise plug time
plug[i].triacState = masterTriacState;
}
if(plug[i].master == 1){ // if a switch is the first pressed it is used as masterTempo
masterTempo = plug[i].interval; // set it interval as masterTempo
masterCurrentMillis = plug[i].currentMillis;
masterPreviousMillis = plug[i].previousMillis; // set it timing as masterCurrentMillis
masterTriacState = plug[i].triacState;
}
if(plug[i].currentMillis - plug[i].previousMillis >= plug[i].interval){ // if time is over interval
if(plug[i].triacState == 0){
plug[i].triacState = 1;
}
else {
plug[i].triacState = 0;
}
digitalWrite(plug[i].triac_pin, plug[i].triacState);
plug[i].previousMillis = plug[i].currentMillis; // init time plug counter
}
if(DEBUG_switch){
Serial.print(plug[i].switch_pin, DEC), Serial.print("\t");
Serial.print(plug[i].switch_val, DEC), Serial.print("\t");
Serial.print(switch_sum, DEC), Serial.print("\t");
Serial.print(plug[i].master, DEC), Serial.print("\t");
Serial.print(masterTempo, DEC);
}
if(DEBUG_pot){
Serial.print(plug[i].switch_pin, DEC), Serial.print("\t");
Serial.print(plug[i].currentMillis, DEC), Serial.print("\t");
Serial.print(plug[i].interval, DEC), Serial.print("\t");
Serial.print(plug[i].pot_val);
}
if(DEBUG_triac){
Serial.print(plug[i].currentMillis, DEC), Serial.print("\t");
Serial.print(plug[i].previousMillis, DEC), Serial.print("\t");
Serial.print(plug[i].interval, DEC), Serial.print("\t");
Serial.print(plug[i].triacState, DEC);
}
if(DEBUG_pot || DEBUG_switch || DEBUG_triac) Serial.println();
}
}