Go Down

Topic: skipping code after adding "while" command (Read 180 times) previous topic - next topic

tony456

hi,

I have botched together all kinds of code in the hope of making a RFID reader that turns on either of two Relays.

There is a LCD display, which displays the time and and shows the option to choose one of two buttons after the valid RFID card has been scanned .
This turns on one or the other relay for a set amount of time.


everything worked fine (kinda) until I added the "while"command to wait for the button input..

now after scanning the correct card --coded into the program-- the arduino skips back to the "normalmessage" without waiting for the button input. (and doing what is programmed after)
if I want the the if command to work I have to press the button before I scan the RFID card.

If anyone has any tips, it would be much appreciated..

thanks,
Tony


it starts going wrong here;

Code: [Select]
  content.toUpperCase();
 if (content.substring(1) == "04 70 D1 BA BA 5D 81" ||
     content.substring(1) =="04 B5 43 E2 BA 5D 80"  ||
     content.substring(1) =="04 1C 67 BA BA 5D 81" )
   {lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("TEXT");
    delay(1500);
   lcd.clear();
   lcd.setCursor(0,2);
   lcd.print("MORE TEXT');
 
while(digitalRead(buttonPin4)==LOW&& digitalRead(buttonPin5)==LOW){ }
 
  if (buttonState4 == HIGH) {
    digitalWrite(relais6, HIGH);
    digitalWrite(relais7, LOW);
    lcd.clear();
    lcd.setCursor(5,1);
    lcd.print("TEXT");
    delay(5400000UL);//wait for 90 minutes 
    digitalWrite(relais6,LOW);}
   
    //else {digitalWrite(relais6,LOW);
    //printNormalModeMessage();}
 
  if (buttonState5 == HIGH) {
    digitalWrite(relais7, HIGH);
    digitalWrite(relais6, LOW);
    lcd.clear();
    lcd.setCursor(5,1);
    lcd.print("TEXT");
     delay(5400000UL);//wait for 90 minutes
    digitalWrite(relais7,LOW);}
    else {digitalWrite(relais7,LOW[/color]);}
    printNormalModeMessage();
    }


the entire code:

Code: [Select]
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#define DS3231_I2C_ADDRESS 0x68
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN);   // Create MFRC522 instance.
//button
const int buttonPin4 = 4;//wassen
const int buttonPin5 = 5;//drogen
const int relais6 = 6; //relais pin d6
const int relais7 = 7; //relais pin d7
int buttonState4 = 0;
int buttonState5 = 0;   

//clock
byte decToBcd(byte val)
{ return( (val/10*16) + (val%10) );} // Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{ return( (val/16*10) + (val%16) );}


void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}

void readDS3231time(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,
byte *year)
{ Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
 
void printNormalModeMessage() {
  lcd.clear(); 
  lcd.setCursor ( 2, 0 ); // Prints the initial message
  lcd.print("TEXT");
  lcd.setCursor(3, 1);
  lcd.print("Scan Card");
  displayTime();
  }

void displayTime()
{ byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour,&dayOfWeek, &dayOfMonth, &month, &year);
   
  if(hour<10){
  lcd.setCursor(1,3);
  lcd.print("0");
  lcd.setCursor(2,3);
  lcd.print(hour);}
  else{
  lcd.setCursor(1,3);
  lcd.print(hour);}
  lcd.setCursor(3,3);
  lcd.print(":");
   if (minute<10){
   lcd.setCursor(4,3);
   lcd.print("0");
   lcd.setCursor(5,3);
   lcd.print(minute);}
   else{
   lcd.setCursor(4,3);
   lcd.print(minute);}//end of time display
   //start date display
    if(dayOfMonth<10){  //date
    lcd.setCursor(9,3);
    lcd.print("0");
    lcd.setCursor(10,3);
    lcd.print(dayOfMonth);}
    else{lcd.setCursor(9,3);
    lcd.print(dayOfMonth);}
    lcd.setCursor(11,3);
    lcd.print("/");
     if (month<10){      //month
     lcd.setCursor(12,3);
     lcd.print("0");
     lcd.print(month);}
     else{
     lcd.setCursor(12,3);
     lcd.print(month);}
  lcd.setCursor(14,3);
  lcd.print("/");
  lcd.setCursor(15,3);
  lcd.print("20");
  lcd.setCursor(17,3);
  lcd.print(year);
  }
 
void setup()

{
  Wire.begin();
  Serial.begin(9600);
   lcd.backlight();
   lcd.init(); 
   SPI.begin();      // Initiate  SPI bus
  mfrc522.PCD_Init();   // Initiate MFRC522
     printNormalModeMessage();
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
 setDS3231time(30,0,0,3,1,5,19);
  pinMode(relais6, OUTPUT); //button pin4
  pinMode(relais7, OUTPUT); //button pin5

  pinMode(buttonPin4, INPUT);//relay pin6
  pinMode(buttonPin5, INPUT); //relay pin7
}



 
 
void loop() {
  buttonState4 = digitalRead(buttonPin4);
  buttonState5 = digitalRead(buttonPin5);
  if ( ! mfrc522.PICC_IsNewCardPresent()) // Look for new cards
  { return; }
  if ( ! mfrc522.PICC_ReadCardSerial())  // Select one of the cards
  { return; }
 
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++)
  { 
     //lcd.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     //lcd.print(mfrc522.uid.uidByte[i], HEX);
     //delay(1000);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }

 
  content.toUpperCase();
 if (content.substring(1) == "04 70 D1 BA BA 5D 81" ||
     content.substring(1) =="04 B5 43 E2 BA 5D 80"  ||
     content.substring(1) =="04 1C 67 BA BA 5D 81" )
   {lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("TEXT");
    delay(1500);
   lcd.clear();
   lcd.setCursor(0,2);
   lcd.print("MORE TEXT');   

while(digitalRead(buttonPin4)==LOW&& digitalRead(buttonPin5)==LOW){ }
 
  if (buttonState4 == HIGH) {
    digitalWrite(relais6, HIGH);
    digitalWrite(relais7, LOW);
    lcd.clear();
    lcd.setCursor(5,1);
    lcd.print("TEXT");
    delay(5400000UL);//wait for 90 minutes 
    digitalWrite(relais6,LOW);}
   
    else {digitalWrite(relais6,LOW);
    printNormalModeMessage();}
 
  if (buttonState5 == HIGH) {
    digitalWrite(relais7, HIGH);
    digitalWrite(relais6, LOW);
    lcd.clear();
    lcd.setCursor(5,1);
    lcd.print("TEXT");
     delay(5400000UL);//wait for 90 minutes
    digitalWrite(relais7,LOW);}
    else {digitalWrite(relais7,LOW);}
    printNormalModeMessage();
    }
   
   
 else
     {lcd.clear();
     lcd.setCursor(2,1);
     lcd.print("invalid card");
     delay(1500);
     printNormalModeMessage();
     }
}

J-M-L

#1
Jun 19, 2019, 06:14 pm Last Edit: Jun 19, 2019, 06:17 pm by J-M-L
assuming you changed your text and made a mistake pasting (you have a ' instead of a " in the full code)

You need to read the status (and wait) for one button to go down at that point - not at the beginning of the loop . Careful about bouncing possibly depending on what you want to do


Note that you could use memcmp() to compare the array mfrc522.uid.uidByte with const arrays with your bytes rather than building a costly String every time...
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

PaulS

Code: [Select]
while(digitalRead(buttonPin4)==LOW&& digitalRead(buttonPin5)==LOW){ }
 
  if (buttonState4 == HIGH) {

The while statement does not assign a value to buttonState4. If you want to know the state of switch (not a f**king button), you need to read the state of the pin again.
The art of getting good answers lies in asking good questions.

Gates

#3
Jun 20, 2019, 11:11 am Last Edit: Jun 20, 2019, 11:15 am by Gates
Code: [Select]
var = 0;
while(var < 200){
  // do something repetitive 200 times
  var++;
  // do something repetitive 200 times, now time to escape while loop  
}  


Maybe your pins are floating in an unknown state
Code: [Select]
  // connect OUTPUT pins to other devices with 470Ω or 1k resistors,
  pinMode (trigger_output[0, 1],OUTPUT);
// Often it is useful to steer an input pin to a known state if no input is present.
// This can be done by adding a pullup resistor (to +5V), or a pulldown resistor
//(resistor to ground) on the input. A 10K resistor is a good value for a pullup or pulldown resistor. 
  pinMode (hold_input[0, 1],INPUT); 

Gates

Code: [Select]
while(digitalRead(buttonPin4)==LOW&& digitalRead(buttonPin5)==LOW){ }
 
  if (buttonState4 == HIGH) {

The while statement does not assign a value to buttonState4. If you want to know the state of switch (not a f**king button), you need to read the state of the pin again.
No need to use language like that.

christop

No need to use language like that.

Besides that, a button is something you can press to operate a device or a machine, despite PaulS's insistence otherwise:

Quote
button noun [ C ] (OBJECT YOU PRESS)

a small object that you press to operate a device or a machine

J-M-L

#6
Jun 21, 2019, 07:01 am Last Edit: Jun 22, 2019, 04:41 am by J-M-L
Terminology and communication...

My take on this:

One can see a button as the physical user interface part that actuates an electrical switch (it's also "a small disc or knob sewn on to a garment, either to fasten it by being pushed through a slit made for the purpose or for decoration".)

In your context The button could be a sliding knob, or a non latching element in which case it could be argued that the state of the switch is directly reflecting the position (state) of the button (after bouncing is gone) But some button could be latching, like press once and you are on and press again and you are off, so you can't infer the button position (state) from reading the switch state (or whilst bouncing). Hence it's more precise to discuss switch state than button state.

Using ** big words is a way to get that message across and is not a  personal attack, just a way to express once frustration for precision. I would not read too much into this

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

tony456

hi,
the quotation mark in the code is a typo,
and I do have a 1K pull down resistor on both buttonpins.

should the value be higher?
moving the "while" command forward created issues with the last "else" command (which refers to an invalid card)


Go Up