Arduino won't stay in sleep mode

Hello all. I'm a bit new to all this so please bear with me.
Anyway a little context before I get to the code portion. So what I'm trying to accomplish is to fade several leds in, then pulse them (fade in and out) 10 times before putting the arduino to sleep with a momentary button as an interrupt. Now I'm sure my code is not the most elegant but I was able to get the leds to do what I wanted, but when I add the code to get the board to go to sleep afterward it either messes with the led sequence or refuses to stay asleep or both. Any help would be appreciated. here's the code

#include <avr/sleep.h>
#define interruptPin 2
const int led = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
const int led5 = 10;
const int WAITTIME = 20;
const int WAITTIME2 = 90;
const int STEP = 5;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  digitalWrite(LED_BUILTIN, HIGH);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}

void loop() {
  int a;

  a = 0;
  while (a <= 255) {
    analogWrite(led, a);
    analogWrite(led2, a);
    analogWrite(led3, a);
    analogWrite(led4, a);
    analogWrite(led5, a);
    delay(WAITTIME2);
    a = a + STEP;
  }
  
for (int b; b<10; b++){
  int i;

  i = 127;
  while (i <= 255){
    analogWrite(led, i);
    analogWrite(led2, i);
    analogWrite(led3, i);
    analogWrite(led4, i);
    analogWrite(led5, i);
    i = i + STEP;}


  i = 255;
  while (i >= 127){
    analogWrite(led, i);
    analogWrite(led2, i);
    analogWrite(led3, i);
    analogWrite(led4, i);
    analogWrite(led5, i);
    i = i - STEP;}

    analogWrite(led, 0);
    analogWrite(led2, 0);
    analogWrite(led3, 0);
    analogWrite(led4, 0);
    analogWrite(led5, 0);
    delay(1000);
    Going_To_Sleep();
}}

void Going_To_Sleep(){
  sleep_enable();
  attachInterrupt(0, wakeUp, LOW);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  sleep_cpu();
  digitalWrite(LED_BUILTIN, HIGH);
}

void wakeUp(){
  sleep_disable();
  detachInterrupt(0);
}

I will take a SWAG and say your input is floating. Post a schematic of your circuit, not a frizzy picture with links to technical information on the hardware devices.


hopefully this is what you're looking for

Use the Autoformat tool in the IDE.

It looks like you are going to sleep within the for loop, I'm at the beach and can't be sure.

a7

Also

  for (int b; b < 10; b++) {

'b' is uninitialised in you for loop, that can make all kindsa things happen.

a7

1 Like

Try also clearing the interrupt flag before using attachInterrupt() and use FALLING instead of LOW.

@alto777 is correct, I think. You have a } in the wrong place. This is why correct indentation is important. Indentation does not change how the code runs, but it does make it easier for us humans to spot errors where { or } are in the wrong place.

While I know this to be true, I never looked into it. I wrote this program to mess with it a bit.

# include <stdio.h>

void function(int k)
{
    for (int b; b < 10; b++) {
        for (int ss = 0; ss < k; ss++) printf("   ");
        printf("level %d before (0?) %d \n", k, b);
        break;
    }
    
    if (k) function(k - 1);

    for (int b; b < 10; b++) {
       for (int ss = 0; ss < k; ss++) printf("   ");
        printf("level %d after (0?) %d \n", k, b);
        break;
    }
}

int main()
{
    printf("Hello World\n");
    
    function(7);

    return 0;
}

It produces different output from run to run, here's one output from it:

Hello World
               level 5 before (0?) -115969989 
            level 4 before (0?) -95325536 
      level 2 before (0?) 0 
   level 1 before (0?) 0 
level 0 before (0?) 0 
level 0 after (0?) 0 
   level 1 after (0?) 0 
      level 2 after (0?) 0 


...Program finished with exit code 0
Press ENTER to exit console.

Fixing the error (which does generate a warning - you all do have warnings turned way up and pay attention to them, right?) gives the expected output, viz:

Hello World
                     level 7 before (0?) 0 
                  level 6 before (0?) 0 
               level 5 before (0?) 0 
            level 4 before (0?) 0 
         level 3 before (0?) 0 
      level 2 before (0?) 0 
   level 1 before (0?) 0 
level 0 before (0?) 0 
level 0 after (0?) 0 
   level 1 after (0?) 0 
      level 2 after (0?) 0 
         level 3 after (0?) 0 
            level 4 after (0?) 0 
               level 5 after (0?) 0 
                  level 6 after (0?) 0 
                     level 7 after (0?) 0 


...Program finished with exit code 0
Press ENTER to exit console.

It may vary from compiler to compiler.

Curiously, the output from a real UNO is correct. I will leave it to the heavies to speculate about why this might be.

a7

My apologies. I believe I posted the wrong iteration of that code. hence the uninitialized 'b' and the 'LOW' rather than 'FALLING'. Anyway here's the one I meant to post. it gives me an error on line 69 of "function-definition not allowed here before '{' token". However it seems that I can't change that without a whole host of other problems arising.

#include<avr/sleep.h>
#define interruptPin 2
const int led = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
const int led5 = 10;
const int WAITTIME = 20;
const int WAITTIME2 = 90;
const int STEP = 5;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  digitalWrite(LED_BUILTIN, HIGH);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}

