High/Low reversed on Arduino Uno

Hello! I am having trouble with my code where the high and lows are reversed. I am not a coder by any means but have made this code through the help of the internet and other forum posts on here. This code was created to control an 8-relay device. Right now, I have everything working as it should, but everything is reversed. I would like, if possible, for the code to run with the proper HIGH/LOW notions. I have another code written reversely that will work as I want it to. Here is my code (the one that is typed correctly but is reversed):

String chamberChoice; //sets chamberChoice variable (whatever the user types into the text box)
String delayChoice; //sets delayChoice variable (whatever the user types into the text box)


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600); //Starts Serial Communication (the text boxes )
  digitalWrite(2, LOW); //Sets all pins to OFF at the start
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  pinMode(2, OUTPUT); //Sets all pins to outputs. Note that pin 1 is used as an input for serial info, so pin 1 can not be an output
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  Serial.println("How long would you like to set the delay (in miliseconds)?"); //Prints the question on a new line
  while (Serial.available() == 0) {} //Tells the computer to wait until input from user is received
  delayChoice = Serial.readString(); //Sets 'delayChoice' variable equal to user input
  Serial.print("You have set a "); //More words
  Serial.print(delayChoice); //Inputs 'delayChoice' variable set by user back into text box
  Serial.print(" milisecond delay.\n"); //More words, \n signals to end the line
}

void loop() { //Anything under this loop function will repeat forever until the Arduino is reset or loses power.
  int delayTime = delayChoice.toInt(); //Changes 'delayChoice' string variable into integer (basically changes it from "words of numbers" to actual numbers)
  digitalWrite(2, LOW); //Sets all pins to OFF while
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  Serial.println("What chamber would you like to activate?"); //Prints question on a new line
  while (Serial.available() == 0) {} //Tells the computer to wait for user input before any action
  chamberChoice = Serial.readString(); //Turns user input into a variable
  Serial.print("You have selected chamber "); //More words
  Serial.println(chamberChoice); //Prints variable back to user
  ////All of these from here on out determine what pin activates given the users choice.
  if (chamberChoice.equals("2")) { //This one turns on pin 2 for a given amount of time determined in setup.
    digitalWrite(2, HIGH); //Turns on pin 2 and keeps all others off
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    delay(delayTime); //Sets the time to the delayTime variable into the delay
  digitalWrite(2, LOW); //Turns all pins to OFF after the delay
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (chamberChoice.equals("3")) { //This one turns on pin 3 for a given amount of time determined in setup.
    digitalWrite(2, LOW); //Turns on pin 3 and keeps all others off
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    delay(delayTime); //Sets the time to the delayTime variable into the delay
  digitalWrite(2, LOW); //Turns all pins to OFF after the delay
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (chamberChoice.equals("4")) { //This one turns on pin 4 for a given amount of time determined in setup.
    digitalWrite(2, LOW); //Turns on pin 4 and keeps all others off
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    delay(delayTime); //Sets the time to the delayTime variable into the delay
  digitalWrite(2, LOW); //Turns all pins to OFF after the delay
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (chamberChoice.equals("5")) { //This one turns on pin 5 for a given amount of time determined in setup.
    digitalWrite(2, LOW); //Turns on pin 5 and keeps all others off
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    delay(delayTime); //Sets the time to the delayTime variable into the delay
  digitalWrite(2, LOW); //Turns all pins to OFF after the delay
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (chamberChoice.equals("6")) { //This one turns on pin 6 for a given amount of time determined in setup.
    digitalWrite(2, LOW); //Turns on pin 6 and keeps all others off
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    delay(delayTime); //Sets the time to the delayTime variable into the delay
  digitalWrite(2, LOW); //Turns all pins to OFF after the delay
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (chamberChoice.equals("7")) { //This one turns on pin 7 for a given amount of time determined in setup.
    digitalWrite(2, LOW); //Turns on pin 7 and keeps all others off
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(9, LOW);
    delay(delayTime); //Sets the time to the delayTime variable into the delay
  digitalWrite(2, LOW); //Turns all pins to OFF after the delay
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (chamberChoice.equals("8")) { //This one turns on pin 8 for a given amount of time determined in setup.
    digitalWrite(2, LOW); //Turns on pin 8 and keeps all others off
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    delay(delayTime); //Sets the time to the delayTime variable into the delay
  digitalWrite(2, LOW); //Turns all pins to OFF after the delay
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  else if (chamberChoice.equals("9")) { //This one turns on pin 9 for a given amount of time determined in setup.
    digitalWrite(2, LOW); //Turns on pin 9 and keeps all others off
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    delay(delayTime); //Sets the time to the delayTime variable into the delay
  digitalWrite(2, LOW); //Turns all pins to OFF after the delay
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  }
  }

What do you mean by "HIGH and LOW are reversed"?

If you use digitalWrite(4, HIGH); with a pin declared OUTPUT, the pin will by driven HIGH.

Use your multimeter to verify that.

Welcome to the forum

digitalWrite(2, LOW); //Sets all pins to OFF while

The majority of relay boards are active LOW, ie they turn on when their input is LOW and off when it is HIGH

You have a number choices, including

  1. Change the code so that you use HIGH to turn the relays off and LOW to turn them on
  2. #define ON as LOW and OFF as HIGH and use ON or OFF when setting the state of the relays. This will not change how they work but will make what the code is doing more obvious
  3. Change the project wiring to use the Normally Closed contacts to switch the load instead of the Normally Open contacts

Door number 2.

// up top some manifest constants

# define ON LOW
# define OFF HIGH

// then eveywhere else always

  digitalWrite(someRelay, ON);

and if things are exactly wrong, fix it by editing the constants boom:

# define ON HIGH
# define OFF LOW

Play the same trick with LEDs and for inputs from buttons and sensors and stuff.

Next to "off by one", "works backwards" is wat easier to fix.

a7

3 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.