WHILE problem.. need help!

So , I have an "i" variable , that has to lets say states: s=0 and s=1...

When (i==1) I need to do

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

and when (i==0) to do

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 !

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);}

Skipping, for a moment, what happens inside the body of the while loops, you have this:

while(i==1) {}

while(i==0) {}

Where does i ever change value?

In the body of each while loop, you have:

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:

digitalWrite(ledPin, digitalRead(pPin);

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);}

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 ?

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:

if ((i==1)&&(digitalRead(pPin) == HIGH)) { 
  digitalWrite(ledPin,HIGH);
} else {
  digitalWrite(ledPin,LOW);
}

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.

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

Does this mean you solved your problem?

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...

You can change the value of i anywhere, by simply using:

i=4; // or any new value

Post all of your code if you really want help.

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

The whole code :

#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;

}}

If you made "i" global, and removed the "for (;;)" loop, it looks like you could do what you want.

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 ...

Groove , Ok, I changed … still not switching !

#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;}

}
//the problem is [u]not[/u] here

[edit]Is there some significance about translating 0x69 to 1, and 0x72 to 0? You're using a "switch" - why not use the original values?[/edit]

I can use and the original stuff like

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

its not the point. the problem i think is in while ... the value of i is not changed in while.

when i do stuff like

if (i==1) digitalWrite(ledPin, HIGH);     
if (i==0) digitalWrite(ledPin, LOW);

it works !

but with while no ...

The variable i is assigned a value based on the value returned by kbd.read(). If you never call that function again, and never re-assign i a new value, I'm at a loss to understand why you expect the value of i to change.

You are not binding i to the value output by kdb.read(), so that a change in which key is pressed automatically assigns a new value to i.

Perhaps you are misunderstanding the purpose of while, using it where you mean if. The while statement is used to do something over and over, until the termination condition is true. The if statement is used to do something once if the condition is true.

look people , i know my code is fine ! I need only the corect sintax !

look people , I can do :

If (i==1) digitalWrite(ledPin, digitalRead(pPin));
if (i==0) digitalWrite(ledPin, LOW);

but I need while (i==1) to do digitalWrite(ledPin, digitalRead(pPin)); and while (i==0) to do digitalWrite(ledPin, LOW);

the value of i is changed before this stuff ! what is the right sintax ?