Arduino UNO Setpoint Issue

I am building a project that utilizes one Arduino UNO as a master controller that monitors six different voltage inputs and depending on the value of each input, it will energize or de-energize two outputs per input (12 outputs total). However, I am running into an issue where my Arduino is only detecting one input and disregarding the others. For example, I have A0 corresponding to D0 and D1. When A0 is above the set point, D0 and D1 are both high. However, For A1 - A5, the associated outputs (D2 - D11) are not doing anything. I'm fairly new to coding with the Arduino, so I was wondering if someone could take a look at what I have and see if there is something I am missing.


// Voltage Input to Arduino
const int analogInPin1 = A0;   
const int analogInPin2 = A1;
const int analogInPin3 = A2;
const int analogInPin4 = A3;
const int analogInPin5 = A4;
const int analogInPin6 = A5;

// Assigning the Ouputs to the Relays
const int R1 = 0;         // Relay 1 
const int R2 = 1;         // Relay 2
const int R3 = 2;         // Relay 3
const int R4 = 3;         // Relay 4
const int R5 = 4;         // Relay 5
const int R6 = 5;         // Relay 6
const int R7 = 6;         // Relay 7
const int R8 = 7;         // Relay 8
const int R9 = 8;         // Relay 9
const int R10 = 9;        // Relay 10
const int R11 = 10;       // Relay 11
const int R12 = 11;       // Relay 12

// Initial States of Inputs/Outputs & Establishing the SetPoint
int sensorValue1 = 0;         // Value Read From Input A0
int sensorValue2 = 0;         // Value Read From Input A1
int sensorValue3 = 0;         // Value Read From Input A2
int sensorValue4 = 0;         // Value Read From Input A3
int sensorValue5 = 0;         // Value Read From Input A4
int sensorValue6 = 0;         // Value Read From Input A5

int setPoint = 300;           // Initial value of setPoint

int R1State = 0;              // Store State of Relay 1
int R2State = 0;              // Store State of Relay 2
int R3State = 0;              // Store State of Relay 3
int R4State = 0;              // Store State of Relay 4
int R5State = 0;              // Store State of Relay 5
int R6State = 0;              // Store State of Relay 6
int R7State = 0;              // Store State of Relay 7
int R8State = 0;              // Store State of Relay 8
int R9State = 0;              // Store State of Relay 9
int R10State = 0;             // Store State of Relay 10
int R11State = 0;             // Store State of Relay 11
int R12State = 0;             // Store State of Relay 12

int voltage = 0;

void setup() {
  // configure hardware
  pinMode(R1, OUTPUT);      // Set Relay 1 as an Output
  pinMode(R2, OUTPUT);      // Set Relay 2 as an Output
  pinMode(R3, OUTPUT);      // Set Relay 3 as an Output
  pinMode(R4, OUTPUT);      // Set Relay 4 as an Output
  pinMode(R5, OUTPUT);      // Set Relay 5 as an Output
  pinMode(R6, OUTPUT);      // Set Relay 6 as an Output
  pinMode(R7, OUTPUT);      // Set Relay 7 as an Output
  pinMode(R8, OUTPUT);      // Set Relay 8 as an Output
  pinMode(R9, OUTPUT);      // Set Relay 9 as an Output
  pinMode(R10, OUTPUT);     // Set Relay 10 as an Output
  pinMode(R11, OUTPUT);     // Set Relay 11 as an Output
  pinMode(R12, OUTPUT);     // Set Relay 12 as an Output

    // read the analog in value
  sensorValue1 = analogRead(analogInPin1);
  voltage = sensorValue1 * 5.0/1023;  
  sensorValue2 = analogRead(analogInPin2);
  sensorValue3 = analogRead(analogInPin3);
  sensorValue4 = analogRead(analogInPin4);
  sensorValue5 = analogRead(analogInPin5); 
  sensorValue6 = analogRead(analogInPin6);
}

void loop() {

  // Check Voltage Level For Relays 1 & 2
  if (sensorValue1 > setPoint)
    R1State = 1;   
  else
    R1State = 0;
  
  if (sensorValue1 > setPoint)
  R2State = 1;
  else
  R2State = 0;


  // Check Voltage Level for Relays 3 & 4
  if (sensorValue2 > setPoint)
  R3State = 1;
  else
  R3State = 0;
  
  if (sensorValue2 > setPoint)
  R4State = 1;
  else
  R4State = 0;

  // Check Voltage Levels for Relays 5 & 6
  if (sensorValue3 > setPoint)
  R5State = 1;
  else
  R5State = 0;

  if (sensorValue3 > setPoint)
  R6State = 1;
  else
  R6State = 0;
  
  // Check Voltage Levels for Relays 7 & 8
  if (sensorValue4 > setPoint)
  R7State = 1;
  else
  R7State = 0;

  if (sensorValue4 > setPoint)
  R8State = 1;
  else
  R8State = 0;

  // Check Voltage Levels for Relays 9 & 10 (SECONDARY)
  if (sensorValue5 > setPoint)
  R9State = 1;
  else
  R9State = 0;

  if (sensorValue5 > setPoint)
  R10State = 1;
  else
  R10State = 0;

  // Check Voltage Levels for Relays 11 & 12 (SECONDARY)
  if (sensorValue6 > setPoint)
  R11State = 1;
  else
  R11State = 0;

  if (sensorValue6 > setPoint)
  R11State = 1;
  else
  R11State = 0;

  // wait 100 milliseconds before the next loop
  delay(1000);                     
}

Code tags when posting here for a start

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

1 Like

I suggest You to develop and debug the code without using D0 and D1, as a beginning.
That way You can use Serial.print of the analog values as well as Serial.print of the digital pin action, using Serial monitor in the IDE.
When that works, let A0 and D0, D1 be activated.

How do You expect loop to handle changes? You don't read the inputs in loop, only once at start up, in setup.

Note that you are only reading the analog inputs once in setup, so the values are never going to change in loop until you hit the reset button.

So, I should put it in the void setup?

You cannot use digital pins 0 and 1 if you have Serial.begin() in setup(). Those pins are reserved for serial communications (usually, the serial monitor).

setup() runs only once. The loop function loops, as its name implies. So, these statements belong in loop():

sensorValue1 = analogRead(analogInPin1);
sensorValue2 = analogRead(analogInPin2);
sensorValue3 = analogRead(analogInPin3);
sensorValue4 = analogRead(analogInPin4);
sensorValue5 = analogRead(analogInPin5);
sensorValue6 = analogRead(analogInPin6);

Please use code tags when posting code, so it looks like the above. You can edit your original post to add them (select code, use editor button "</>").

Could I re-assign the D0 and D1 for unused pins like D12 and D13?

Use any available digital pins for the relays. The order and name you give them does not matter, as long as the wiring is correct.

Please edit your post to add code tags.

An excellent idea!

The UNO only has a 10 bit ADC so the maximum value is 1023. With a set point of 1000 you will be operating at the end of the range.
You can also group statements {} in the if tests and eliminate half your if statements - for example.

// Check Voltage Level for Relays 3 & 4

if (sensorValue2 > setPoint) {
   R3State = 1;
   R4State = 1;
}
else {
   R3State = 0;
   R4State = 0;
}

I would strongly recommend you study up on arrays and structs.

Your code will shrink by more than 50%, and will be infinitely more readable and maintainable.

I agree, but I don't have that kind of time right now. It's crunch time for me right now and I just need to get this to work.

I'm only getting negative voltage coming from the outputs.

I tried this. It didn't work.

How do You manage to get that? It's not possible, no way.

You tried which one of the replies?

Tried moving the outputs to 12 and 13. Then, I tried just removing the serial 9600 because I'm not doing any printing and it still didn't work.

Arrays would make your code smaller and faster to work with, its a poor decision to postpone such a drastic improvement in productivity and code quality. The huge amount of replication in your code means any bugs could be in many copies, all of which have to be fixed once discovered.

This is my take:

// Voltage Input to Arduino

const byte analog_pins[] = {A0, A1, A2, A3, A4, A5} ;

const analog_pin_count = sizeof (analog_pins) / sizeof (byte) ;


// Assigning the Ouputs to the Relays
const byte relay_pins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13} ;

const relay_count = sizeof (relay_pins) / sizeof (byte) ;

// Initial States of Inputs/Outputs & Establishing the SetPoint

int analog_values [analog_pin_count] ;

int setPoint = 300;           // Initial value of setPoint

int states [relay_count] ;

int voltage = 0;

void setup()
{
  // configure hardware
  for (byte i = 0 ; i < relay_count ; i++)
    pinMode(relays[i], OUTPUT);      // Set Relay as an Output
}


void loop() {
    // read the analog in value
  for (byte i = 0 ; i < analog_pin_count ; i++)
    analog_values[i] = analogRead (analog_pins[i]) ;

  voltage = analog_values[0] * 5.0/1024;

  // Check Voltage Level For Relays 1 & 2, 3 & 4, etc in pairs
  for (byte i = 0 ; i < analog_pin_count ; i++)
  {
    for (byte j = 0 ; j < 2 ; j++)
      states[2*i+j] = analog_values[i] > setPoint ;
  }

  // you forgot to write the states out
  for (byte i = 0 ; i < relay_count ; i++)
    digitalWrite (relays[i], states[i]) ;

  // wait 100 milliseconds before the next loop 
  delay(1000);                     
}

Not sure what the variable called voltage is for...

Now You are running around like a chicken having the head chopped off. Calm down.
Having a Serial.begin and some Serial.prints, and Serial monitor activated, doesn't change anything. That is a tool to find out what the code is doing, from inside.

Have You practised what reply #4 and reply #5 says?