user49886: 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);

}


user15180: 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 :)


49886: <p>I want is to stop the loop and when it read  Greater than 900.00 again it will do task again.</p>

<p>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</p>

<pre><code>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 &gt;= 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 &lt; 400; i++){
    digitalWrite(smStepPin, HIGH);
    delayMicroseconds(700);
    digitalWrite(smStepPin, LOW);
    delayMicroseconds(700);
}
}

  delay(1000);

}
</code></pre>
 15180: <pre><code>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&lt;=900) &amp;&amp; (reading&gt;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
}
</code></pre>

<ul>
<li>So on begining <code>old_reading</code> is just zero, but being declared outside <code>loop</code>, it keeps its value between different runs of <code>loop</code>. </li>
<li>the <code>if (condition)</code> does not hit, as <code>reading</code> is less than 900 (if it starts with more then 900, see next part) </li>
<li>the end with <code>old_reading=reading;</code> makes <code>old_reading</code> to be the last known value of <code>reading</code></li>
<li>when <code>reading</code> goes Greater than 900 for first time, the <code>old_reading</code> is Less or Equal 900 (as it was the last value of <code>reading</code> before it crossed 900 up. </li>
<li><ul>
<li>so the <code>if (condiotion)</code> is true and you start moving motor</li>
</ul></li>
<li><ul>
<li>then (on the end) you set <code>old_reading=reading;</code> and <code>old_reading</code> is also larger then 900</li>
</ul></li>
<li><ul>
<li>while <code>reading</code> stays over 900 in next loops, the <code>old_reading</code> does so, and therefore the <code>if (condition)</code> does not hit - <code>(old_reading&lt;=900)</code> is not true</li>
</ul></li>
<li><ul>
<li>when <code>reading</code> falls down 900 or lower, the <code>if(condition)</code> also does not hit, but after that the <code>old_reading=reading;</code> makes <code>old_reading</code> less or equal 900</li>
</ul></li>
<li>and so we are at the start again, skipping the part after <code>if(condition)</code> until <code>reading</code> gets Greater than 900 (then we run motor, set <code>old_reading</code> to something greater than 900 and we already was here and know, how it works :)</li>
</ul>
