Go Down

Topic: WHILE problem.. need help! (Read 2787 times) previous topic - next topic


May 14, 2010, 01:47 pm Last Edit: May 14, 2010, 01:50 pm by GrooveFlotilla Reason: 1
Code: [Select]
//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]
Per Arduino ad Astra


I can use  and the original stuff like

Code: [Select]
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
Code: [Select]
if (i==1) digitalWrite(ledPin, HIGH);    
if (i==0) digitalWrite(ledPin, LOW);

it works !

but with while  no ...


May 14, 2010, 02:14 pm Last Edit: May 14, 2010, 02:16 pm by PaulS Reason: 1
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 :
Code: [Select]
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 ?


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

Code: [Select]
  // Do something

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

Code: [Select]
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: [Select]
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.

look people , i know my code is fine !

Look, DIXIS, it isn't.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131