How can I make this code more efficient?

The idea is to have a potentiometer drive two sides, and 3 stage levels.

Stage 0: Do nothing, all relays off
Left 1: Relay 1 on
Left 2: Relay 1 and 2 on
Left 3: Relay 1,2 and 3 on

Right 1: Relay 3 on
Right 2: Relay 3 and 4 on
Right 3: Relay 3,4 and 5 on

I am mapping my potentiometer into 7 sections.
0 [All Off] = +/- 50
-1 [Left 1] = -50 to -150
-2 [Left 2] = -150 to -250
-3 [Left 3] = -250 to -350

The right side is the inverse of the left.

The code I have written is as follows, it seems very clunky, but I am not sure how to make it better.

//These haven't been set yet, and are probably not valid, disregard for now
#define RELAY1  2                        
#define RELAY2  7                        
#define RELAY3  8                        
#define RELAY4  10
#define RELAY5  11
#define RELAY6  12
#define KNOB  0


int Signal = 0;

 //Low turns relay on
 //High turns relay off
    
void setup()
{    
// Initialize the Arduino data pins for OUTPUT
  pinMode(RELAY1, OUTPUT);  //  Left  Stage:1
  pinMode(RELAY2, OUTPUT);  //  Left  Stage:2
  pinMode(RELAY3, OUTPUT);  //  Left  Stage:3
  pinMode(RELAY4, OUTPUT);  //  Right  Stage:1
  pinMode(RELAY5, OUTPUT);  //  Right  Stage:2
  pinMode(RELAY6, OUTPUT);  //  Right  Stage:3
  pinMode(KNOB, INPUT);  //  Potentiometer Input
  //pinMode(LEFTPSI, INPUT);
  //pinMode(RIGHTPSI, INPUT);
  

//Start Serial Output
Serial.begin(9600);

//  System Check
//  All Relays "OFF"
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);
digitalWrite(RELAY3, HIGH);
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);


//  Left & Right Level:1
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY4,LOW);
delay(750);

//  Left & Right Level: 2
digitalWrite(RELAY2, LOW);
digitalWrite(RELAY4, LOW);
delay(750);

//  Left & Right Level: 3
digitalWrite(RELAY3, LOW);
digitalWrite(RELAY6, LOW);
delay(750);

//  All Relays "OFF"
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);
digitalWrite(RELAY3, HIGH);
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);

}
 
 void loop()
{
  
int Signal = analogRead(KNOB);
Signal = map(Signal, 0, 10000, -350, 350); //Maps Potentiometer from -300 to +300
Serial.println(Signal);

//  Zero Setting
if((Signal > -50) && (Signal < 50))
{
  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(RELAY4, HIGH);
  digitalWrite(RELAY5, HIGH);
  digitalWrite(RELAY6, HIGH);
  delay(300);
}

//  Right Stage: 1
if((Signal >= 50) && (Signal < 150))
{
  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(RELAY4, LOW);
  digitalWrite(RELAY5, HIGH);
  digitalWrite(RELAY6, HIGH);
  delay(300);
}

//  Right Stage: 2
if((Signal >= 150) && (Signal < 250))
{
  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(RELAY4, LOW);
  digitalWrite(RELAY5, LOW);
  digitalWrite(RELAY6, HIGH);
  delay(300);
}

//  Right Stage: 3
if((Signal >= 250) && (Signal <= 350))
{
  digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(RELAY4, LOW);
  digitalWrite(RELAY5, LOW);
  digitalWrite(RELAY6, LOW);
  delay(300);
}

//  Left Stage:1
if((Signal <= -50) && (Signal > -150))
{
  digitalWrite(RELAY1, LOW);
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(RELAY4, HIGH);
  digitalWrite(RELAY5, HIGH);
  digitalWrite(RELAY6, HIGH);
  delay(300);
}

//  Left Stage:2
if((Signal <= -150) && (Signal > -250))
{
  digitalWrite(RELAY1, LOW);
  digitalWrite(RELAY2, LOW);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(RELAY4, HIGH);
  digitalWrite(RELAY5, HIGH);
  digitalWrite(RELAY6, HIGH);
  delay(300);
}

//  Left Stage:3
if((Signal <= -250) && (Signal >= -350))
{
  digitalWrite(RELAY1, LOW);
  digitalWrite(RELAY2, LOW);
  digitalWrite(RELAY3, LOW);
  digitalWrite(RELAY4, HIGH);
  digitalWrite(RELAY5, HIGH);
  digitalWrite(RELAY6, HIGH);
  delay(300);
}


Thank you!


} // End Main Loop

int Signal = analogRead(KNOB); Signal = map(Signal, 0, 10000, -350, 350); //Maps Potentiometer from -300 to +300

When will analogRead ever return a value greater than 1023?

You can also use arrays to simplify things a bit. Less lines and cleaner to read.

The 10,000 was a placeholder, but yes, it'll be less than that. My mistake.

I have never used arrays before, but did not think they would apply to a situation like this.

Thank you-

If you use 2,3,4,5,6,7 for the relays, writing to PORTD directly will simplify things as long as you do not need portability. For UNO.

byte PortD = 0;

void setup()
{
// sets UNO pins 1 to 7 as outputs, pin 0 (RX) as input
//                 DDDDDDDD 
//                 76543210
   DDRD = DDRD | 0b11111100; 

// . . .  
}

Void loop()
{
. . . 
//                   DDDDDDDD 
//                   76543210
   PORTD = PORTD | 0b11111100  // Make D2-D7 HIGH
   PORTD = PORTD & 0b00000011  // MAKE D2-D7 LOW

   PortD = PORTD & 0b00000010  // keep what is on D1
   PORTD = PortD | 0b10101000  // Digital pins 7,5,3 HIGH
                               // Leaves D1 alone. 
or   
   PortD = PORTD & 0b00000010  // keep what is on D1
   PORTD = 0xA8;               // Digital pins 7,5,3 HIGH
                               // Leaves D1 alone. 


. . .

If you assign your pins in a sequence AKA 1,2, skip 3 ,4,5... you can use for loop

for( starting pin = REALY1 , ending pin = RELAY6 , increment pin ) digitalWrite(pin , HIGH);

And even if you have pin 3 out of sequence ( setting HIGH) and it is not time critical you can just add a code to reset it after the for loop digitalWrite(3, LOW);

Same goes for any sequence of code.

digitalWrite(RELAY1, HIGH);
  digitalWrite(RELAY2, HIGH);
  digitalWrite(RELAY3, HIGH);
  digitalWrite(RELAY4, HIGH);
  digitalWrite(RELAY5, HIGH);
  digitalWrite(RELAY6, HIGH);