while - problem

Hello,

I do write

while(relaypinstatus == LOW)
        {
          relaypinstatus = digitalRead(relaypin);
        };

and the relaypin is always low.

Now do I expect that the program is endlessly running in the loop without any way out.

But what I see is a running program as if the while instruction would't be there. By the way runs the program also if the relaypin is always high.

I read my Arduino book again and again and did'nt find any failure in my while instruction. I did write it exactly so like written in the book.

Where is the failure here?

What is connected to the pin you are read? A complete schematic would be in order.

There is a 10k resistor to the 5 V pin on the arduino board. Additonally is and old 1 k resistor and a switch. If the switch is closed then is the input on high level. But I see, if the switch is open then is the port indefinite. It has to be changed in the final construction

But the while problem is in each switch position. In one of that positions is all correct, there should it work. VG mgbd

So

relaypinstatus == LOW

is clearly NOT true when the while STATEMENT is reached!

POST ALL YOUR CODE!

Mark

mgbd: There is a 10k resistor to the 5 V pin on the arduino board. Additonally is and old 1 k resistor and a switch. If the switch is closed then is the input on high level. But I see, if the switch is open then is the port indefinite. It has to be changed in the final construction

But the while problem is in each switch position. In one of that positions is all correct, there should it work. VG mgbd

10k from where to where? switch from where to where? Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? Thanks.. Tom... :)

relaypinstatus = digitalRead(relaypin);   // Forgot to initialize
while(relaypinstatus == LOW)
        {
          relaypinstatus = digitalRead(relaypin);
        };

jimLee: relaypinstatus = digitalRead(relaypin);   // Forgot to initialize while(relaypinstatus == LOW)        {          relaypinstatus = digitalRead(relaypin);        };

Another way to write it:

    do {
        relaypinstatus = digitalRead(relaypin);
    } while (relaypinstatus == LOW);

or even:

while (digitalRead(relaypin) == LOW);
relaypinstatus = HIGH;

picky picky picky :)

-jim lee

Hi holmes4

here is the (shorted) code, I hope you can identfy my failure:

// while-Problem-2                             4.12.16


#include <EEPROM.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

int eepromaddress = 0;                 // <---------   XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
byte eepromwert;

LiquidCrystal_I2C lcd(0x20,16,2);      // Achtung: auf den neuen I2C-LCD gibt es Adressjumper.
                                       // Alle gesetzt, gibt I2C-Adresse 0x27  (ox20?),
                                        
#define LEDpin 13
int sensorpin1 = 8;                    // Vorlauf   Anschluß des SMT160-30
int sensorpin2 = 12;                   // außen
#define relaypin 9                     // Heizungsrelais
 

int cyclen = 2500;                     // wie oft wird ein Sensor abgefragt, bis daraus ein Mittelwert gebildet wird
float high, low;                       // float -3.4 E38 bis +3.8 E38 (4 Byte)
float High, Low;
float Temp, Temp1 ,Temp2;
int i;                                 // +/- 32768 (2byte)
float a;                               // wird für die Ziffernausgabe benötigt
int eepromintervall = 1;             // nach wieviel LCD-Ausgaben wird ein Wert ins EEPROM geschrieben  <-------  XXXXXXXXXXXXXXXXX
byte highbyte, lowbyte;
int relaypinstatus = 1;
int letzterrelaypinstatus = 0;
int ausschaltzaehler = 0;

void setup() {
  // put your setup code here, to run once:

}

void loop() {
// |||||||| ... Hier startet der Versuch der Relaisabfrage ||||||||||||||||||||||||||||||||||||||||  

        while(relaypinstatus == LOW)
        {
         relaypinstatus = digitalRead(relaypin);
        }; 
        
    lcd.println(relaypinstatus);
}

//....snip  (many instructions)


       while(relaypinstatus == HIGH)
        {
          relaypinstatus = digitalRead(relaypin);
        };

        
        

  
  }       // End of loop XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The problem is just what we said it was!

Mark

Um ... Your shortened code as posted above does not compile, and nobody knows how you have it wired.

Corrected sketch. It always displays ‘1’. Did you expect any different?

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x20, 16, 2);    // Achtung: auf den neuen I2C-LCD gibt es Adressjumper.
// Alle gesetzt, gibt I2C-Adresse 0x27  (ox20?),

#define relaypin 9                     // Heizungsrelais
int relaypinstatus = 1;

void setup() {
  // put your setup code here, to run once:
  pinMode(relaypin, INPUT_PULLUP); // or INPUT depending on wiring
}

void loop() {
  // |||||||| ... Hier startet der Versuch der Relaisabfrage ||||||||||||||||||||||||||||||||||||||||

  while (relaypinstatus == LOW) {
    relaypinstatus = digitalRead(relaypin);
  };
  // HERE relaypinstatus IS ALWAYS 1

  lcd.println(relaypinstatus);

  //....snip  (many instructions)

  while (relaypinstatus == HIGH) {
    relaypinstatus = digitalRead(relaypin);
  };
  // HERE relaypinstatus IS ALWAYS 0
}       // End of loop XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX