MolenLight @MolenWest

Installation lumière avec UN GROS BOUTON , arduino , 3 relais, et bandes de leds.

// la fonction assis() se compte en Dixieme de seconde !!!


#include <Adafruit_SleepyDog.h>
#include <EasyButton.h> // https://easybtn.earias.me/docs/fundamentals

#define BTN_PIN 7 // entrée bouton
#define NBRE_R 3 // nbre de relais

const int R_PIN[4] = {3, 4, 5, 6};


// pin, debounce, pullup
EasyButton button(BTN_PIN, 10, true);

unsigned long lastShowTime = 0;  // the last time the output pin was toggled
unsigned long delayShowTime = 120000; // automatique toutes les 5 mins
int cycle = 0;

void setup() {
  //Serial.begin(9600);

  //Serial.println(sizeof(R_PIN));// <<<< comment ca fonctionne ?
  delay(1000);

  for (int i = 0; i < NBRE_R; i++) {
    pinMode(R_PIN[i], OUTPUT);

    //Serial.println(R_PIN[i]);
    delay(10);
  }
  ALL_ON();
  delay(2000);
  button.begin();

  Watchdog.enable(4000);// si pas de reponse durant 4 secondes reboot

}
void loop() {
  button.read();
  Watchdog.reset();
  delay(10);
  if (button.wasReleased() ||  millis() - lastShowTime >= delayShowTime )// se declanche si btn relaché ou toute les
  {

    lastShowTime = millis();
    cycle++;
    delay(10);
    switch (cycle) {
      case 1:
        cycle1();
        break;
      case 2:
        cycle2();
        break;
      case 3:
        cycle3();
        cycle = 0;
        break;
     // case 4:
       // cycle4();
       // break;
     // case 5:
      //  cycle5();
      //  break;
      
      default:
        cycle = 0;
        break;
    }
    Serial.println("out switch");
    delay(10);
    ALL_ON();
    assis(10);
  }
}

void assis(int dSec) {
  if (dSec > 0) {
    for (int i = 0; i < dSec; i++) {
      Watchdog.reset();
      delay(100);
    }
  }
}
void cycle1() {
  ALL_ON();
  Serial.println ("cycle1 on");
  int j;
  for (j = 0; j < 10; j++) {
    for (int i = 0; i < 3; i++) {
      digitalWrite(R_PIN[i], HIGH);
      assis( 10 - j);
      digitalWrite(R_PIN[i], LOW);
    }
  }
  Serial.println ("cycle1 off");
}
void cycle2() {
  Serial.println ("cycle2 on");
  int j = 0;
  for (j; j < 10; j++) {
    ALL_OFF();
    assis(10);
    int r = random(3);
    //Serial.print ("random : ");
    //Serial.println (r);
    digitalWrite(R_PIN[r], LOW);
    assis(10);
  }

  //Serial.println ("cycle2 off");

}

void cycle3() {
  //Serial.println ("cycle3 on");

  for (int j = 0; j < 10; j++) {
    digitalWrite(R_PIN[0], LOW);
    digitalWrite(R_PIN[2], LOW);
    digitalWrite(R_PIN[1], HIGH);
    assis(10);
    digitalWrite(R_PIN[0], HIGH);
    digitalWrite(R_PIN[2], HIGH);
    digitalWrite(R_PIN[1], LOW);
    assis(10);
  }
  //Serial.println ("cycle3 off");
}
void cycle4() {
  //Serial.println ("cycle4 on");
  digitalWrite(R_PIN[3], HIGH);
  assis(20);
  digitalWrite(R_PIN[3], LOW);
  assis(20);
  //Serial.println ("cycle4 off");
}
void cycle5() {
  //Serial.println ("cycle4 on");
  digitalWrite(R_PIN[3], HIGH);
  assis(20);
  digitalWrite(R_PIN[3], LOW);
  assis(20);
  //Serial.println ("cycle4 off");
}

void ALL_ON() {
  //Serial.println ("ALL on");
  for (int i = 0; i < NBRE_R; i++) {
    digitalWrite(R_PIN[i], LOW);

  }
}
void ALL_OFF() {
  //Serial.println ("ALL off");
  for (int i = 0; i < NBRE_R; i++) {
    digitalWrite(R_PIN[i], HIGH);
  }
}
julien L. Écrit par :

Soyez le premier à commenter

Laisser un commentaire