I want is to stop the loop and when it read Greater than 900.00 again it will do task again.
I need condition. When it back to greater than 900.00 the code below will do task and then it will stop. And when it will reach the condition again it will do the code again and it will stop .. Please help me
void loop(){
float reading = analogRead(LIGHTSENSORPIN); //Read light level
float square_ratio = reading / 1023.0; //Get percent of maximum value (1023)
square_ratio = pow(square_ratio, 2.0); //Square to make response more obvious
analogWrite(LEDPIN, 255.0 * square_ratio); //Adjust LED brightness relatively
analogWrite(LED_BUILTIN, 255.0 * square_ratio); //Adjust LED brightness relatively
Serial.println(reading); //Display reading in serial monitor
if(reading >= 900.00){
digitalWrite(smDirectionPin, LOW); //Writes the direction to the EasyDriver DIR pin. (LOW is counter clockwise).
/*Turns the motor fast 1600 steps*/
for (int i = 0; i < 400; i++){
digitalWrite(smStepPin, HIGH);
delayMicroseconds(700);
digitalWrite(smStepPin, LOW);
delayMicroseconds(700);
}
}
delay(1000);
}
kervy hiponakervy hipona
1 Answer 1
setup(){ ....
......
}
float old_reading=0;
loop() {
float reading = analogRead(LIGHTSENSORPIN); //Read light level
// ... do this always (maybe empty)
// in your code it is LED shining and printing
if ((old_reading<=900) && (reading>900)) {
// ... do this just just once when reading goes over 900 up
// in your code it is manipulating motor
}
old_reading=reading;
// ... do this always , but after special part (maybe empty)
// in your code it is just delay
}
- So on begining
old_reading
is just zero, but being declared outsideloop
, it keeps its value between different runs ofloop
. - the
if (condition)
does not hit, asreading
is less than 900 (if it starts with more then 900, see next part) - the end with
old_reading=reading;
makesold_reading
to be the last known value ofreading
- when
reading
goes Greater than 900 for first time, theold_reading
is Less or Equal 900 (as it was the last value ofreading
before it crossed 900 up. - so the
if (condiotion)
is true and you start moving motor
- so the
- then (on the end) you set
old_reading=reading;
andold_reading
is also larger then 900
- then (on the end) you set
- while
reading
stays over 900 in next loops, theold_reading
does so, and therefore theif (condition)
does not hit -(old_reading<=900)
is not true
- while
- when
reading
falls down 900 or lower, theif(condition)
also does not hit, but after that theold_reading=reading;
makesold_reading
less or equal 900
- when
- and so we are at the start again, skipping the part after
if(condition)
untilreading
gets Greater than 900 (then we run motor, setold_reading
to something greater than 900 and we already was here and know, how it works :)
answered Sep 28, 2018 at 17:21
-
Thank you sir gilhad... i appreciate your time and effort doing this.. keep helping otherskervy hipona– kervy hipona2018年09月28日 17:34:27 +00:00Commented Sep 28, 2018 at 17:34
-
1:) This is just basic programming (not even about Arduino) - you really should read more articles/books about programming and read examples and shorter programs and try to understand it by yourself - it will give much more understanding. I suggest to read arduino.cc and/or other sites about arduino and about programming for beginers. Really pays off in long term :)gilhad– gilhad2018年09月28日 17:48:47 +00:00Commented Sep 28, 2018 at 17:48
lang-cpp
if
toif(oldReading<900.00 and reading>=900.00)