Hello everyone. I am working on a sun shade project and i generally need help with my code. I want one of the conditions in my void loop to run, and then never run again until the other condition is satisfied,(so the same code or servo movement doesn't happen twice. The way I have my code setup now is that when the bottom condition in the loop is satisfied, it'll run, but then it will run again, which I don't want, and then when I get values that fall within the other condition, nothing happens. Here's my code and any help would be appreciated.
// Reports the frequency from the TSL230, higher number means brighter
// Part: http://www.sparkfun.com/products/8940
// Article: http://bildr.org/2011/08/tsl230r-arduino/
#include <Servo.h>
Servo myservo1;
int TSL230_Pin = 4; //TSL230 output
int TSL230_s0 = 3; //TSL230 sensitivity setting 1
int TSL230_s1 = 2; //TSL230 sensitivity setting 2
int TSL230_samples = 30; //higher = slower but more stable and accurate
boolean once;
void setup(){
Serial.begin(9600);
setupTSL230();
pinMode(5,OUTPUT);
myservo1.attach(5);
}
void loop(){
float lightLevel = readTSL230(TSL230_samples);
Serial.println(lightLevel);
boolean once;
if(lightLevel>1800 && lightLevel<10000 && once == true)
{
myservo1.writeMicroseconds(1300);delay(5000);
myservo1.writeMicroseconds(1500);delay(5000);
once = false;
}
if(lightLevel<1800 && once == false)
{
myservo1.attach(5);
myservo1.writeMicroseconds(1700);delay(5000);
myservo1.writeMicroseconds(1500);delay(5000);
once = true;
}
}
Even if "once" were initialized correctly, you're still going to have problems with it because you've declared it twice. It is declared globally, and declared in the loop() function. I think you should remove the one in loop().
As AWOL is pointing out, you are using (in this case testing) a variable before setting it to a known value. Often a newly declared variable with default to zero (which is also boolean false). Not always, though, sometimes the default value may be what ever was left over in the memory space now being used for the variable. Best practice is to explicitly set a variable's value before using it so it is in a known state before it is used.
I also notice that you declare once twice. The first time is outside of any function so you have a variable called "once" that has a global scope. Any function can access it unless they declare their own variable with the same name. Then in loop() you declare another variable called "once" that has local scope to loop() masking the global variable named "once". But because it doesn't use the static keyword it looses it's contents on every iteration of loop().
Only declare once once, either as a global, or local to loop() with the static keyword. Also, make sure that it has a default value before you use it for anything.
Okay I've taken out the boolean once out of the void loop, and the read TSL_320 is in the actual full program, I was just posting what I had issues with. So your saying that I need to define once as something for it to be true or false. But how would It know to change the once value to make the conditions true or false? And here's the code.
// Reports the frequency from the TSL230, higher number means brighter
// Part: http://www.sparkfun.com/products/8940
// Article: http://bildr.org/2011/08/tsl230r-arduino/
#include <Servo.h>
Servo myservo1;
int TSL230_Pin = 4; //TSL230 output
int TSL230_s0 = 3; //TSL230 sensitivity setting 1
int TSL230_s1 = 2; //TSL230 sensitivity setting 2
int TSL230_samples = 30; //higher = slower but more stable and accurate
boolean once;
void setup(){
Serial.begin(9600);
setupTSL230();
pinMode(5,OUTPUT);
myservo1.attach(5);
}
void loop(){
float lightLevel = readTSL230(TSL230_samples);
Serial.println(lightLevel);
if(lightLevel>1800 && lightLevel<10000 && once == true)
{
myservo1.writeMicroseconds(1300);delay(5000);
myservo1.writeMicroseconds(1500);delay(5000);
once = false;
}
else if(lightLevel<1800 && once == false)
{
myservo1.attach(5);
myservo1.writeMicroseconds(1700);delay(5000);
myservo1.writeMicroseconds(1500);delay(5000);
once = true;
}
}
void setupTSL230(){
pinMode(TSL230_s0, OUTPUT);
pinMode(TSL230_s1, OUTPUT);
//configure sensitivity - Can set to
//S1 LOW | S0 HIGH: low
//S1 HIGH | S0 LOW: med
//S1 HIGH | S0 HIGH: high
digitalWrite(TSL230_s1, LOW);
digitalWrite(TSL230_s0, HIGH);
}
float readTSL230(int samples){
//sample light, return reading in frequency
//higher number means brighter
float start = micros();
int readings = 0;
while(readings < samples){
pulseIn(TSL230_Pin, HIGH);
readings ++;
}
float length = micros() - start;
float freq = (1000000 / (length / samples)) * 10;
return freq;
}