Go Down

Topic: WHILE problem.. need help! (Read 1 time) previous topic - next topic

DIXIS

May 13, 2010, 03:01 pm Last Edit: May 13, 2010, 03:03 pm by DIXIS Reason: 1
So , I have an  "i" variable , that has to lets say states:  s=0 and s=1...

When (i==1) I need to do

Code: [Select]
if (digitalRead(pPin) == HIGH) digitalWrite(ledPin,HIGH );
if (digitalRead(pPin) == LOW) digitalWrite(ledPin,LOW);


and when (i==0) to do
Code: [Select]
if (digitalRead(pPin) == HIGH) digitalWrite(ledPin,LOW );
if (digitalRead(pPin) == LOW) digitalWrite(ledPin,LOW);


When Im doing this with  WHILE it works only in a half.. the second part with (i==0) is ignored !

Code: [Select]
while(i==1) {
if (digitalRead(pPin) == HIGH) digitalWrite(ledPin,HIGH );
if (digitalRead(pPin) == LOW) digitalWrite(ledPin,LOW);}

while(i==0) {
if (digitalRead(pPin) == HIGH) digitalWrite(ledPin,LOW );
if (digitalRead(pPin) == LOW) digitalWrite(ledPin,LOW);}


PaulS

#1
May 13, 2010, 03:27 pm Last Edit: May 13, 2010, 03:31 pm by PaulS Reason: 1
Skipping, for a moment, what happens inside the body of the while loops, you have this:
Code: [Select]
while(i==1) {}

while(i==0) {}


Where does i ever change value?

In the body of each while loop, you have:
Code: [Select]
if (digitalRead(pPin) == HIGH) digitalWrite(ledPin,HIGH );
if (digitalRead(pPin) == LOW) digitalWrite(ledPin,LOW);


Wouldn't an else be sufficient? The value returned by digitalRead is either HIGH or else it is LOW.

Also, notice that you are setting the LED pin to the same value read from the button pin. You could do this:
Code: [Select]
digitalWrite(ledPin, digitalRead(pPin);

DIXIS

I connected the ps2Keyboard to the board  for ex.  i=1 is when I press button "alt" and i=0 when I press "ctrl" !   i changes the value before the while stuff ...the while's are at the and of the code ...

the body are not the same...  

in i==1

if (digitalRead(pPin) == HIGH) digitalWrite(ledPin,HIGH );
if (digitalRead(pPin) == LOW) digitalWrite(ledPin,LOW);}

and in i==0 I have

if (digitalRead(pPin) == HIGH) digitalWrite(ledPin,LOW );
if (digitalRead(pPin) == LOW) digitalWrite(ledPin,LOW);}

DIXIS

I see the problem in the fact that in the body of while , the value of "i" is without changes, when in the program the i is changing.
Or Im wrong ?

CowJam

The while loop repeats until the condition is not met.  In your example i is not changed within the while loop, so it will repeat forever within one of the two loops.

There is a more elegant way of doing it though.  In your example, ledPin is only set as HIGH if pPin is HIGH and i is 1.  You could use this:
Code: [Select]
if ((i==1)&&(digitalRead(pPin) == HIGH)) {
 digitalWrite(ledPin,HIGH);
} else {
 digitalWrite(ledPin,LOW);
}

PaulS

Quote
I see the problem in the fact that in the body of while , the value of "i" is without changes, when in the program the i is changing.


You are right. Nowhere in the code that you posted does the value of i change.

Whether it changes somewhere else, or not, is a mystery, since you posted so little code.

DIXIS

OK  pPin is a DSC pir sensor ... when is no movement its LOW  when is movement its HIGH  

i==1 is ARMED state ...  when the pPin is HIGH , it lighs the led
when pPin is LOW, the led is turned off...

i==0 is NORMAL status ... whatever pPin state is , the led is turned off

PaulS

Does this mean you solved your problem?

DIXIS

#8
May 14, 2010, 10:25 am Last Edit: May 14, 2010, 10:44 am by DIXIS Reason: 1
no!   the question is , how to change the value of "i"  when "i" is in the body of while ! when the value of "i" is changed in the main loop...

PaulS

You can change the value of i anywhere, by simply using:
Code: [Select]
i=4; // or any new value

Post all of your code if you really want help.

Groove

Quote
when the value of "i" is changed in the main loop


It may be that your code could be simply reconfigured.
Don't forget that "loop" itself is in a "while(1)" loop, so your code may not need a "while" loop at all, just "if" conditionals.

But, as PaulS said, we'd need to see some more
Per Arduino ad Astra

DIXIS

#11
May 14, 2010, 01:32 pm Last Edit: May 14, 2010, 01:33 pm by DIXIS Reason: 1
The whole code :
Code: [Select]
#include <ps2.h>
PS2 kbd(3, 4); //3- clock; 4- DATA
int pPin = 53; //sensor pin
int ledPin = 13;
void kbd_init()
{  char ack;  kbd.write(0xff);   ack = kbd.read();   ack = kbd.read();}
//------------------------------------------------------
void setup()
{   kbd_init();
pinMode(pPin, INPUT);
pinMode(ledPin, OUTPUT);}
//------------------------------------------------------
void loop()
{int i=2;
 unsigned char code;
   for (;;) {    code = kbd.read();  
//--------------------------------
if (code==0x69) {i=1;}
if (code==0x72) {i=0;}
//------------------------------------------------------

switch (i) {
 case 0:    
   digitalWrite(ledPin, LOW);
   break;
 case 1:
while (i!=0){
 digitalWrite(ledPin, digitalRead(pPin));}   //the problem is here ... a can not switch to 0 case, because in the bosy of while, the value of i is not refreshing !  is there something like echo in arduino language
break;

}}


Groove

#12
May 14, 2010, 01:35 pm Last Edit: May 14, 2010, 01:35 pm by GrooveFlotilla Reason: 1
If you made "i" global, and removed the "for (;;)" loop, it looks like you could do what you want.
Per Arduino ad Astra

DIXIS

#13
May 14, 2010, 01:37 pm Last Edit: May 14, 2010, 01:49 pm by DIXIS Reason: 1
Groove ,  I need to use while , because I have two states   when I pres 1 on keyboard (i=1) and 0 on the keyboard(i=0) !  by presing 1, I activate my system, by pressing 0, I diactivate ...

DIXIS

#14
May 14, 2010, 01:40 pm Last Edit: May 14, 2010, 01:46 pm by DIXIS Reason: 1
Groove ,  Ok, I changed ... still not switching !

Code: [Select]
#include <ps2.h>
PS2 kbd(3, 4); //3- clock; 4- DATA
int pPin = 53; //sensor pin
int ledPin = 13;
void kbd_init()
{  char ack;  kbd.write(0xff);   ack = kbd.read();   ack = kbd.read();}
//------------------------------------------------------
void setup()
{   kbd_init();
pinMode(pPin, INPUT);
pinMode(ledPin, OUTPUT);}
//------------------------------------------------------
void loop()
{int i=2;
 unsigned char code;
      code = kbd.read();  
//--------------------------------
if (code==0x69) {i=1;}
if (code==0x72) {i=0;}
//------------------------------------------------------

switch (i) {
 case 0:    
   digitalWrite(ledPin, LOW);
   break;
 case 1:
while (i!=0){
 digitalWrite(ledPin, digitalRead(pPin));}   //the problem is here ... a can not switch to 0 case, because in the bosy of while, the value of i is not refreshing !  is there something like echo in arduino language
break;}

}


Go Up