Stopping a Loop

Hi, I am starting to learn more about the Arduino and programming however I have hit a dead end and just need a little guidance to which direction to go next.

Basically i want to make a script which measures an Analogue input pin, when its inside a specified range a Yellow LED illuminates, when its outside that range I want the Green LED to illuminate for 8 seconds and then to switch off. however with my codes (see below) It will switch the Green LED off for a single operation and then loop back and stay on for 8 seconds. The other issue is that if the Analogue input increases to the acceptable region while its in the 8 second delay, it will not instantly interrupt until the 8 seconds is up.

 */

int sensorPin = A0;    // select the input pin for the potentiometer
int YellowLED = 13;
int GreenLED = 12;
int sensorValueMax = 600;  // variable to store the value coming from the sensor
int sensorValueMin = 200;

void setup() {
  pinMode(YellowLED, OUTPUT);
  pinMode(GreenLED, OUTPUT);
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);


if (sensorValueMax > analogRead(sensorPin) && sensorValueMin < analogRead(sensorPin)){
     digitalWrite(YellowLED, HIGH);
     digitalWrite(GreenLED, LOW);
}
else
{
     digitalWrite(YellowLED, LOW);
     digitalWrite(GreenLED, HIGH);
     delay(8000);
     digitalWrite(GreenLED, LOW);     
}
}

I have tried to experiment with attachInterrupt but with not much luck.

If anyone could give some guidance on where to go/ what to try next i would be very grateful

Thanks :slight_smile:

Warlord_1011:
The other issue is that if the Analogue input increases to the acceptable region while its in the 8 second delay, it will not instantly interrupt until the 8 seconds is up.

Welcome to the wonderful world of delay(), otherwise known as sitAroundAndTwiddleMyThumbs(). Take a look at the Blink Without Delay example for writing non-blocking code that can emulate a "delay".

int sensorPin = A0;    // select the input pin for the potentiometer
...
  int sensorReading = analogRead(A0);

Why bother giving your pin a name if you're not going to use it?

  int sensorReading = analogRead(A0);

if (sensorValueMax > analogRead(sensorPin) && sensorValueMin < analogRead(sensorPin)){

Why bother storing the results of the reading in a variable and not using the variable?

     digitalWrite(YellowLED, LOW);
     digitalWrite(GreenLED, HIGH);
     delay(8000);
     digitalWrite(GreenLED, LOW);

This piece of code will run so long as the sensor is still within the acceptable range, meaning after you turn the green LED off, a few microseconds of code will execute, and it will be turned on again for another 8 seconds. You're going to have to define your requirements a little better. After 8 seconds, the LED should turn off? Until when? What if the sensor is still within the acceptable range?

I have tried to experiment with attachInterrupt but with not much luck.

Not surprised, nothing in this code screams I need an interrupt.

I actually copied the Analogue Input code from the Example :smiley:

So i wasnt quite sure why.

So i will have a look at the Blink Without Delay

Cheers :slight_smile:

Warlord_1011:
I actually copied the Analogue Input code from the Example :smiley:

So i wasnt quite sure why.

Except the Analog Input actually uses all of the variables it declares.

Im still not having much luck!

I have it so it will now switch between either LED when the Analogue Input changes, however the Green LED flashes, I am not sure how to change it so that it flashes once and stays low for eternity until the Analogue reading changes to an acceptable level.

 */

// constants won't change. Used here to 
// set pin numbers:
int sensorPin = A0;    // select the input pin for the potentiometer
int YellowLED = 13;
int sensorValueMax = 600;  // variable to store the value coming from the sensor
int sensorValueMin = 200;
int GreenLED =  12;      // the number of the LED pin

// Variables will change:
int ledState = LOW;       // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(YellowLED, OUTPUT);
  pinMode(GreenLED, OUTPUT); 
  digitalWrite(GreenLED, HIGH);  
}

void loop()
{
if (sensorValueMax > analogRead(sensorPin) && sensorValueMin < analogRead(sensorPin)){
     digitalWrite(YellowLED, HIGH);
     digitalWrite(GreenLED, LOW);
}
else
{
       digitalWrite(YellowLED, LOW);      
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(GreenLED, ledState);
  }
}
}

Any tips?

The Blink Without Delay example demonstrates the concepts on using non-blocking code to "time" things. In the example, an LED is continuously blinked. You don't want an LED to continuously blink, so copying and pasting the code into your code, expecting it to do what you like doesn't seem appropriate. Around here, Take a look really means play with the example enough so that you know exactly how it works and how to bend it to your liking.

In your specific example, my hint would be that the variable named previousMillis, would be better named lastTimeThatReadingWasInsideOfThreshold. A bit lengthy, but the updated name gives you a hint on when you should be setting it to millis(), and what the value of millis() - lastTimeThatReadingWasInsideOfThreshold represents.

Ahh ok that makes a bit more sense.

I have been playing with it. And was able to get it to latch low, but when the analogue input was raised to the acceptable level and the dropped again (via a potentiometer) It would not illuminate at all.

That's a shame.
Perhaps if you posted the code you'd been playing with, we could offer more concrete help.