Go Down

Topic: While loop & Reset (Read 2 times) previous topic - next topic

ferkheusa

Hello,
   I am trying to do a Reset after the user enters his numbers. I am also trying to fix my while loops because once in the loop it just stays there and if the user tries the other combination it wouldn't allow him because of the while loop.
I know the problem is because of the condition I have inside my while loop (i.e the 1) but I don't know what condition to put inside it so when the user start pressing the keys the program will reset the previous input and let the user put the new combination.
The program works but I have to reset using the reset button of the arduino. I want some reset in the code.
Thank you very very much. Here is my code:
Code: [Select]
#include <Keypad.h>
#include <Password.h>
int IOXpin =2;
Password Disp1 = Password( "20" );
Password Disp2 = Password( "200" );

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {
    '1','2','3'                      }
  ,
  {
    '4','5','6'                      }
  ,
  {
    '7','8','9'                      }
  ,
  {
    '*','0','#'                      }
};

byte rowPins[ROWS] = {
  5, 4, 3, 12}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
  pinMode(IOXpin, OUTPUT);
  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop(){
  digitalWrite(IOXpin, HIGH);
  keypad.getKey();
  char key = keypad.getKey();
  Serial.println(key);
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
  case PRESSED:
    Serial.print("Pressed: ");
    Serial.println(eKey);
    switch (eKey){
    case '*':
      checkDisp1();
      break;
    case '#':
      checkDisp2();
    case '1':
      Disp1.reset();
      Disp2.reset();
      break;
    default:
      Disp1.append(eKey);
      Disp2.append(eKey);
    }
  }
}

void checkDisp1(){
  if (Disp1.evaluate()){
    Serial.println("Dispensing 20 uL");
    //Add code to run if it works
    while(1)
    {
      digitalWrite(IOXpin, LOW);
      delay(100);
      digitalWrite(IOXpin, HIGH);
      delay(100);
    }
  }
  else{
    Serial.println("Not Available!! Reset using 1");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
void checkDisp2(){
  if (Disp2.evaluate()){
    Serial.println("Dispensing 200 uL");
    //Add code to run if it works
    while(1)
    {
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(1000);
    }
  }
  else{
    Serial.println("Not Available!! Reset using 1");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}




kf2qd

Is it time dependent? Use millis() and check for a time out and then reset the values.

Configure it so the user hits some key to start the entry sequence. Like a "command" key

ferkheusa

No it's not time dependent. I just want to sense when the user start touching the keys and that should trigger something to reset the previous entry and leave the loop. Turn pin 2 high and then it's ready to check the new entry.if it's correct it will enter the second loop if not it'll go to the second condition(not available!!)
Thanks

PaulS

Code: [Select]
  keypad.getKey();
  char key = keypad.getKey();
  Serial.println(key);

Read the keypad, and throw the value away. Read it again, and print the key being pressed, even if no key is being pressed. Why?

Code: [Select]
    while(1)
    {
      digitalWrite(IOXpin, LOW);
      delay(100);
      digitalWrite(IOXpin, HIGH);
      delay(100);
    }

If this statement is ever reached, the infinite loop is the only thing that the code will ever do. Not the greatest idea to not include a way to get out of this loop. Fortunately, the Arduino people did provide a way - the reset button.


ferkheusa

I appreciate the response, but what you trying to say with you answer. I have a problem with loop how can we solve it?

Go Up