Bonjour à tous! Dans des billets antérieurs, je vous présentais un montage simple de rampe  à LED intelligente, basée sur un Arduino. Suite aux nombreuses requêtes, voici le billet fournissant le code de la lampe à LED intelligente.

Je modifierai ce billet ultérieurement pour expliquer le code de la lampe à LED intelligente, mais il est commenté (au moins les variables, que vous pouvez ajuster), et fonctionne. Donc vu la demande, je poste déjà le code, que voici ci après (je le mettrai sur un dépot github) :

/**
 * Licence : GNU GPL V3
 * by Audrey robinel
 * this simple code for Arduino controls an automatic light, that activates
 * and deactivate depending on the output of a PIR sensor. There is also a progressive
 * fading in and fading out. fade speed can be set in the code.
 * v0.4
 */

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 4;   // the number of the pushbutton pin
const int ledPin = 3;      // the number of the LED pin
int ir_sensor_pin=7;       // where the PIR sensor output is plugged

int buttonLedPin=6;
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by



//int lightsState = LOW;              //LOW = no change, HIGH = changing.
int lightFadeDir=0;                   //0= no change, 1= fading in, -1 = fading out.
int lightBrightness=0;                //light current brightness, from 0 to 255. starts at 0 by default
int lightFadeAmount=1;                //by how much we change the light level at each iteration
int lightFadeInDelay=5;               //how long do we wait between two light adjustment (ms) for fade in
int lightFadeOutDelay=30;             //how long do we wait between two light adjustment (ms) for fade out
long lastLightBrightnessChangeTime=0; //last time when light was adjusted

int ledState = LOW;           // the current state of the output pin
int buttonState;              // the current reading from the input pin
int lastButtonState = LOW;    // the previous reading from the input pin

int bl_brightness = 0;        // how bright the LED indicator is
int bl_fadeAmount = 1;        // how much the LED changes at each step
long last_bl_change_time = 0; // when did the led indicator change last
int bl_fade_time=5;           // how many time between to LED indicator adjustments

int mode=2;               //mode 1 is manual mode
                          //mode 2 is auto mode


long buttonPressStartTime=0;      //when the button started beeing pressed
const long longPressDuration=1000;  //the duration of a long press (ms)



long last_ir_detection_time=0;    // last time when an IR event was detected.
long auto_off_delay=120000;       //how long to wait for lights shutdown after the last IR event

long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
int i=0;

void setup() 
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ir_sensor_pin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonLedPin, OUTPUT);
  Serial.begin(9600);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() 
{
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  if(mode==2)//if auto mode
  {
    // set the brightness of the button's LED:
    analogWrite(buttonLedPin, bl_brightness);
    if (millis() > (last_bl_change_time + bl_fade_time))
    {
      bl_brightness=bl_brightness+bl_fadeAmount;
      last_bl_change_time=millis();
      if (bl_brightness <= 0 || bl_brightness >= 255) 
      {
        bl_fadeAmount = -bl_fadeAmount ;
      }
      if(bl_brightness>255)
          {bl_brightness=255;}
      else if(bl_brightness<0) {bl_brightness=0;} } } else if(mode==1) //if manual mode { digitalWrite(buttonLedPin, HIGH); } //debouncing the button if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) 
  {
    if (reading != buttonState) 
    {
      buttonState = reading;
      if (buttonState == LOW)//if the button was pushed
      {

        buttonPressStartTime=millis();

        while(buttonState==LOW)
        {
          buttonState=digitalRead(buttonPin);
          delay(1);
        }

        //long press detected, changing mode
        if(millis()>buttonPressStartTime+longPressDuration)
        {

          if(mode==1)
            {mode=2;}
          else if(mode==2)
            {mode=1;}
        }
        
        //short press detected
        else
        {
          if(lightFadeDir==0)
          {
            if(lightBrightness==0)
            {
              lightFadeDir=1;
            }
            else
            {
              lightFadeDir=-1;
            }
          }
          else
          {
            if(lightFadeDir==1)
            {
              lightFadeDir=-1;
            }
            else if(lightFadeDir==-1)
            {
              lightFadeDir=1;
            }
          }
        }
        
      }
    }
  }

  //change the light brightness
  if(lightFadeDir!=0)
  {
    int fadeWaitTime=0;
    if(lightFadeDir==1)
    {
      fadeWaitTime=lightFadeInDelay;
    }
    else
    {
      fadeWaitTime=lightFadeOutDelay;
    }
    if (millis() > (lastLightBrightnessChangeTime + fadeWaitTime))
    {
      lightBrightness=lightBrightness+lightFadeAmount*lightFadeDir;
      lastLightBrightnessChangeTime=millis();
      if(lightBrightness<=0) { lightBrightness=0; lightFadeDir=0; } else if(lightBrightness>=255)
      {
        lightBrightness=255;
        lightFadeDir=0;
      }
      analogWrite(ledPin,lightBrightness);
     }
  }
  
  int ir_status=digitalRead(ir_sensor_pin);
  if(ir_status==1)
  {
    last_ir_detection_time=millis();
    if(lightFadeDir==0 && mode==2)
    {
      if(lightBrightness<255) { lightFadeDir=1; } } } else { if(millis()>last_ir_detection_time+auto_off_delay && mode==2)
    {
      if(lightBrightness>0)
      {
        lightFadeDir=-1;
      }
    }
  }
 
  lastButtonState = reading;
}


Réseaux sociaux