Chat freely about anything...
User avatar
By komarek
#74582 i am using a 8266-12e on nodeMcu
with a 4x4 keypad to switch my door (in the future)

i was trying my sketch
but the device keeps goin on SOFT RESET while in the WHILE LOOP

i do not really understand why... i tried the same code with an arduino NANO and it worked
actually it is perfectly working on ESP too except that if i wait too much time after a key-press it goes on SOFT RESET.


this is the code:

Code: Select all/*4x4 Matrix Keypad connected to Arduino
This code prints the key pressed on the keypad to the serial port*/

#include <Keypad.h>

const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
int buzz = D8;
String s ="";

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {D7,D6,D5,D4}; //Rows 0 to 3
byte colPins[numCols]= {D3,D2,D1,D0}; //Columns 0 to 3

//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup()
{
pinMode(buzz, OUTPUT);
Serial.begin(9600);
}

//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole keypad scan process
void loop(){
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY)
{
suona();
s = s + keypressed;
Serial.println(s);
int tempo_zero = millis();
press_loop();
}
}

void press_loop(){
int n = 0;
int tempo_zero = millis();
int tempo = millis();

while (n<5){
  Serial.println("WHILE");
  char k = myKeypad.getKey();
  if (k != NO_KEY){
    s = s + k;
    Serial.println(s);
    suona();
    n = n + 1;
  }
  tempo = millis();
  if (tempo-tempo_zero>20000) break;
}

delay(350);
if (s == "00000#") suono_on();
else suono_off();
s = "";
Serial.println("");

}



void suona() {
  tone(buzz, 1400); // Send 1KHz sound signal...
  delay(100);        // ...for 1 sec
  noTone(buzz);
}

void suono_off() {
  tone(buzz, 1000); // Send 1KHz sound signal...
  delay(200);        // ...for 1 sec
  noTone(buzz);
  delay(50);
  tone(buzz, 200);
  delay(400);
  noTone(buzz);
}

void suono_on() {
  tone(buzz, 1500);
  delay(200);
  noTone(buzz);
  delay(50);
  tone(buzz, 1500);
  delay(200);
  noTone(buzz);
  delay(50);
  tone(buzz, 1500);
  delay(200);
  noTone(buzz);
}



and this is where the device RESET:

Code: Select allwhile (n<5){
  Serial.println("WHILE");
  char k = myKeypad.getKey();
  if (k != NO_KEY){
    s = s + k;
    Serial.println(s);
    suona();
    n = n + 1;
  }
  tempo = millis();
  if (tempo-tempo_zero>20000) break;
}


any idea?
Last edited by komarek on Wed Mar 14, 2018 6:32 am, edited 1 time in total.