Hi all,
I would like to interface my board with 1 push button.
Starting from initial status = 0; if i press it once i want to see the value 1, two times the value 2 and so on, when i reach the maximum value, it has to restart from 0.
i would also like to write this code so that i can add more push buttons later without rewriting many lines for each PB, so i would like to make an object that i can call in just one line.
So i wrote this:
//===PB Latching===//
int PB0as = 0; // Actual Status
int PB0ns = 0; // New Status
int PB0os = 1; // Old Status
int PB0sq = 5; // StatusQty, 2=on/off; 3=a,b,c etc.
int prevPB0as = 0;
const int PB0Pin = 4; // On nano
// const int PB0Pin = D4; //On Wemos D1 mini
void setup() {
delay(2000);
Serial.begin(115200);
pinMode(PB0Pin, INPUT_PULLUP);
Serial.println(">>>Setup complete<<<");
}
void loop() {
PB0ns= !digitalRead(PB0Pin);
PBoperation (PB0as, PB0ns, PB0os, PB0sq );
if (PB0as!=prevPB0as) {
Serial.println(PB0as);
prevPB0as = PB0as;
}
}
//////////========== Push Button Latching ==========//////////
int PBoperation(int &Status, int &New, int &Old, int &StatusQty) {
if (Old==0 && New ==1) { // Rising front
Status++;
if (Status >= StatusQty) Status = 0;
}
Old=New;
}
This code works very well on nano, without any problem, but on my Wemos D1 mini it has a very strange behaviour, it resets all the time and on the serial this appears:
>>>Setup complete<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
Exception (0):
epc1=0x40203faa epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000
>>>stack>>>
ctx: cont
sp: 3ffffe00 end: 3fffffc0 offset: 0190
3fffff90: 3fffdad0 00000000 3ffee500 40201058
3fffffa0: feefeffe feefeffe 3ffee554 402018e8
3fffffb0: feefeffe feefeffe 3ffe85e0 40100b95
<<<stack<<<
--------------- CUT HERE FOR EXCEPTION DECODER ---------------
ets Jan 8 2013,rst cause:2, boot mode:(3,7)
load 0x4010f000, len 3460, room 16
tail 4
chksum 0xcc
load 0x3fff20b8, len 40, room 4
tail 4
chksum 0xc9
csum 0xc9
v00041c90
~ld
Does anyone have any idea why and a solution to this problem?
I'm not an advanced programmer and don't really understand pointer mechanics, I think the problem is in the "&" in front of the variable in PBoperation, what do you think?