Pages: [1]   Go Down
Author Topic: Want to make my lcd to go to sleep when input isnt changed after 3 seconds  (Read 330 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I built an arduino project that changes a certain value via press buttons and displays it on a 16x2 character lcd
What i want to do is to send the screen to sleep if the input hasnt changed in three seconds
Is it possible? And if so how?
Logged

NSW Australia
Offline Offline
Faraday Member
**
Karma: 87
Posts: 3521
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Presumably by "send the screen to sleep", you mean turn off the backlight.  There is little economy to be achieved by shutting down the actual LCD display driving circuit.

I would think at least five seconds would be more reasonable.  In any case, it is easy enough to do given that you are either using a backpack which provides control of the backlight, or a shield which does (bearing in mind the design fault described in one of the "sticky" threads here) or you are controlling it with a transistor of your own arrangement.

Your "loop" code if written correctly, would allow for a countdown timer which is continuously advanced to 3000 (or whatever value you decide on) beyond the current "millis()" value on any keypress, and when in the loop, that value is exceeded, the backlight is turned off.

If you need more description than that, it is your turn to post your code for review.   smiley-grin
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code will be uploaded soon
And say i have control of the backlight
Can you send me a link to where i can find the certain library i need to use or a tut that exsplaind the main idea
Understanding how to set backlight on and off with the mills()
Thanks
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Any updates on this?  I'm interested in some sample code.
I want to make the backlight sleep unless there is button action and/or motion sensed by PIR sensor.
Logged

NSW Australia
Offline Offline
Faraday Member
**
Karma: 87
Posts: 3521
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I submit to you my code extending the "Blink Without Delay" as an example of performing multiple timing functions simultaneously.  Worth playing with in itself, so that you understand it and can then extract the timing function for your own use.  It is long, but readily extensible to any reasonable degree.  A logical adaptation would be to use arrays for many similar control operations.
Code:
// Blink without "delay()" - multi!

const int led1Pin =  13;    // LED pin number
const int led2Pin =  10;
const int led3Pin =  11;
int led1State = LOW;        // initialise the LED
int led2State = LOW;
int led3State = LOW;
unsigned long count1 = 0;   // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) {
    *marker += interval;    // move on ready for next interval
    return true;       
  }
  else return false;
}

// Auxiliary function to intiate a new delay without checking
void arm(unsigned long *marker, unsigned long interval) {
  *marker = millis() + interval;    // set the next interval
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far,
    if (button == HIGH) return false; // Nothing happening!
    else {
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far,
    if (button == LOW) return false; // Nothing happening!
    else {
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else {
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else {
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    { 
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);     
  pinMode(led2Pin, OUTPUT);     
  pinMode(led3Pin, OUTPUT);     
}

void loop() {
  // Act if the latter time (ms) has now passed on this particular counter,
  if (timeout(&count1, 500UL )) {
    if (led1State == LOW) {
      led1State = HIGH;
    }
    else {
      led1State = LOW;
    }
    digitalWrite(led1Pin, led1State);
  }

  if (timeout(&count2, 300UL )) {
    if (led2State == LOW) {
      led2State = HIGH;
    }
    else {
      led2State = LOW;
    }
    digitalWrite(led2Pin, led2State);
  }

  if (timeout(&count3, 77UL )) {
    if (led3State == LOW) {
      led3State = HIGH;
    }
    else {
      led3State = LOW;
    }
    digitalWrite(led3Pin, led3State);
  }
}

Use the "arm" function to defer a timeout.
Logged

Pages: [1]   Go Up
Jump to: