Read button from setup function

I am programming a M5Paper device, so basically an ESP32 with an E-Ink touchscreen attached. I have successfully programmed a weather sketch which shuts down for most of the time to preserve the battery power, the screen being E-Ink continues to display the weather info during shutdown. Therefore the loop() function is not used at all.

I am more used to programing non-E-Ink devices and so not using the loop() has been a challange for me, but now I have come against an issue where not using the loop() is proving very difficult;

I want to offer the user the opportunity to switch between Daylight Saving Time and Summertime via a switch or via the touchscreen. However this usually depends on having the digitalRead(buttonPin); (on an Arduino) or if (M5.BtnL.wasPressed()) (on M5Paper) in the loop() function so that it is constantly monitored. Can this command be used at the start of the setup() function? I have tried it with a delay() with no success and I don't know if it is even possible. Below is the code I have tried for the M5Paper;

void setup() {
  M5.begin();
  M5.EPD.SetRotation(90);
  M5.EPD.Clear(true);
  canvas.createCanvas(540, 960);
 //attempr to set DST
  M5.update(); 
  if (M5.BtnL.wasPressed()) DST = 1;
  if (M5.BtnL.wasPressed()) DST = 2;
  Serial.print("DST = ");
  Serial.println(DST);
  delay(500);
..................

That conclusion does not logically follow on from

Perhaps I should have said "Therefore there is no requirement to use the loop() function."

No, but it could. That would make reading the button much easier

In any case, it looks like your current code

  if (M5.BtnL.wasPressed()) DST = 1;
  if (M5.BtnL.wasPressed()) DST = 2;

is always going to result in DST being set to 2

I am not explaining myself very well. If the device is in shutdown mode, the loop() cannot function anyway, can it? Are you saying I can shut the device down but still have the loop() checking for a button press?

If the device is in shutdown then the setup() function is not running either, so why the need to detect the button press in setup() ?

What I want is for the user to have the chance at startup to set the DST. I don't want to use the loop() because I need the device to shut down to save battery life.

things like this are commonly handled with dip switches or pull-up/down resistors tied to some pin (even an output pin before it is configured). that pin is read once in setup

You can use the loop() function to allow the user to set the DST at startup. It seems to me that it would be easier to do that than trying to shoehorn the code into setup()

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