Hello all, I am greeting you today with a program question. How would I make two conditions in a void loop only applicable one time unless the condition changes. By saying this I mean that, I want this void loop to run continually, unless the other IF condition is satisfied. And if same condition is met twice in a row,(ie. sensor reading between 800 and 10000, like 5000 and 6032), nothing will run. Here's my code and any help as to what reference commands I should use would be greatly 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
void setup(){
Serial.begin(9600);
setupTSL230();
pinMode(5,OUTPUT);
}
void loop(){
float lightLevel = readTSL230(TSL230_samples);
Serial.println(lightLevel);
if(lightLevel>800 && lightLevel<10000)
{
myservo1.attach(5);
myservo1.writeMicroseconds(1700);delay(5000);
myservo1.writeMicroseconds(1500);delay(5000);
}
if(lightLevel<800)
{
myservo1.attach(5);
myservo1.writeMicroseconds(1300);delay(5000);
myservo1.writeMicroseconds(1500);delay(5000);
}
}
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;
}
Declare a new int variable to count the number of times that the reading falls into your important range. Set the variable to zero initially. When the reading is in this range, increment (add one to) the variable. Finally, add further conditions to the if conditions to prevent the code inside from running if the count is two or greater.
When the reading is in this range, increment (add one to) the variable.
When the reading changes from outside the range to inside the range increment (add one to) the variable. Otherwise the value of the variable will increment all the time the value is in the range.
UKHeliBob:
the value of the variable will increment all the time the value is in the range.
Hi Bob, once the sketch detects a reading in that range, it won't detect it again for 10s, so I didn't bother suggesting what you describe. We don't know what the application is doing, the OP didn't describe it.
Would I define my command as an integer, defining it as x plus how many times it has run, then put along with my if variables, && x cannot equal more than two?
So what you are trying to do is read a certain value or higher and do one thing and then stop. read a different value or lower and do something else and stop w/o the the operations repeating themselves? I am struggling with the same problem as well. I would figure that this would be a common question but don't know where to look for the answer.
You are declaring a variable, not a command. You cannot define it as the number of times something happened because when the sketch begins to run, nothing has happened yet. So you can only initialse it to zero. You must increment the variable when the condition happens.
Yes, you need to add another condition to your if statements, using the new variable.
So your saying that I need to add the new x condition to my if statement conditions using &&, but when this code runs, add one, to x, so that when the last value isn't applied, it doesn't run and skips to the next line of conditions. If so, I really can't figure it out.