Maurin Donneaud

Designer d’interaction @ DataPaulette, Hackerspace textile, 19 rue Garibaldi 93100 Montreuil (Seine-Saint-Denis).

Plug/Arduino/firmware

  1. // Aubergine electrique farcie aux de puces 1.0
  2. // Aubergine Plug is a plug to switch on and off objects that are plug in it.
  3. #define SWITCH 5
  4. typedef struct type_plug {
  5.   unsigned int switch_pin;
  6.   unsigned int pot_pin;
  7.   unsigned int triac_pin;
  8.   boolean switch_val;
  9.   unsigned int pot_val;
  10.   unsigned long currentMillis;
  11.   unsigned long previousMillis;
  12.   unsigned long interval;
  13.   boolean triacState;
  14.   unsigned int master;
  15. };
  16. unsigned int switch_sum = 0;
  17. unsigned long masterCurrentMillis = 0;
  18. unsigned long masterPreviousMillis = 0;
  19. unsigned long masterTempo = 2000;
  20. boolean masterTriacState = 0;
  21. type_plug plug[SWITCH] = {
  22.   {2, 0, 7, 0, 0, 0, 0, 0, 0, 0},
  23.   {3, 1, 8, 0, 0, 0, 0, 0, 0, 0},
  24.   {4, 2, 9, 0, 0, 0, 0, 0, 0, 0},
  25.   {5, 3, 10, 0, 0, 0, 0, 0, 0, 0},
  26.   {6, 4, 11, 0, 0, 0, 0, 0, 0, 0},
  27. };
  28. boolean DEBUG_pot = false;         // print pot val to the serial port
  29. boolean DEBUG_switch = false;      // print switch val to the serial port
  30. boolean DEBUG_triac = false;       // print triac val to the serial port
  31. void setup(){
  32.   if(DEBUG_pot || DEBUG_switch || DEBUG_triac) Serial.begin(9600);  // set the boad rate of the communication ligne
  33.   for(int i=0; i<SWITCH; i++){
  34.     pinMode(plug[i].switch_pin, INPUT);
  35.     pinMode(plug[i].triac_pin, OUTPUT);
  36.   }
  37. }
  38. void loop(){
  39.   switch_sum = 0;                                                            // reset switch_sum
  40.   for(int i=0; i<SWITCH; i++){
  41.     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
  42.     switch_sum += plug[i].switch_val;                                        // make sum of all switchs value to alow first presed one selection
  43.     if(plug[i].switch_val == 1 && switch_sum == 1){                          // is master ?
  44.       plug[i].master = 1;
  45.     }
  46.     else {
  47.       plug[i].master= 0;
  48.     }
  49.   }
  50.   ///////////////////////////////////////////////////////////////////////////////////////////////
  51.   for(int i=0; i<SWITCH; i++){
  52.     if(plug[i].switch_val == 0 || plug[i].master == 1){     // if the switch is not activeted or the plug is the master
  53.       plug[i].pot_val = analogRead(plug[i].pot_pin);        // read Arduino pot_pin
  54.       plug[i].interval = plug[i].pot_val * 6;               // mapping between pot_val(0-1024) and delay(0-16384) ms
  55.       plug[i].currentMillis = millis();                     // set time plug counter  
  56.     }
  57.    
  58.     if(plug[i].switch_val == 1 && plug[i].master == 0){     // si le switch est activé et que le plug n'est pas le master
  59.       plug[i].interval = masterTempo;                       // set plug tempo to masterTempo
  60.       plug[i].previousMillis = masterPreviousMillis;          // synchronise plug time
  61.       plug[i].currentMillis = masterCurrentMillis;          // synchronise plug time
  62.       plug[i].triacState = masterTriacState;
  63.     }
  64.    
  65.     if(plug[i].master == 1){                                // if a switch is the first pressed it is used as masterTempo
  66.       masterTempo = plug[i].interval;                       // set it interval as masterTempo
  67.       masterCurrentMillis = plug[i].currentMillis;
  68.       masterPreviousMillis = plug[i].previousMillis;        // set it timing as masterCurrentMillis
  69.       masterTriacState = plug[i].triacState;
  70.     }
  71.    
  72.     if(plug[i].currentMillis - plug[i].previousMillis >= plug[i].interval){   // if time is over interval
  73.       if(plug[i].triacState == 0){
  74.         plug[i].triacState = 1;
  75.       }
  76.       else {
  77.         plug[i].triacState = 0;
  78.       }
  79.       digitalWrite(plug[i].triac_pin, plug[i].triacState);
  80.       plug[i].previousMillis = plug[i].currentMillis;                         // init time plug counter  
  81.     }
  82.     if(DEBUG_switch){
  83.       Serial.print(plug[i].switch_pin, DEC), Serial.print("\t");
  84.       Serial.print(plug[i].switch_val, DEC), Serial.print("\t");
  85.       Serial.print(switch_sum, DEC), Serial.print("\t");
  86.       Serial.print(plug[i].master, DEC), Serial.print("\t");
  87.       Serial.print(masterTempo, DEC);
  88.     }
  89.     if(DEBUG_pot){
  90.       Serial.print(plug[i].switch_pin, DEC), Serial.print("\t");
  91.       Serial.print(plug[i].currentMillis, DEC), Serial.print("\t");
  92.       Serial.print(plug[i].interval, DEC), Serial.print("\t");
  93.       Serial.print(plug[i].pot_val);
  94.     }
  95.     if(DEBUG_triac){
  96.       Serial.print(plug[i].currentMillis, DEC), Serial.print("\t");
  97.       Serial.print(plug[i].previousMillis, DEC), Serial.print("\t");
  98.       Serial.print(plug[i].interval, DEC), Serial.print("\t");
  99.       Serial.print(plug[i].triacState, DEC);
  100.     }
  101.     if(DEBUG_pot || DEBUG_switch || DEBUG_triac) Serial.println();
  102.   }
  103. }

Forum sur abonnement

Pour participer à ce forum, vous devez vous enregistrer au préalable. Merci d'indiquer ci-dessous l'identifiant personnel qui vous a été fourni. Si vous n'êtes pas enregistré, vous devez vous inscrire.

Connexions'inscriremot de passe oublié ?