Arduino abort function

Hi
I have only just started using the Arduino UNO, I am looking to know how I build in an abort/stop programme in to programme code (if a voltage is below or above a certain limit ie out of control), also how do I build in hysterisis if I set my mid voltage (2.5v) ? any help would be appriciated

Use analogRead() to read a number directly proportional to the voltage on one of the analog input pins (A0-A5). The number will be between 0 and 1023 where 0 represents 0V and 1024 represents the Analog Reference voltage, usually 5V.

The rest is programming.

For hysteresis, use different values for the turn-on and turn-off points.

If you want the sketch to do something specific ("abort/stop programme") when your input is above or below certain limits, just test for those limits and write the sketch to do what you want.

Hi johnwasser, thank you for your reply,I am using a potemtiometer, I have used 511.5 to represent 2.5v which is the midpoint (this I am calling balanced) when the value drops below this level I am looking to switch on a green led and above this value to turn on a red led,but when it drops below 0.25v or above 4.75 v I want to abort and turn off the Leds, my programme is still in editing stage (full of mistakes so far) will keep trying, thanks for the help

const int RedLEDPin = 2;
const int GreenLEDPin = 3;

void setup() {
  pinMode(RedLEDPin, OUTPUT);
  pinMode(GreenLEDPin, OUTPUT);
}
void loop() {
  float voltage = analogRead(A0) * 5.0 / 1024.0;
  
  if (voltage < 0.25 || voltage > 4.75) {
    digitalWrite(RedLEDPin, LOW);
    digitalWrite(GreenLEDPin, LOW);
    while (true) {}  // do nothing further until reset
  }

  if (voltage < 2.5) {
    digitalWrite(RedLEDPin, LOW);
    digitalWrite(GreenLEDPin, HIGH);
  }

  if (voltage > 2.5) {
    digitalWrite(RedLEDPin, HIGH);
    digitalWrite(GreenLEDPin, LOW);
  }
}

Hi;

 if (voltage < 0.25 || voltage > 4.75) {
    digitalWrite(RedLEDPin, LOW);
    digitalWrite(GreenLEDPin, LOW);
    while (true) {}  // do nothing further until reset]
  }

  if (voltage < 2.5) {
    digitalWrite(RedLEDPin, LOW);
    digitalWrite(GreenLEDPin, HIGH);
  }

  if (voltage > 2.5) {
    digitalWrite(RedLEDPin, HIGH);
    digitalWrite(GreenLEDPin, LOW);
  }

Should be;

 if (voltage <= 0.25 || voltage >= 4.75) {
    digitalWrite(RedLEDPin, LOW);
    digitalWrite(GreenLEDPin, LOW);
    while (true) {}  // do nothing further until reset
  }

  if (voltage < 2.5) {
    digitalWrite(RedLEDPin, LOW);
    digitalWrite(GreenLEDPin, HIGH);
  }[

  if (voltage > 4.75) {
    digitalWrite(RedLEDPin, HIGH);
    digitalWrite(GreenLEDPin, LOW);
  }

Good morning, coffee just kicked in.

Tom....... :slight_smile:

TomGeorge:
Should be;

 if (voltage < 2.5) {

digitalWrite(RedLEDPin, LOW);
   digitalWrite(GreenLEDPin, HIGH);
 }[

if (voltage > 4.75) {
   digitalWrite(RedLEDPin, HIGH);
   digitalWrite(GreenLEDPin, LOW);
 }




Good morning, coffee just kicked in.

Tom....... :)

You need more coffee.
"2.5v which is the midpoint (this I am calling balanced) when the value drops below this level I am looking to switch on a green led and above this value to turn on a red led,"
You moved the switch point from "above 2.5V" to "above 4.75V"

Thanks Guys for the help, it is much appreciated, the circuit is working :smiley: one more question, I am using 1k resistors from Led to ground, why are the Leds very dimly lit ? also I am looking to add a delay see below before turning off the red LED but with the below the red stays on, any ideas what I am doing wrong ?

if (voltage > 2.5) {
digitalWrite(RedLEDPin, HIGH);
digitalWrite(GreenLEDPin, LOW);

delay (5000)
digitalWrite (RedLEDPin, LOW);
}

Use a smaller resistor to get brighter LEDs - try 330 ohm.

Your delay occurs while the LED is on. Then you turn it off, but immediately afterwards loop runs again and turns it back on. Put another delay after

    digitalWrite (RedLEDPin, LOW);

completestarter:
I am using 1k resistors from Led to ground, why are the Leds very dimly lit ?

Did you forget to set pinMode(RedLEDPin, OUTPUT); and pinMode(GreenLEDPin, OUTPUT); in setup()?

completestarter:
also I am looking to add a delay see below before turning off the red LED but with the below the red stays on, any ideas what I am doing wrong ?

Since the voltage is still >2.5 the red LED will be turned back on a few microseconds after you turn it off. If you want the LED to go off after a specific period of time even if the voltage is still above 2.5V that will take more design work and programming. You have a lot of decisions to make. What happens if the voltage goes below 2.5 before the time is up?

Thanks guys, that was the problem, wildbill as you suggested I added a delay after digitalWrite (redLEDpin, low) and this worked, The delay was to be for 4 minutes before turning off for another period so I added this in so all good, many thanks

Hi, I am still working on the same code, I am looking to change the below code that instead of a 3 minute delay that I can increment to check that the voltage is in range and if not, that it will turn on an LED for up to a max 3 minutes, how can I write this using ++x or is there an easier way ?

if(setpoint <409 && > 51.15){ // if the voltage is less than 2 V and greater than 0.5V

digitalWrite(redLed, HIGH);

delay (4 * 60 * 1000);

digitalWrite (redLed, LOW);