void loop() {
  int a;

  a = 0;
  while (a <= 255) {
    analogWrite(led, a);
    analogWrite(led2, a);
    analogWrite(led3, a);
    analogWrite(led4, a);
    analogWrite(led5, a);
    delay(WAITTIME2);
    a = a + STEP;
  }

  for (int b = 0; b < 10; b++) {
    int i;

    i = 127;
    while (i <= 255) {
      analogWrite(led, i);
      analogWrite(led2, i);
      analogWrite(led3, i);
      analogWrite(led4, i);
      analogWrite(led5, i);
      delay(WAITTIME);
      i = i + STEP;
    }

    i = 255;
    while (i >= 127) {
      analogWrite(led, i);
      analogWrite(led2, i);
      analogWrite(led3, i);
      analogWrite(led4, i);
      analogWrite(led5, i);
      delay(WAITTIME);
      i = i - STEP;

      analogWrite(led, 0);
      analogWrite(led2, 0);
      analogWrite(led3, 0);
      analogWrite(led4, 0);
      analogWrite(led5, 0);
      Going_To_Sleep();
    }
  }
  void Going_To_Sleep() {
    sleep_enable();
    attachInterrupt(0, wakeUp, FALLING);
    set_sleep_mode(SLEEP_MODE_PWR_DWN);
    digitalWrite(LED_BUILTIN, LOW);
    delay(50);
    sleep_cpu();
    digitalWrite(LED_BUILTIN, HIGH);
    )

    void wakeUp() {
      sleep_disable();
      detachInterrupt(0);
    }
  }
}

Nope it does not show much of anything. Where are the pull up resistors for I2C?

You cannot place a function definition within another function.

This is basic syntax. Error messages my be a bit hard to read, but they do tell you what is wrong.

And you still going to sleep inside the for loop, so every time, not just after ten times.

a7

Alright so I think I understand what you mean. here's what I have now. I ran it through debug mode and everything seems to go fine until the 'Going_To_Sleep' function, where it seems to just continue right on past 'sleep_cpu' before starting the loop again.

#include<avr/sleep.h>
#define interruptPin 2
const int led = 3;
const int led2 = 5;
const int led3 = 6;
const int led4 = 9;
const int led5 = 10;
const int WAITTIME = 20;
const int WAITTIME2 = 90;
const int STEP = 5;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  digitalWrite(LED_BUILTIN, HIGH);
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}

void loop() {
  int a;

  a = 0;
  while (a <= 255) {
    analogWrite(led, a);
    analogWrite(led2, a);
    analogWrite(led3, a);
    analogWrite(led4, a);
    analogWrite(led5, a);
    delay(WAITTIME2);
    a = a + STEP;
  }

  for (int b = 0; b < 10; b++) {
    int i;

    i = 127;
    while (i <= 255) {
      analogWrite(led, i);
      analogWrite(led2, i);
      analogWrite(led3, i);
      analogWrite(led4, i);
      analogWrite(led5, i);
      delay(WAITTIME);
      i = i + STEP;
    }

    i = 255;
    while (i >= 127) {
      analogWrite(led, i);
      analogWrite(led2, i);
      analogWrite(led3, i);
      analogWrite(led4, i);
      analogWrite(led5, i);
      delay(WAITTIME);
      i = i - STEP;
    }
    analogWrite(led, 0);
    analogWrite(led2, 0);
    analogWrite(led3, 0);
    analogWrite(led4, 0);
    analogWrite(led5, 0);
  }
  Going_To_Sleep();
}

void Going_To_Sleep() {
  sleep_enable();
  attachInterrupt(0, wakeUp, FALLING);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  digitalWrite(LED_BUILTIN, LOW);
  delay(50);
  sleep_cpu();
  digitalWrite(LED_BUILTIN, HIGH);
}

  void wakeUp() {
    sleep_disable();
    detachInterrupt(0);
}

actually I misspoke. it continues past 'sleep_cpu' to the built in led turning on then it bounces back to the 'Going_To_Sleep' function and THEN it starts the loop again

Code doesn't bounce.

I don't doubt your observations, just your interpretation of them.

Put some Serial.print() statements in your code and prove that you are correct… or more likely, see where this thing is really going off the rails.

When I'm home and dry I'll take a closer look - sleep is an art, microprocessor and real life. :expressionless:

You may want to try reading

it is the definitive guide to sleeping. It may seem like a slog, but give it a go pretending like you know what he's talking about, and study the full working examples he offers to make sure you are modeling you attempt to do this on known good sequences of code.

a7

This is a picture. Not a schematic.

thank you for linking me to that. After reading it I think sleep mode isn't really what I was looking for from the get go. It turns out what I wanted was to simply exit the loop and wire the button to reset. now it all works flawlessly. next step is assembly. thanks everyone for the help

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