Context
I keep the board in sleep mode and want to wake it with different buttons. I need to know which button has been pushed on boot.
In the following schematic, SW-R is actually used to reset the board.
If I hold SW-D5 or SW-D6 while pushing SW-R, the code can detect which button I pushed.
Problem
I don't want to have to push 2 buttons at the same time and, being new to electronic, I don't know how to wires things up so it reset and set a pin to HIGH simultaneously.
Question
How can I reset the board and trigger a HIGH on an IO corresponding to a button while it boot?
Considering the HIGH only need to be hold for less than a second, can it be simply achieved with capacitors and/or transistors? If so, how? Or any other suggestion?
schematic
simulate this circuit – Schematic created using CircuitLab
The actual code example only contain 1 button but I'll have many.
#include <Spi.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
int buttonPin = D5;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(BUILTIN_LED, OUTPUT);
int buttonValueAtBoot = digitalRead(buttonPin);
Serial.begin(115200);
Serial.println();
if (buttonValueAtBoot == HIGH) {
// I want to trigger this code pushing only 1 button
// should also work with many buttons
Serial.println("button was pressed");
}
digitalWrite(BUILTIN_LED, HIGH);
}
void loop() {
delay(5000);
ESP.deepSleep(0);
}
-
Things would be simpler if your used a "shallower" sleep mode permitting wake on a pin change interrupt. If you want to actually reset the processor, you may need to find a way to activate the reset and the input pin from the same button, apparently in a way that supports multiple buttons, and perhaps also locks out generating additional resets once the chip is awake..Chris Stratton– Chris Stratton2017年10月29日 02:11:06 +00:00Commented Oct 29, 2017 at 2:11
-
Please, post your solution as an answer and then accept it to close the question. That way it will be clear to others what was the problem and what is the solution. Or delete your question if you thing your case is too particular.user31481– user314812017年10月29日 15:28:26 +00:00Commented Oct 29, 2017 at 15:28
-
just use a pin that's high during reset, then you don't need to switch anything.dandavis– dandavis2017年10月29日 23:40:38 +00:00Commented Oct 29, 2017 at 23:40
-
@dandavis can you please be more explicit? Use it in which way?Remi– Remi2017年10月30日 00:15:11 +00:00Commented Oct 30, 2017 at 0:15
-
feed a pin that's high during reset (0,2,3,16) to the input that needs to be high during reset .dandavis– dandavis2017年10月30日 00:18:50 +00:00Commented Oct 30, 2017 at 0:18
1 Answer 1
Just found that I can change pins behavior. Inverting things a little bit, I can get my pins to work as LOW instead of as HIGH.
That way, I only need 1 button to trigger a reset and change pin status. Plus, grounding the reset is done temporarily because of the capacitor, which is what I need.
Here is the new schema
schematic
simulate this circuit – Schematic created using CircuitLab
New code
#include <Spi.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
int buttonPin = D5;
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(BUILTIN_LED, OUTPUT);
int buttonValueAtBoot = digitalRead(buttonPin);
Serial.begin(115200);
Serial.println();
if (buttonValueAtBoot == LOW) {
Serial.println("button was pressed");
digitalWrite(BUILTIN_LED, HIGH);
}
// Avoid problem when the button is hold
while (digitalRead(buttonPin) == LOW) {
// wait before going to deepsleep
// or the capacitor will be drained
delay(1);
}
Serial.println("going to sleep");
ESP.deepSleep(0);
}
void loop() {
}