Pages: 1 [2]   Go Down
Author Topic: While loop & Reset  (Read 1841 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
... I will print a message (dispensing 20 ul) and the pin start turning high and low (see my time) until the user touches the keys again ...

I'm sorry but I'm not just going to sit here and write your code. You have explained what you want, but your code doesn't reflect that.

Code:
while(1)
    {
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }

That says, turn the pump on, wait 1 second, turn it off, wait 5 seconds, and then keep doing that until you power off the Arduino.

If you just want to run for one second, get rid of the loop, eg.

Code:
   digitalWrite(IOXpin, LOW);
   delay(1000);
   digitalWrite(IOXpin, HIGH);
   delay(5000);
   

If you want to run until they press a key, well test for that:

Code:
while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
          break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }

You don't need to reset the system. Try to design so you don't have those loops that you never leave.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I should point out though that this code will only check the key every 6 seconds. You may want to eliminate the delay altogether and just check for elapsed time.
Logged

83 - var
Offline Offline
God Member
*****
Karma: 9
Posts: 863
ARDUINO Powa !
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yop !

Code:
unsigned long time1=0,time2=0;
int xx=1;

while (1)
{
     

      keypad.getKey ();
      if (keypad.getState () == presse)
          break; // quitter la boucle

      switch (xx)
      {
       
      case 1 :
     
            digitalWrite (IOXpin, LOW);
            time2=millis();
            if (time2>time1+1000)//1000 = 1sec
            {
                xx=2;
                time1=millis();
            }
            break;
           
      case 2 :     

            digitalWrite (IOXpin, HIGH);
            time2=millis();
            if (time2>time1+5000)//1000 = 1sec
            {
                xx=1;
                time1=millis();
            }
            break;
    }
}

it's good for your problem?? =)

Skizo !
Logged

Un tien vaux mieux que deux tu l'auras !

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much for your help.
I am working now on my void loop() function. In my code (see bellow) I have to press "#" to dispense the 200 and "*" to dispense the 20.
How can I use just one * or # to do the same thing because I am trying to increase the number of combination to 10 and it's gonna be a mess to have a case for each one.
So here is my code so far:
Code:
#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();
}

//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();
      break;
    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)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //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)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
and here is the print screen of what I get when I run it:
Code:
Pressed: 2
Pressed: 0
Pressed: *
Dispensing 20 uL
Pressed: 2
Pressed: 0
Pressed: #
Not Available, Reset!
Pressed: 2
Pressed: 0
Pressed: 0
Pressed: #
Not Available, Reset!
Pressed: 2
Pressed: 0
Pressed: 0
Pressed: #
Not Available, Reset!
Pressed: 1
Pressed: 2
Pressed: 0
Pressed: 0
Pressed: #
Dispensing 200 uL
Pressed: 2
Pressed: 0
Pressed: *
Not Available, Reset!
Pressed: 2
Pressed: 0
Pressed: 0
Pressed: #
Not Available, Reset!
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

now here is what I am trying to actually do:
Code:
#include <Keypad.h>
#include <Password.h>
int IOXpin =2;
Password Disp1 = Password( "20" );
Password Disp2 = Password( "40" );
Password Disp3 = Password( "60" );
Password Disp4 = Password( "80" );
Password Disp5 = Password( "100" );
Password Disp6 = Password( "120" );
Password Disp7 = Password( "140" );
Password Disp8 = Password( "160" );
Password Disp9 = Password( "180" );
Password Disp10 = 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();
}

//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();
      checkDisp2();
      checkDisp3();
      checkDisp4();
      checkDisp5();
      checkDisp6();
      checkDisp7();
      checkDisp8();
      checkDisp9();
      checkDisp10();
      break;
    case '1':
      Disp1.reset();
      Disp2.reset();
      Disp3.reset();
      Disp4.reset();
      Disp5.reset();
      Disp6.reset();
      Disp7.reset();
      Disp8.reset();
      Disp9.reset();
      Disp10.reset();
      break;
    default:
      Disp1.append(eKey);
      Disp2.append(eKey);
      Disp3.append(eKey);
      Disp4.append(eKey);
      Disp5.append(eKey);
      Disp6.append(eKey);
      Disp7.append(eKey);
      Disp8.append(eKey);
      Disp9.append(eKey);
      Disp10.append(eKey);
     
    }
  }
}

void checkDisp1(){
  if (Disp1.evaluate()){
    Serial.println("Dispensing 20 uL");
    //Add code to run if it works
    while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
void checkDisp2(){
  if (Disp2.evaluate()){
    Serial.println("Dispensing 40 uL");
    //Add code to run if it works
    while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
void checkDisp3(){
  if (Disp2.evaluate()){
    Serial.println("Dispensing 60 uL");
    //Add code to run if it works
    while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
void checkDisp4(){
  if (Disp2.evaluate()){
    Serial.println("Dispensing 80 uL");
    //Add code to run if it works
    while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
void checkDisp5(){
  if (Disp2.evaluate()){
    Serial.println("Dispensing 100 uL");
    //Add code to run if it works
    while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
void checkDisp6(){
  if (Disp2.evaluate()){
    Serial.println("Dispensing 120 uL");
    //Add code to run if it works
    while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
void checkDisp7(){
  if (Disp2.evaluate()){
    Serial.println("Dispensing 140 uL");
    //Add code to run if it works
    while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
void checkDisp8(){
  if (Disp2.evaluate()){
    Serial.println("Dispensing 160 uL");
    //Add code to run if it works
    while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
void checkDisp9(){
  if (Disp2.evaluate()){
    Serial.println("Dispensing 180 uL");
    //Add code to run if it works
    while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }
}
void checkDisp10(){
  if (Disp2.evaluate()){
    Serial.println("Dispensing 200 uL");
    //Add code to run if it works
    while(1)
    {
      keypad.getKey ();
      if (keypad.getState() == PRESSED)
        break;  // leave the loop
      digitalWrite(IOXpin, LOW);
      delay(1000);
      digitalWrite(IOXpin, HIGH);
      delay(5000);
    }
  }
  else{
    Serial.println("Not Available, Reset!");
    //add code to run if it did not work
    digitalWrite(IOXpin, HIGH);
  }   
}

And here is the problems when I use one case for a bunch of functions.
Code:
Pressed: 2
Pressed: 0
Pressed: *
Dispensing 20 uL
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Pressed: 2
Pressed: 0
Pressed: 0
Pressed: *
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Pressed: 1
Pressed: 2
Pressed: 0
Pressed: 0
Pressed: *
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Pressed: 1
Pressed: 1
Pressed: 2
Pressed: 0
Pressed: 0
Pressed: *
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Pressed: 4
Pressed: 0
Pressed: *
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Not Available, Reset!
Pressed: 1
Pressed: 4
Pressed: 0
Pressed: *
Not Available, Reset!
Dispensing 40 uL
Dispensing 60 uL
Dispensing 80 uL
Dispensing 100 uL
Dispensing 120 uL
Dispensing 140 uL
I am getting a lot of prints that I don't need. Another thing is when I pressed 4 and 0 and * to validate I got what I have in the print screen and the key don't print anything after this. Also the Disp2() function in my code does not work.
I know I asked a lot of questions but hopefully this will be my last question. I am working on a project and it's due soon. Thanks
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This isn't really a password is it? I'd throw the password library out.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes you are right but that's how much brain I got. Do you have another way of using a keypad to actually get something useful and use it to do what I talked about. If not I hope that You can just help fix what I have right now and after the project I will work on it myself to improve it.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was looking what's available online. How to use a keypad and I mostly found how to use for a password, so I used it to do two things but now they want in between in increment of 20 so I have to do 10 setting.
And I just can go back and look for other solutions. especially I am new to this things.
Thanks anyway for your feedbacks
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's what I am talking about:

Code:
#include <Keypad.h>

const byte IOXpin = 2;

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

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup()
{
  pinMode(IOXpin, OUTPUT);
  Serial.begin (9600);
}  // end of setup

void processNumber (const unsigned long amount)
  {
  Serial.print ("Dispensing ");
  Serial.print (amount);
  Serial.println (" ml.");

  // Sanity Claus
 
  if (amount < 5 || amount > 500)
    {
    Serial.println ("Invalid amount ... aborting.");
    return;   
    }
   
  unsigned long startTime = millis ();
  unsigned long runTime = amount * 100;   // assume 0.1 secs per ml
 
  digitalWrite (IOXpin, LOW);
  while (millis () - startTime < runTime)
    {
    if (kpd.getKey() == '*')  // cancel button
       {
       Serial.println ("Cancelled.");
       break; 
       }  // end of if cancelled
    }  // end of waiting for time to be up
  digitalWrite (IOXpin, HIGH);

  Serial.println ("Done.");
 
  }  // end of processNumber

void handleKeypress (const char key)
  {
  Serial.print ("Got: ");
  Serial.println (key);

  static unsigned long receivedNumber = 0;
 
  switch (key)
    {
    case '#': 
      processNumber (receivedNumber);
      receivedNumber = 0;
      break;
     
    case '0' ... '9':
      receivedNumber *= 10;
      receivedNumber += key - '0';
      break;

    default:
      Serial.println ("Unexpected input!");
      break;
     
    } // end of switch 
   
  }  // end of handleKeypress

void loop()
{
  byte key = kpd.getKey();
  if (key)
    handleKeypress (key);
}

Don't need passwords. Just a simple keypad handler. Send the keys off to a state machine. That handles numbers. Then the "#" "commits" the number and it starts dispensing. You use the "*" to abort.

Now this might not do everything you want, but it shows the general idea.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Merci beaucoup mon ami pour votre aid. I don't know how to thank you. I am new to this and you just saved me the headache. I am trying to learn but given the time I have left and what I still have to do. I realized it's not worth it to spend more time to think about what each function does in the code. But Arduino is definitley fascinating. I need to look into it.
Thank you very much.
Logged

Pages: 1 [2]   Go Up
Jump to: