Pages: 1 [2]   Go Down
Author Topic: WHILE problem.. need help!  (Read 1589 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
//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]
« Last Edit: May 14, 2010, 06:50:30 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

I can use  and the original stuff like

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

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

when i do stuff like
Code:
if (i==1) digitalWrite(ledPin, HIGH);    
if (i==0) digitalWrite(ledPin, LOW);
it works !

but with while  no ...
Logged

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

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.
« Last Edit: May 14, 2010, 07:16:33 am by PaulS » Logged

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

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

look people , I can do :
Code:
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 ?
Logged

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

The while statement is used for two purposes. The first is to create an infinite loop.

Code:
while(1)
{
   // Do something
}

The second is to create a loop that repeats for an unknown number of times.

Code:
unsigned long interval = 5000;
unsigned long then = millis();
while((millis() - then) > interval)
{
   // Do something
}

In the first case, the loop will never terminate. In the second case, the loop will terminate, but we have no idea how many times it will be executed, because the length of time it takes to execute the loop is unknown.

Another indeterminate example:
Code:
char inByte = 'A';
while(Serial.available() && inByte != '\n')
{
   inByte = Serial.read();
}
This loop will terminate when there is no more data to read, or when the data read was a \n.

In the two cases that terminate, the while clause depends on something that changes.

In your case, you never change the value of i in the body of the loop or in the while clause itself, so the loops you have set up are infinite loops.

Quote
look people , i know my code is fine !

Look, DIXIS, it isn't.
Logged

Pages: 1 [2]   Go Up
Jump to: