I need help for programming three voltage sensors

Hello everyone

I'm making some kind of voltage alarm. I have three voltage sensors connected to the analog inputs and one sensor at the digital input. How to write a program that detects the first two inputs A0 and A1 too high or too low voltage and immediately writes it to the digital output for 5 seconds (HIGH) then back to (LOW) and waits until the value returns to the set. However, the sensor at input A2 only detects too low a voltage value. On the digital input 2 there is a switch that detects if the lid of the box where the sensor is located is closed.

void setup() {
  // Pin Mode Definition
  pinMode(2, INPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);


void loop() {
  if (analogRead(A0) > 170 || analogRead(A0) < 100) {
    digitalWrite(12, HIGH);
    digitalWrite(12, LOW);
    while (!(analogRead(A0) < 170 || analogRead(A0) > 100)) {
  if (analogRead(A1) > 170 || analogRead(A1) < 100) {
    digitalWrite(11, HIGH);
    digitalWrite(11, LOW);
    while (!(analogRead(A1) < 60 || analogRead(A1) > 100)) {
  if (analogRead(A2) < 320) {
    digitalWrite(10, HIGH);
    digitalWrite(10, LOW);
    while (!(analogRead(A2) > 320)) {
  if (digitalRead(2) == LOW) {
    digitalWrite(9, HIGH);
    digitalWrite(9, LOW);
    while (!(digitalRead(2) == HIGH)) {

Thanks for any help

Please read How to get the best out of this forum.

How I (or anyone else) would write the code is not essential, the essence is how YOU would write it.


#define TIMEOUT 5000

enum { Off = HIGH, On = LOW };

#undef MyHW
#ifdef MyHW
byte ledPins  [] = { 10, 11, 12 };
byte anlgPins [] = { A1, A2, A3 };

byte ledPins  [] = { 12, 11, 10 };
byte anlgPins [] = { A0, A1, A2 };

# define N_PINS  sizeof(ledPins)

int threshLow [N_PINS] = { 100, 100, 100 };
int threshHi  [N_PINS] = { 170, 170, 170 };
int alarm     [N_PINS];

unsigned long msecLst [N_PINS];

void setup() {
    Serial.begin (9600);

    for (unsigned n = 0; n < N_PINS; n++)  {
        pinMode (anlgPins [n], INPUT_PULLUP);   // used as buttons
        pinMode (ledPins  [n], OUTPUT);
        digitalWrite (ledPins  [n], Off);

void loop() {
    unsigned long msec = millis ();

    for (unsigned n = 0; n < N_PINS; n++)  {
        int anlg = analogRead (anlgPins [n]);

#ifdef NyHW
        if (1000 < anlg)        // translate button val to mid-range val
            anlg = 140;

        if (anlg < threshLow [n] || threshHi [n] < anlg)  {
            msecLst [n] = msec;

            alarm   [n] = 1;
            digitalWrite (ledPins [n], On);

        if (alarm [n] && (msec - msecLst [n]) > TIMEOUT)  {
            alarm   [n] = 0;
            digitalWrite (ledPins [n], Off);

analogReads take a considerable time, so you'd be better reading them once into variables at the top of loop

you should use constants for IO-pins-numbers instead of hardcoded direct numbers.
This will make your code easier to understand and easier to maintain.

you do not have even a comment what IO-pin 2,9,10,11,12 are for.
So I'm using names that could make sense but will not describe what they mean in your project,
It is just to show the principle.

  pinMode(2, INPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);

using constants means your code looks like this

const byte lidOfBox_pin = 2;
const byte sirene_pin   = 9;
const byte watervalvle_pin = 10;
const byte buzzer_pin      = 11;
const byte myBlaBla_pin    = 12;

const byte batteryVoltage_pin = A0;
const byte solarVoltage_pin   = A1;

  if (analogRead(batteryVoltage_pin) > 170 || analogRead(batteryVoltage_pin) < 100) {
    digitalWrite(myBlaBla_pin, HIGH);
    digitalWrite(myBlaBla_pin, LOW);

you can use whatever names you like. But they should be self-explaining.

So the questions are
what is connected to your IO-pins and to the analog-pins?

using delay is blocking for 5000 milliseconds nothing else happends than
waiting for the 5000 milliseconds to pass by.

Your code should be more responsive. therefor you need non-blocking timing based on function millis()

Add serial-debug-output to see what is really going on in your code.

Ask specific question about any detail or word you don't know yet.

best regards Stefan

Hi, @simonpivk
Welcome to the forum.

Can you please post a circuit diagram please?

What is the overall application?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

It seem to be a piece of homework, isn´t it?

Not really. I would need such a system at home, but I intended to make a security system.

const byte lidOfBox_pin     = 2;
const byte dooropen_pin     = 9;
const byte batterylow_pin  = 10;
const byte zone2broken_pin = 11;
const byte zone1broken_pin = 12;

const byte zone1control_pin   = A0;
const byte zone2control_pin   = A1;
const byte batterycontrol_pin = A2;

what does this mean?

"You would need such a systemA at home."

Under what circumstances do you need it?

and then your sentence goes on
"but I intended to make a security system"

in this case the word "but" indicates instead of needing such a system you planned in the past) to make a security system (which seems something different) than the systemA

As this planning was in the past what are you doing now in the present?

I assume you mean something different but thats what your english words are saying.

If english is not your native language use google-translate.
I guarantee that if you write what you want to say in your motherlanguage and let do google-translate the translation it will be much better understandable.

best regards Stefan

Addition: you have a very unconventional way of drawing electronic schematics.
It was an interesting exercise to re-draw a part of your schematic to the usual way to understand how the rersistors are connected.

To write a program I do not have much experience. So please understand.

I want to use this device to control a mesh fence for animals. After the fence above and below I will install a wire in between I will add a 10kohm resistor which will serve as a voltage divider along with a 15kohm resistor on the circuit. The actual intermediate voltage then becomes halfway from the input and so I sense that an error has occurred if someone inserts a bridge over the complete circuit.
I sense this at inputs A0 and A1.
At input A2, I measure the voltage of the battery to power the entire system so that it does not discharge too much.
On the digital input 2 I have the system box door connected so that in the event of the box opening an alarm is triggered.
Output IO 12,11,10 and 9 I have a connected wireless transmitter.

i think it's pretty obvious what you're trying to do.

monitor voltages, set some LEDs when voltages are out of range

That's what I want to do.

Can you please post a picture of your project?
We need to see your component layout.
Particularly the protoboard.

Have you got a DMM?

Tom... :smiley: :+1: :coffee: :australia: