Pages: [1] 2   Go Down
Author Topic: WHILE problem.. need help!  (Read 1549 times)
0 Members and 1 Guest are viewing this topic.
Chisinau, Moldova
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks =)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (digitalRead(pPin) == HIGH) digitalWrite(ledPin,HIGH );
if (digitalRead(pPin) == LOW) digitalWrite(ledPin,LOW);

and when (i==0) to do
Code:
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:
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);}
« Last Edit: May 13, 2010, 08:03:05 am by DIXIS » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 48993
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Skipping, for a moment, what happens inside the body of the while loops, you have this:
Code:
while(i==1) {}

while(i==0) {}

Where does i ever change value?

In the body of each while loop, you have:
Code:
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:
digitalWrite(ledPin, digitalRead(pPin);
« Last Edit: May 13, 2010, 08:31:19 am by PaulS » Logged

Chisinau, Moldova
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks =)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Chisinau, Moldova
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks =)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Wigan, UK
Offline Offline
God Member
*****
Karma: 19
Posts: 814
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if ((i==1)&&(digitalRead(pPin) == HIGH)) {
  digitalWrite(ledPin,HIGH);
} else {
  digitalWrite(ledPin,LOW);
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 48993
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Chisinau, Moldova
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks =)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 48993
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this mean you solved your problem?
Logged

Chisinau, Moldova
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks =)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
« Last Edit: May 14, 2010, 03:44:45 am by DIXIS » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 48993
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Post all of your code if you really want help.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Per Arduino ad Astra

Chisinau, Moldova
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks =)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The whole code :
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;

}}
« Last Edit: May 14, 2010, 06:33:56 am by DIXIS » Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you made "i" global, and removed the "for (;smiley-wink" loop, it looks like you could do what you want.
« Last Edit: May 14, 2010, 06:35:21 am by GrooveFlotilla » Logged

Per Arduino ad Astra

Chisinau, Moldova
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks =)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ...
« Last Edit: May 14, 2010, 06:49:02 am by DIXIS » Logged

Chisinau, Moldova
Offline Offline
Newbie
*
Karma: 0
Posts: 22
Arduino rocks =)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Groove ,  Ok, I changed ... still not switching !

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

}

« Last Edit: May 14, 2010, 06:46:05 am by DIXIS » Logged

Pages: [1] 2   Go Up
Jump to: