Push button state and sleep mode

Hello,

I am a bit stuck with the implementation of a sleep mode in my code. Here is a resume of the behavior i want :

  • When i hit the button one time, the led start fading for x seconds.
  • When the x seconds are elapsed then the led turn off
  • If i hit the button again while the led is still fading, the led turn off

I want to simply implement a sleep mode so that :

  • When i hit the button one time, the led start fading for x seconds AND the sleep mode turn off
  • When the x seconds are elapsed then the led turn off AND the sleep mode turn on
  • If i hit the button again while the led is still fading, the led turn off AND the sleep mode turn on

The problem i have is that when i hit the button while the led is still fading, it also lauch the interrupt so i've added a delay(250); like this

      myLEDTurnOff();
      delay(250);
      Going_To_Sleep();

But now :

  • If i hit the button again while the led is still fading, the led turn off AND the sleep mode turn on BUT when i want to hit the button again to turn the led on, nothing happens, it seems to get stuck case 0 -> LEDoff
    Here's what's the monitor return when it get stuck
18:39:12.400 -> i'm in case ledfading
18:39:12.435 -> 6
18:39:12.502 -> i'm in case ledoff
18:39:12.718 -> Sleep ON
-------> I hit the button again but nothing happen
18:39:14.433 -> Sleep OFF
18:39:14.433 -> Detach Interrupt pin
18:39:14.470 -> i'm in case ledoff
18:39:14.723 -> Sleep ON

Maybe i get stuck in the case 0 (ledoff) because it doesn't increment, so it can't switch to the default case (ledfading) ?

#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes

// Constants remain the same:
const int  buttonPin = 2;
const byte redPin = 11;
const byte greenPin = 10;
const byte bluePin = 9;
const int potPin = A1;

// Variables will change:
// button
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 1;         // current state of the button
int lastButtonState = 1;     // previous state of the button
// time
unsigned long ledElapsedTime = 0;
unsigned long ledStartTime = 0;
int hasTimedOut = false;
// luminosity
unsigned long rLuminosity;
unsigned long gLuminosity;
unsigned long bLuminosity;
// color
int r;
int g;
int b;

enum states {LEDOff};
states ourState;


void setup() {

  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);


}

void loop() {

  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {

    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      buttonPushCounter++;
      Serial.println(buttonPushCounter);
      ledStartTime = millis();
      //Serial.println("Appuyer");
    } else {
      //Serial.println("Relacher");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  switch (buttonPushCounter % 2) {
    case 0:
      Serial.println("i'm in case ledoff");
      myLEDTurnOff();
      delay(250);
      Going_To_Sleep();
      delay(50);
      break;
    default:
      // if nothing else matches, do the default
      // default is optional
      Serial.println("i'm in case ledfading");
      myLEDFading ();
      delay(50);
      break;
  }

  changeColor();

}

void Going_To_Sleep(){
    Serial.println("Sleep ON");
    sleep_enable();//Enabling sleep mode
    attachInterrupt(digitalPinToInterrupt(buttonPin), wakeUp, LOW);//attaching a interrupt to pin d2
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
    delay(1000); //wait a second to allow the led to be turned off before going to sleep
    sleep_cpu();//activating sleep mode
    
  }
 
void wakeUp(){
  sleep_disable();//Disable sleep mode
  Serial.println("Sleep OFF");//Print message to serial monitor
  detachInterrupt(digitalPinToInterrupt(buttonPin)); //Removes the interrupt from pin 2;
  Serial.println("Detach Interrupt pin");
}

void myLEDFading () {
  // This function make the LED Fade
  ledElapsedTime = (millis() - ledStartTime);
  rLuminosity = map(ledElapsedTime, 10000, 0, 0, r);
  gLuminosity = map(ledElapsedTime, 10000, 0, 0, g);
  bLuminosity = map(ledElapsedTime, 10000, 0, 0, b);

  hasTimedOut = ledElapsedTime >= 3000;
  if (hasTimedOut) {
    myLEDTurnOff();
    buttonPushCounter++;
  }

  analogWrite(redPin, rLuminosity);
  analogWrite(greenPin, gLuminosity);
  analogWrite(bluePin, bLuminosity);

  //Serial.print ("luminosity :");
  //Serial.println(luminosity);
  //delay(50);
}

void myLEDTurnOff() {
  // This function turn off the LED
  ourState = LEDOff;
  digitalWrite(redPin, ourState);
  digitalWrite(greenPin, ourState);
  digitalWrite(bluePin, ourState);
  //Serial.print ("ourstate :");
  //Serial.println(ourState);
  //delay(50);

}

void changeColor() {
  // Reading the value of the pot pin (it's always 0 - 1024)
  // and converting it to a RGB range (0 - 255).
  int potPinValue = map(analogRead(potPin), 0, 1024, 0, 255);

  if (potPinValue <= 63.5) {
    r = 255;
    g = 48;
    b = 0;
    //Serial.println("color #1 = aurore");
  }

  if ((potPinValue > 63.5) && (potPinValue <= 127)) {
    r = 191;
    g = 127;
    b = 156;
    //Serial.println("color #2 = warm light");
  }

  if ((potPinValue > 127) && (potPinValue <= 190.5)) {
    r = 102;
    g = 127;
    b = 110;
    //Serial.println("color #3 = ice light");
  }

  if ((potPinValue > 190.5) && (potPinValue <= 255)) {
    r = 20;
    g = 0;
    b = 255;
    Serial.println("color #4 = night blue");
  }
  //delay(80);

}

You will need to post your code in code tags or attach it if it is greater than 9K. Most people will not go to a link to get your code.

I just added it @Todd, thanks for the advice

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.