0

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);
}
asked Sep 28, 2018 at 15:06
6
  • You need to also store the old reading. Then change the if to if(oldReading<900.00 and reading>=900.00) Commented Sep 28, 2018 at 15:23
  • how can i store the old reading?? Commented Sep 28, 2018 at 15:27
  • Please post your code, instead of an image. Commented Sep 28, 2018 at 15:30
  • Sir can u please modify my code to do that? Commented Sep 28, 2018 at 15:40
  • anybody can help?? please.. Commented Sep 28, 2018 at 15:57

1 Answer 1

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 outside loop, it keeps its value between different runs of loop.
  • the if (condition) does not hit, as reading is less than 900 (if it starts with more then 900, see next part)
  • the end with old_reading=reading; makes old_reading to be the last known value of reading
  • when reading goes Greater than 900 for first time, the old_reading is Less or Equal 900 (as it was the last value of reading before it crossed 900 up.
    • so the if (condiotion) is true and you start moving motor
    • then (on the end) you set old_reading=reading; and old_reading is also larger then 900
    • while reading stays over 900 in next loops, the old_reading does so, and therefore the if (condition) does not hit - (old_reading<=900) is not true
    • when reading falls down 900 or lower, the if(condition) also does not hit, but after that the old_reading=reading; makes old_reading less or equal 900
  • and so we are at the start again, skipping the part after if(condition) until reading gets Greater than 900 (then we run motor, set old_reading to something greater than 900 and we already was here and know, how it works :)
answered Sep 28, 2018 at 17:21
2
  • Thank you sir gilhad... i appreciate your time and effort doing this.. keep helping others Commented 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 :) Commented Sep 28, 2018 at 17:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.