Multiple sensors for similar output

Hi all,
I'm still really new to this ardino malarkey, I've spent a bit of time getting to grips with some of the basics.
I've managed to get as far as I can with some code, but I cant quite get my head around something.

Excuse the code, I'm keeping it as basic to me as I can until I understand everything better. I'm currently using a Uno to prototype what I want to do, and will be using a nano in the final project, hence some extra lines of code, (8 output and inputs)

What I am trying to achieve is:
Independently switch 4 relays on and off from a voltage from 4 independent voltage sensors.

So when voltage is read from say pin 1, relay 4 is active, but relays 1,2 and 3 are not.
Then when voltage is read from pin 4, relay 2 is active but relays 1,3 and 4 are not. etc

i think its a case of some if, else if, and else statements, but i may be wrong.

see below for my attempt at code, and help point me in the right direction.

/*
 * Control for 8 relays to control 8 LED strips.
 * 8 relays triggered to shut 7 relays open from an indepentant voltage input from a voltage sensor on analog pins.
 * Relays closed in normal state, so lighting on. signal switches LED off
 */

const int analogPin1 = A0;     // read voltage from pin 0
const int analogPin2 = A1;     // read voltage from pin 1
const int analogPin3 = A2;     // read voltage from pin 2
const int analogPin4 = A3;     // read voltage from pin 3
const int analogPin5 = A4;     // read voltage from pin 4
const int analogPin6 = A5;     // read voltage from pin 5
const int analogPin7 = A6;     // read voltage from pin 6
const int analogPin8 = A7;     // read voltage from pin 7

                      
const int threshold = 4;           // input voltage 4v or more


void setup()
{
  pinMode(13, OUTPUT);// sets the digital pin 13 as output
  pinMode(12, OUTPUT);// sets the digital pin 12 as output
  pinMode(11, OUTPUT);// sets the digital pin 11 as output
  pinMode(10, OUTPUT);// sets the digital pin 10 as output
  pinMode(9, OUTPUT);// sets the digital pin 09 as output
  pinMode(8, OUTPUT);// sets the digital pin 08 as output
  pinMode(7, OUTPUT);// sets the digital pin 07 as output
  pinMode(6, OUTPUT);// sets the digital pin 06 as output

   Serial.begin(9600);              //  setup serial
}

void loop() {
  // read the sensor:
  const int analogValue = analogRead(analogPin1);
  const int analogValue2= analogRead(analogPin2);
  

  // if the analog value is 0, keep all LEDs on, is analog value detected at sensor, turn off 7 bays keep one on.
  if (analogValue < threshold) {
    digitalWrite(13, LOW);
     digitalWrite(12, LOW);
     digitalWrite(11, LOW);
     digitalWrite(10, LOW);
     digitalWrite(9, LOW);
     digitalWrite(8, LOW);
     digitalWrite(7, LOW);
     digitalWrite(6, LOW);
    
  } else {
    digitalWrite(13, HIGH);
     digitalWrite(12, HIGH);
     digitalWrite(11, HIGH);
     digitalWrite(10, HIGH);
     digitalWrite(9, HIGH);
     digitalWrite(8, HIGH);
     digitalWrite(7, HIGH);
     digitalWrite(6, LOW);
     
  }
  if (analogValue2 < threshold) {
    digitalWrite(13, LOW);
     digitalWrite(12, LOW);
     digitalWrite(11, LOW);
     digitalWrite(10, LOW);
     digitalWrite(9, LOW);
     digitalWrite(8, LOW);
     digitalWrite(7, LOW);
     digitalWrite(6, LOW);
    
  } else {
    digitalWrite(13, HIGH);
     digitalWrite(12, HIGH);
     digitalWrite(11, HIGH);
     digitalWrite(10, HIGH);
     digitalWrite(9, HIGH);
     digitalWrite(8, HIGH);
     digitalWrite(7, LOW);
     digitalWrite(6, HIGH);
     
  }

  // print the analog value:
  Serial.println(analogValue);
  delay(1);        // delay in between reads for stability
}

Cheers!

This line may be the major problem

const int threshold = 4;           // input voltage 4v or more

analogRead() gives a value from 0 to 1023 for 0v and 5v. Consequently 4v would give a value of 1023 / 5 * 4 = 818.

...R

Just tried 818 as a value, none of the relays switched.

4v was just an value i picked, being greater than 0v with threshold being 4, i can get one of my relays to switch, 818 switches none .

Thanks

There is a lot more to debugging a program than just trying one number that someone like me suggests and then reporting that none of the relays switches.

You need to think hard about the problem and how you might investigate it.

For example, what number is your analogRead() actually giving?

...R

Hi,
What output do you want if both inputs are over the threshold.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

You have 4 inputs, that is 16 different combinations?

You need to account for each combination.

Tom... :slight_smile:

  const int analogValue = analogRead(analogPin1);
  const int analogValue2= analogRead(analogPin2);

You declare a constant then try to read a (changing) value into it. I don't think that will work out well.

Hi all,
hopefully this will help clear up what it is i'm trying to achieve.
Below is the circuit I'm working with essentially.
I know the sensors in the CAD drawing are not voltage sensors, but i couldn't find them.

I have changed some code this evening, I now get a value in the serial monitor for each sensor.
I used to get a continuous reading of give of take the same value, when voltage was applied to a sensor.
Now I have each sensor printing to the serial monitor. I get a value displaying like this:

0
0
0
485
0
0
0
485

for arguments sake.

Not like below

485
485
485
485
485

When I did with just one sensor.

I have also included a photo of what it is im trying to achieve.
As can be seen I have 8 games consoles with 8 LED strips wired in parallel.
What Im trying to do, is when none of the consoles are on all 8 lights are on.
When I turn one console on for example my Sega mega drive (genesis, if you are from outside europe).
The light in that bay stays on, and the other 7 shut off.
I'll be taking a feed off each power on switch which then goes to the voltage sensor.

Hope that makes some sense!

Cheers

OCDbyrnes:
I have changed some code this evening,

Please post the latest version so we can keep up with you.

And please DO NOT over write the code in your Original Post. It can be useful to compare versions to see what was changed.

...R

Hey Robin, see code below

/*
 * Control for 8 relays to control 8 LED strips.
 * 8 relays triggered to shut 7 relays open from an indepentant voltage input from a voltage sensor on analog pins.
 * Relays closed in normal state, so lighting on. signal switches LED off
 */

 int analogPin1 = A0;     // read voltage from pin 0
 int analogPin2 = A1;     // read voltage from pin 1
 int analogPin3 = A2;     // read voltage from pin 2
 int analogPin4 = A3;     // read voltage from pin 3
 int analogPin5 = A4;     // read voltage from pin 4
 int analogPin6 = A5;     // read voltage from pin 5
 int analogPin7 = A6;     // read voltage from pin 6
 int analogPin8 = A7;     // read voltage from pin 7

                      
const int threshold = 4;           // input voltage 4v or more


void setup()
{
  pinMode(13, OUTPUT);// sets the digital pin 13 as output
  pinMode(12, OUTPUT);// sets the digital pin 12 as output
  pinMode(11, OUTPUT);// sets the digital pin 11 as output
  pinMode(10, OUTPUT);// sets the digital pin 10 as output
  pinMode(9, OUTPUT);// sets the digital pin 09 as output
  pinMode(8, OUTPUT);// sets the digital pin 08 as output
  pinMode(7, OUTPUT);// sets the digital pin 07 as output
  pinMode(6, OUTPUT);// sets the digital pin 06 as output

   Serial.begin(9600);              //  setup serial
}

void loop() {
  // read the value of the sensor:
  int analogValue = analogRead(analogPin1);
  int analogValue1 = analogRead(analogPin2);
  int analogValue2 = analogRead(analogPin3);
   int analogValue3 = analogRead(analogPin4);
    int analogValue4 = analogRead(analogPin5);
     int analogValue5 = analogRead(analogPin6);
      int analogValue6 = analogRead(analogPin7);
       int analogValue7 = analogRead(analogPin8);
  

  // if the analog value is high enough, turn on the LED:
  if (analogValue < threshold) {
    digitalWrite(13, LOW);
     digitalWrite(12, LOW);
     digitalWrite(11, LOW);
     digitalWrite(10, LOW);
     digitalWrite(9, LOW);
     digitalWrite(8, LOW);
     digitalWrite(7, LOW);
     digitalWrite(6, LOW);
  }
     
    
   else {
    digitalWrite(13, HIGH);
     digitalWrite(12, HIGH);
     digitalWrite(11, HIGH);
     digitalWrite(10, HIGH);
     digitalWrite(9, HIGH);
     digitalWrite(8, HIGH);
     digitalWrite(7, HIGH);
     digitalWrite(6, LOW);
     
  }

   if (analogValue1 < threshold) {
    digitalWrite(13, LOW);
     digitalWrite(12, LOW);
     digitalWrite(11, LOW);
     digitalWrite(10, LOW);
     digitalWrite(9, LOW);
     digitalWrite(8, LOW);
     digitalWrite(7, LOW);
     digitalWrite(6, LOW);
  }
     
    
   else {
    digitalWrite(13, HIGH);
     digitalWrite(12, HIGH);
     digitalWrite(11, HIGH);
     digitalWrite(10, HIGH);
     digitalWrite(9, HIGH);
     digitalWrite(8, HIGH);
     digitalWrite(7, LOW);
     digitalWrite(6, HIGH);
     
  }
  

  // print the analog value:
  Serial.println(analogValue);
  Serial.println(analogValue1);
  Serial.println(analogValue2);
  Serial.println(analogValue3);
  Serial.println(analogValue4);
  Serial.println(analogValue5);
  Serial.println(analogValue6);
  Serial.println(analogValue7);
  delay(1);        // delay in between reads for stability
}

Try this version. See the value of using arrays.

byte relaySettings[8] = {0,0,0,0,0,0,0,0); // assumes 0 = off
byte relayPins[8] = {13,12,11,10,9,8,7,6};
int adcVal;

//================
void setup() {
    for (byte n = 0; n < 8; n++) {
        pinMode(relayPins[n], OUTPUT);
    }
    Serial.begin(9600);      


//================
void loop() {
    for (byte n = 0; n < 8; n++) {
        adcVal = analogRead(A0 + n);
        printAdcData(n);
        if (adcVal < threshold) {
            relaySettings[n] = 1;
        }
        else {
            relaySettings[n] = 0;
        }
    }

    for (byte n = 0; n < 8; n++) {
        digitalWrite(relayPins[n], relaySettings[n]);
    }
}

//===============
void printAdcData( byte adcNum) {
    Serial.print("Adc ");
    Serial.print(adcNum);
    Serial.print(" ");
    Serial.println(adcVal);
}

...R

Hi,

Thanks for the pic and image. :slight_smile:

Can you please post picture of a hand drawn circuit in jpg, png?
Your hand has so more informative component symbols than Fritzy ever will. :o

I know you are not using LDRs, so please show what your voltage sensors will be.
You cannot drive relays directly with the UNO output, it cannot supply the current.

Can you post a table of your input combinations vs relay outputs.
There is a better way than using a confusing array of if.. else.. statements.

The other solution involves giving each input a unique binary value.
Then only activating outputs to specified combinations of input.

If you input is going to be either HIGH or LOW signals then use digital input type sigmals.

Tom.... :slight_smile:

Ops Pics


Tom... :slight_smile:

Thanks for your help Robin and Tom!

The code Robin provided has now made each sensor control one relay independently and display a value in the serial monitor.

Who'd have thought that id use mathematical equations since i left school many years ago, haha!

Am i right in thinking that if i was to essentially create a table of values like how a logic gate works, i can then do what I want?

I've attached a hand written table of that I want to achieve for half the amount of sensors, which will be replicated for all 8 sensors.

I assume a 'table of events' would do away with a long list of if...else statements?

Also, I'm not sure how to embed an image rather than adding them as attachements.

Thanks again guys, its's gone midnight so ill have a think about all this at work tomorrow and have a play around when I get home!

Hi,
Thanks for the table, but what do you want to have happen if sensor 1 AND sensor 2 are ON, or 1,2 and 3, or 2 and 4 ?

If humans are controlling this, then any combination is possible.

24 = 16 combinations.

Tom.. :slight_smile:

Image from Reply #12 so we don't have to download it. See this Simple Image Guide

...R

Please try again. I am NOT going to turn my laptop on its side to read it,

It would be much better if you type the stuff in your text editor and then just copy and paste the text.

...R

The pattern is apparently that if a sensor is 'on' the system should turn on it's corresponding relay and all others should be off. Easy to achieve by extending Robin2's array example a little.

It won't be pretty though if two inputs are on - should there be some delay mechanism to avoid the relays trying to destroy themselves?

Hi,
This looks like another use for switch.. case and assigning each input a binary value, 1 ,2, 4, 8, 16.

That is why I want to know about the different combinations of more than one input HIGH.

Tom... :slight_smile:

wildbill:
The pattern is apparently that if a sensor is 'on' the system should turn on it's corresponding relay and all others should be off. Easy to achieve by extending Robin2's array example a little.

I don't think my example needs any extending.

...R

Robin2:
I don't think my example needs any extending.

...R

It may. Your example implements turning a relay on or off controlled by its associated sensor. It doesn't address turning all others off when you turn one on. It'll be fine if only one sensor can be on at a time of course.