Go Down

Topic: Wrong going into while-loop (Read 540 times) previous topic - next topic

Fabitosh

Hi all
I'm working on my first Arduino-project. It's realy funny, but unfortunately I recently got a problem.
My goal to make a crosshair out of lightsensors. A laser beam aims in the middle of it. When the laserlight exits the middle of the crosshair (-> hit a lightsensor) it should give a signal, that an engine (actually represented by a light) can move the light back to the right place.
I did a screenshot while I was playing around with it in the workroom.
http://fabitosh.bplaced.net/arduino/screenshots/whileloop.png

After that I updated my commentaries in the code at home, I hope it's a bit better explained due to them. (on the screenshot are still the old ones, sorry!)
Here are the important parts of the code for now:
Code: [Select]

#define LEDa   13 // the pin for the LEDa

int photocellPin1 = 1;
int photocell1; // lightintensity from photocell 1
int photocell1old; // old value of the lightintensity

// int dif1; // change of the lightintensity


void setup() {
 pinMode(LEDa, OUTPUT);
 // Sensors are automatically INPUT
 Serial.begin(9600);
}

void loop() {
 photocell1 = analogRead(photocellPin1); // read the value from the sensor and save in variable photocell1 - The higher the value of photocell1, the more light shines on the photocell

 
 Serial.print("P1 ="); Serial.print(photocell1); Serial.print(" | Dif1 = "); Serial.println(dif1); // current photocell input

 
 while(photocell1 > 500); // activate reactor (light, engine) as long as the light is shining on the photocell
 {
   Serial.println("In der while-Schleife!");
   digitalWrite(LEDa, HIGH);
   photocell1 = analogRead(photocellPin1);
//    dif1 = photocell1 - photocell1old;
 }
 digitalWrite(LEDa, LOW); // stop reactor (light, engine)

 delay(1000);
 Serial.println();
 
}


The thing that confuses me is that the program goes in the while-loop, even if 'photocell1' is smaller that the required 500. When it gets bigger than 500 the program just stops and doesen't do anything more.
What's the fault here?

Coding Badly


Remove the highlighted character...

Code: [Select]
 while(photocell1 > 500)[glow]   ;   [/glow] // activate reactor (light, engine) as long as the light is shining on the photocell

Fabitosh

#2
May 07, 2010, 08:50 pm Last Edit: May 07, 2010, 08:58 pm by Fabitosh Reason: 1
Arghh.. I tried so many things and it's that - too dumb! :)
Thank you!

[edit]Shouldn't the 'Verify'-action report that as bug?[/edit]

Coding Badly

#3
May 07, 2010, 09:05 pm Last Edit: May 07, 2010, 09:05 pm by bcook Reason: 1

You're not dumb.  We've all made that kind of mistake.  It's very difficult to review your own source code.


A while loop with no body is valid C...

 while ( condition ) ;  // wait here until the condition is false

A body with no condition is valid C (and sometimes useful; it defines a "more local" scope)...

void MyFunc( void )
{
 // some code here
 {
   // more code here
 }
 // even more code here
}


Verify should not report any problems because there aren't any.

Go Up