arkhipenko / TaskScheduler

Cooperative multitasking for Arduino, ESPx, STM32, nRF and other microcontrollers
http://playground.arduino.cc/Code/TaskScheduler
BSD 3-Clause "New" or "Revised" License
1.26k stars 230 forks source link

OLED turn off after some time #66

Closed tstanescu1 closed 6 years ago

tstanescu1 commented 6 years ago

Thank you for this great scheduler.. I've been able to do pretty much everything I've needed except one weird task.

I'm trying to set up a timeout on an OLED screen connected to the arduino. There is a button that can turn it off or on, but I'd like to implement a timeout after let's say 60 seconds to save the screen.

I've set up all the tasks, and tried to use the t1.delay(60000); method but it didn't do anything, eg:

void displayWake() {

u8x8.setPowerSave(false); //screen is on sleepTimerTask.setCallback(&displaySleep); //method sets power save to true sleepTimerTask.delay(60000); //delay before the function above is executed

} where the sleep timer is: Task sleepTimerTask(TASK_SECOND, TASK_ONCE, &displaySleep);

Thanks again for your help T

arkhipenko commented 6 years ago

Each task can be assigned a timeout since version 2.6.0 What you need to do is:

#include <TaskScheduler.h>
...
void displaySleep();

Scheduler ts;
Task sleepTimerTask(TASK_MINUTE, TASK_ONCE, &displaySleep, ts, false);

void displaySleep() {
// do whatever needs to be done to turn the OLED Off
}

void displayWake() { 
  u8x8.setPowerSave(false); //screen is on
  sleepTimerTask.restartDelayed(); //restart the timer function
}

If anywhere in the code you want to reset the timeout (i.e., a button is pressed):

if (buttonPin == HIGH) {
  // do button stuff
  sleepTimerTask.restartDelayed(); //restart the timer function
}

You can also combine timeout function with a task that does something else. Details are here: timeout

Example is here: timeout example

tstanescu1 commented 6 years ago

Thanks so much for your reply. Unfortunately I can't seem to wrap my head around this timeout. I've tried several combinations and it doesn't seem like the displaySleep function is engaging, it doesn't sleep. Do I need to enable anything else? What about using something like the following, where I could add one task after the other?

Task t2(5 * TASK_SECOND, TASK_FOREVER, &task2Callback, &ts, false, NULL, &task2OnDisable);

tstanescu1 commented 6 years ago

Here is the code I have right now, it's quite messy sorry: `/ Simple version of the wake up on capacitive button /

include //Display library

include

//Capacitative Button int pushButton = 11;

define DISPLAY_AUTOSLEEP_MS 15000 // sleep display on idle (after ms)

define DISPLAY_BLIT_FREQ 1000 // redraw display every second

// Forward declarations for Display void blitDisplay(); //Function that draws the displays data on OLED, but no backlight here void tick(); //Function that senses rotary click or movement to wake up screen void displaySleep(); //Function that makes screen sleep void displayWake(); //Function that wakes up screen

// logic variables //int timelapseInterval = 0; //bool timelapseRunning = false; bool blit = true;

// Library initializations U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/ reset=/ U8X8_PIN_NONE);

Scheduler scheduler; Task sleepTimerTask(TASK_SECOND * 10, TASK_ONCE, &displaySleep, &scheduler, false); Task blitDisplayTask(DISPLAY_BLIT_FREQ, TASK_FOREVER, &blitDisplay);

//Task sensorDataSend(TASK_SECOND, TASK_FOREVER, &sensorRead);

void setup(void) {

pinMode(pushButton, INPUT); //Capacitive touch sensor pinMode(TdsSensorPin, INPUT); //TDS/PPM Sensor u8x8.begin(); //u8x8.setFont(u8x8_font_pxplusibmcgathin_r); u8x8.setFont(u8x8_font_chroma48medium8_r); u8x8.setContrast(0); //dims it down

// Initialize the scheduler. scheduler.init(); scheduler.addTask(blitDisplayTask); // scheduler.addTask(sleepTimerTask); //blitDisplayTask.enable(); //sleepTimerTask.enable(); scheduler.enableAll(); // sleepTimerTask.setTimeout(10 * TASK_SECOND);

}

void loop(void) { //Sensors: ppm(); tick(); // tick inputs scheduler.execute(); // tick scheduler

}

void blitDisplay() { //Function that displays on OLED

if (blit) { // blit must be on for screen to draw, but doesnt control backlight //ON / OFF ? //u8x8.drawString(13, 7, timelapseRunning ? "ON " : "OFF"); // X, Y, if timelapserunning TRUE? then u8x8.setInverseFont(1); u8x8.drawString(0, 0, "HydraPonics V0.2");

//PH Display
u8x8.setInverseFont(1);
u8x8.drawString(0, 2, "pH Value:       ");
u8x8.setInverseFont(0);
u8x8.setCursor(0, 3);
float pHValue = ph();
u8x8.print(pHValue, 2);
u8x8.drawString(5, 3, "pH");
//u8x8.setCursor(9, 3);
//u8x8.print(voltage,2);
//u8x8.drawString(14, 3, "v");

//PPM/EC Display
u8x8.setInverseFont(1);
u8x8.drawString(0, 4, "PPM/EC:         ");
u8x8.setInverseFont(0);
u8x8.setCursor(0, 5);
u8x8.print(tdsValue, 0);
u8x8.drawString(5, 5, "ppm");
u8x8.setCursor(9, 5);
u8x8.print(tdsValue * 2 / 1000);
u8x8.drawString(14, 5, "EC");

//Temp Display
u8x8.setInverseFont(1);
u8x8.drawString(0, 6, "Temperature:    ");
// u8x8.setFont(u8x8_font_amstrad_cpc_extended_r);
u8x8.setInverseFont(0);
u8x8.setCursor(0, 7);
float temperature = getTemp();
//SENSOR_DATA[1];
u8x8.print(temperature);
u8x8.drawString(5, 7, "^C");
u8x8.setCursor(8, 7);
u8x8.print(temperature * 9 / 5 + 32);
u8x8.drawString(13, 7, "^F");

sleepTimerTask.enable();

} blit = false; //drawing done, blitter off }

void tick(void) { //Checks button, and rotary, and wakes up screen on click

//CAPACITIVE TOUCH TO TURN SCREEN ON/OFF - SIMPLE BUT IN THE FUTURE SHOULD ALSO TIMEOUT AND MAKE PIN OFF int buttonState = digitalRead(pushButton); if (buttonState == HIGH) { //If button is clicked, device is not sleeping blit = true; displayWake(); } else { displaySleep(); blit = false;

}

}

void displaySleep() { u8x8.setPowerSave(true); blit = false; Serial.print("Sleep"); }

void displayWake() { u8x8.setPowerSave(false); Serial.print("Awake"); } `

arkhipenko commented 6 years ago

Let's take this offline from the issues. Please send me an email to arkhipenko@hotmail.com

arkhipenko commented 5 years ago

@tstanescu1 : Do you still want the updated code? I have it written just need your email to send it to. Please send me an email to arkhipenko@hotmail.com

tstanescu1 commented 5 years ago

Hi, i'm sorry I had to pause my project for a bit. I will send you an email as soon as I start it back up.

Thank you for all your help!