Arduino going to sleep?

I have a really basic installation where I have an Arduino Uno that polls a digital pin and waits for an input.

When the button is pressed the Arduino simply displays a message on a thermal printer.

It works great most of the time.

But if I leave everything powered up 24/7, often when I come back after a day or so and press the button, nothing happens and I need to power cycle the Arduino. At which point it is fine again.

The Uno is being powered by a 9v PSU that is always on.

Is there some weird sleep thing going on?

Any thoughts?

Cheers

Phil

It's really hard to say anything useful without looking at the code.
Please post it (and use [code][/code] tags, or use the </> button).

I've never seen an Arduino going to sleep mode all by itself, so I suspect there's a memory problem or an infinite loop somewhere in your program.

Pieter

Thanks Pieter.

Here is the main loop code.

void loop() {


  buttonState = digitalRead(buttonPin);

  if (buttonState == 0) {
    Serial.println("Somebody pressed the button");

    printRandomSentance (24);
  

  }



}//end loop

The problem is in the code you didn't post, or the wiring.
Please read and follow the directions in the "How to use this forum" post.

Sorry about that, I was trying to make it simple, my bad.

Here is the full code.

The wiring is also pretty simple.

Push button connected to GND and D0
Thermal Printer RX Pin2 and TX Pin5

9v DC is coming in through the DC power jack.

Thanks.

// wrap to 33 
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <avr/pgmspace.h>
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#include "lips.h" // Bonfire logo

#define TX_PIN 2 // Arduino transmit  YELLOW WIRE  labeled RX on printer
#define RX_PIN 5 // Arduino receive   GREEN WIRE   labeled TX on printer


SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial);     // Pass addr to printer constructor
char inchar;
int delimeter = 59; // ";"
long mySelect;
File myFile;
char mydata;
long idx = 0;
const int buttonPin = 0;
int buttonState = 0;         // variable for reading the pushbutton status


void setup() {
  Serial.begin(9600);
  mySerial.begin(19200);
  Serial.println("Starting.....");
  randomSeed(analogRead(A0));
  printer.begin();
  printer.setSize('S');
  pinMode(buttonPin, INPUT_PULLUP);
  SD.begin(10);    // initialise the SD card


} // setup

void loop() {


  buttonState = digitalRead(buttonPin);

  if (buttonState == 0) {
    Serial.println("Somebody pressed the button");

    printRandomSentance (24);
  

  }



}//end loop



void printRandomSentance(int num)
{

  Serial.print ("\nreading fortune number " +num);
  printer.setSize('L');
  printer.justify('C');
  printer.println("----------------");
  printer.println("Chad Fantastic\nFortune Teller");
  printer.setSize('S');
  printer.justify('L');
  printer.println("\n\nChad says....");
  printer.setSize('M');

  //printer.feed(1);

  mySelect = random(1, num);
  myFile = SD.open("chad.txt");

  while (myFile.available()) {
    mydata = (myFile.read());
    if (mydata == 59) { // delimeter
      mydata = 13; // reset my character

      idx++ ; // increment index
    }
    if (idx == mySelect) {
      printer.print(mydata);
      Serial.print(mydata);


    }

  }


  myFile.close();
  idx = 0;

  printer.feed(4);
  printer.setSize('S');
  printer.justify('C');
  printer.printBitmap(lips_width, lips_height, lips_data);

  printer.feed(2);
  printer.println("--------------------------");
  printer.feed(5);


}

There is a conflict between the serial port (RX) and button input on D0 when you use the serial routines.

It is clear what you intend, but I can't imagine how the following can work:

Serial.print ("\nreading fortune number " +num);

D0 and D1 are used for serial communication - if you're using them for serial, you can't use them for other functions (and if you were actually pushing the button, it would break uploading too!). Use a different pin.

So that's probably part of the problem.

If if persists - does pressing the reset button on the Arduino fix it? If not, that suggests the issue is on the printer side (does it go into some sort of power saving mode?)

Great, I'll switch pins and take it from there, if it still fails in 24 hours I'll try reseeting the board but not the printer.

Thanks.

Phil

Ok... I tried a few things.

I moved the pin button to D8 to avoid serial conflict.

After a few days it still stops responding.

If I reset the Arduino using the reset button, it starts working again.

Any other thoughts or even a work around?

Cheers.

Phil

Post ALL the revised code, using code tags.

As requested, here is the updated code. The only thing changed was the pin assignment for the button input.

Thanks,

// wrap to 33
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <avr/pgmspace.h>
#include "Adafruit_Thermal.h"
#include "SoftwareSerial.h"
#include "lips.h" // Bonfire logo

#define TX_PIN 2 // Arduino transmit  YELLOW WIRE  labeled RX on printer
#define RX_PIN 5 // Arduino receive   GREEN WIRE   labeled TX on printer


SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial);     // Pass addr to printer constructor
char inchar;
int delimeter = 59; // ";"
long mySelect;
File myFile;
char mydata;
long idx = 0;
const int buttonPin = 8;
int buttonState = 0;         // variable for reading the pushbutton status


void setup() {
  Serial.begin(9600);
  mySerial.begin(19200);
  Serial.println("Starting.....");
  randomSeed(analogRead(A0));
  printer.begin();
  printer.setSize('S');
  pinMode(buttonPin, INPUT_PULLUP);
  SD.begin(10);    // initialise the SD card
  buttonState = digitalRead(buttonPin);
  if (buttonState == 0) {
    Serial.println("Starting.....");
    printAll (24);

  }



} // setup

void loop() {


  buttonState = digitalRead(buttonPin);

  if (buttonState == 0) {
    Serial.println("Somebody pressed the button");

        printRandomSentance (24);

    // delay(1000);

  }



}//end loop



void printRandomSentance(int num)
{

  Serial.print ("\nreading fortune number " + num);
  printer.setSize('L');
  printer.justify('C');
  printer.println("----------------");
  printer.println("Chad Fantastic\nFortune Teller");
  printer.setSize('S');
  printer.justify('L');
  printer.println("\n\nChad says....");
  printer.setSize('M');

  //printer.feed(1);

  mySelect = random(1, num);
  myFile = SD.open("chad.txt");

  while (myFile.available()) {
    mydata = (myFile.read());
    if (mydata == 59) { // delimeter
      mydata = 13; // reset my character

      idx++ ; // increment index
    }
    if (idx == mySelect) {
      printer.print(mydata);
      Serial.print(mydata);


    }

  }


  myFile.close();
  idx = 0;

  printer.feed(4);
  printer.setSize('S');
  printer.justify('C');
  printer.printBitmap(lips_width, lips_height, lips_data);
  // printer.println("What's the worst\nthat could happen?");

  printer.feed(2);
  printer.println("--------------------------");
  printer.feed(5);


}



void printAll(int num)
{


  printer.println("\n\nPrinter Test....");
  printer.setSize('M');

  printer.feed(1);

  myFile = SD.open("chad.txt");

  for (int i = 0; i < num; i++) {

    while (myFile.available()) {
      mydata = (myFile.read());
      printer.print(mydata);
      Serial.print(mydata);
      if (mydata == 59) { // delimeter

        mydata = 13; // reset my character
        Serial.println("");
        printer.feed(2);
   
        idx++ ; // increment index
      }


    }

  }

  myFile.close();
  idx = 0;


}

What does this line actually do?

Serial.print ("\nreading fortune number " + num);

jremington:
What does this line actually do?

"\nreading fortune number " is an array of chars.

"\nreading fortune number " + num is an offset into that array.

so "\nreading fortune number " + 9 would point to the 'f' in that array.

If num is greater than the length of the buffer, then it points to 'somewhere in your address space' and dereferencing it may lead to unexpected behavior.

Edit to add: So in your code, first it prints that string from position 1, so the colon and the space, then from position 2, so just the space, and then from position 3, so just the terminating zero, i.e. empty string. If you do that with a longer sting than "i: ", you should see it more clearly, like this:

ood morning
od morning
d morning

That code was meant for debugging but I don't think it is causing my sleeping Arduino issue.

Cheers

Phil

dereferencing it may lead to unexpected behavior.

My point, exactly.

It's probably not sleeping but crashed; the reason might be what was stated earlier.

And do yourself (and everybody else) a favour and use ';' for the delimiter instead of 59 and '\r' instead of 13. Makes it a million times easier to understand.

philspitler:
That code was meant for debugging but I don't think it is causing my sleeping Arduino issue.

Don't think :wink: Prove it to yourself by removing that line or modifying it.

Thanks, I'll do these fixes and report back.

It take a few days of inactivity before the issue shows up.

Much appreciated.

Phil