Sequential Startup - Code Help

Hello everyone. Im Currently Building a Tube AMplifier (my 6th)

This Time i am using an Arduino for a sequential Startup and Shutdown.
Since i am not really a Programmer i tried to create a Code, it works but i am not happy because the shutdown takes as long as the Startup. Im stuck. I need some Help please.
My Code is self explanatory. Im sure you immediately find what is wrong. I also want to heating up LED to be blinking

There are 5 LED's and 4 Relais
I am using the Arduino Relais Shield with 4 Relais

int switch_1 = 3;
int relay_1 = 4; // HT Relais (600V)
int relay_2 = 7; // Heater Relais 6.3V
int relay_3 = 8; // speaker protection left
int relay_4 = 12; // speaker protection right
int led_1 = 6; // standby LED active (red)
int led_2 = 9; // standby LED off (green)
int led_3 = 10; // heating LED active (want it to blink)
int led_4 = 11; // heating LED finished (green static)
int led_5 = 2; // active on

int buttonState = 0;
int standbyState = 0;
int activeState = 0;
int heaterState = 0;

void setup() {
pinMode(switch_1, INPUT);
pinMode(relay_1, OUTPUT);
pinMode(relay_2, OUTPUT);
pinMode(relay_3, OUTPUT);
pinMode(relay_4, OUTPUT);
pinMode(led_1,OUTPUT);
pinMode(led_2,OUTPUT);
pinMode(led_3,OUTPUT);
pinMode(led_4,OUTPUT);
pinMode(led_5,OUTPUT);

}

void loop() {
buttonState = digitalRead(switch_1);
standbyState = digitalRead(led_1);
heaterState = digitalRead(led_3);
activeState = digitalRead(led_5);

if(digitalRead(switch_1) == HIGH){
digitalWrite(led_1, LOW); // standby LED off
digitalWrite(led_2, HIGH); // activ LED on
digitalWrite(relay_2, HIGH); // heater relais on
digitalWrite(led_3, HIGH); // heater LED on (blinking?)
delay(30000); // wait 30 seconds to heating up
digitalWrite(relay_1, HIGH); // HT Power on (switching on 600V relais)
digitalWrite(led_5, HIGH); // active LED on
delay(100);
digitalWrite(relay_3, HIGH); // speaker protection L on
digitalWrite(relay_4, HIGH); // speaker protection R on

}
else if(digitalRead(switch_1) == LOW){
digitalWrite(relay_3, LOW); // speaker protection L off
digitalWrite(relay_4, LOW); // speaker protection R off
delay(200);
digitalWrite(relay_1, LOW); // HT Power off (switching 600V relais off)
delay(100);
digitalWrite(relay_2, LOW); // heater relais off
digitalWrite(led_4, LOW); // heater LED 1 off
digitalWrite(led_3, LOW); // heater LED 2 off
digitalWrite(led_2, LOW); // active LED off
digitalWrite(led_1, HIGH); // standby LED on
}
}

Thank you kindly for your Help or Input
Randy

Image 1 of the Colossus Tubeamp

Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

Can you put your code in the code format, just use the </> tool

Also, I would strongly advice you against using the delay function to time your code and use the millis() instead.

and what do you mean by this?

not sure how that is possible. there's a 30 sec delay in one case, but not the other

should switch_1 be configured as INPUT_PULLUP?

Although I agree that the use of millis() would be neat, you can replace the delay by a for-loop with the blink example inside it.; let the for-loop run e.g. 15 times.

for(byte cnt=0;cnt<15;cnt++)
{
  digitalWrite(led_3, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(led_3, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Adding to @gcjr's comment about INPUT_PULLUP: or do you have a pull-up or pull-down resistor in place?

One advise
Don't use numbered variables, even though you have commented your code. E.g. led_1 indicates standby so call it e.g. led_Stby, led_2 indicates on so call it led_On and so on. Same applies to the relays.

Nice amp, by the way.

As is whichever state switch_1 is in that code will run continually.  This would only be noticeable in the startup sequence because of the desired blinking.

Create a boolean called modeStarting.  Its initial value will be false.  Incorporate @sterretje's blinker above.

Assume switch_1 is LOW* at power up. Use switch_1 == LOW and modeStarting == false as the condition for the shutdown sequence. When shutdown ends set modeStarting = true.  This will disable the shutdown code.  Startup is also disabled since switch_1 is still LOW.

Now flip switch_1 to HIGH. The condition of switch_1 == HIGH and modeStarting == true enables the startup code. When startup ends set modeStarting back to false.  You're again in a state where neither section of code is executing and things will remain so until switch_1 changes state.

* depends on your wiring scheme.

I think this may be close to what you want.

Note: You are setting relay pins HIGH to turn the relay ON and LOW to turn the relay OFF. In my experience, it is the other way around for most relay modules.

const byte PowerSwitchPin = 3;
const byte RelayPin_600V = 4;
const byte RelayPin_Heater = 7;
const byte RelayPin_SpeakerLeft = 8;
const byte RelayPin_SpeakerRight = 12;
const byte LEDPin_StandbyRed = 6;
const byte LEDPin_StandbyGreen = 9;
const byte LEDPin_HeatingUp = 10;
const byte LEDPin_Heated = 11;
const byte LEDPin_Active = 2;

void setup()
{
  pinMode(PowerSwitchPin, INPUT);
  pinMode(RelayPin_600V, OUTPUT);
  pinMode(RelayPin_Heater, OUTPUT);
  pinMode(RelayPin_SpeakerLeft, OUTPUT);
  pinMode(RelayPin_SpeakerRight, OUTPUT);

  // Start in 'Standby' mode (Red on, Green off)
  digitalWrite(LEDPin_StandbyRed, HIGH);
  digitalWrite(LEDPin_StandbyGreen, LOW);

  pinMode(LEDPin_StandbyRed, OUTPUT);
  pinMode(LEDPin_StandbyGreen, OUTPUT);
  pinMode(LEDPin_HeatingUp, OUTPUT);
  pinMode(LEDPin_Heated, OUTPUT);
  pinMode(LEDPin_Active, OUTPUT);
}

void loop()
{
  // Wait for someone to turn on the power
  while (digitalRead(PowerSwitchPin) == LOW) {}

  // Power is ON.  Go through the start-up procedure.

  digitalWrite(LEDPin_StandbyRed, LOW); // standby LED off
  digitalWrite(LEDPin_StandbyGreen, HIGH); // activ LED on

  digitalWrite(RelayPin_Heater, HIGH); // heater relais on

  // Blink the Heating LED for 30 seconds
  for (int i = 0; i < 30; i++)
  {
    digitalWrite(LEDPin_HeatingUp, HIGH);
    delay(500);
    digitalWrite(LEDPin_HeatingUp, LOW);
    delay(500);
  }
  digitalWrite(LEDPin_Heated, HIGH);

  digitalWrite(RelayPin_600V, HIGH); // Turn on 600V

  delay(100);

  // Enable speakers
  digitalWrite(RelayPin_SpeakerLeft, HIGH); // speaker protection L on
  digitalWrite(RelayPin_SpeakerRight, HIGH); // speaker protection R on

  digitalWrite(LEDPin_Active, HIGH);

  // Wait until the power is turned off
  while  (digitalRead(PowerSwitchPin) == HIGH) {}

  digitalWrite(LEDPin_Active, LOW);

  // Disable speakers
  digitalWrite(RelayPin_SpeakerLeft, LOW);
  digitalWrite(RelayPin_SpeakerRight, LOW);
  delay(200);
  digitalWrite(RelayPin_600V, LOW); //600V off
  delay(100);
  digitalWrite(RelayPin_Heater, LOW);
  digitalWrite(LEDPin_Heated, LOW);

  digitalWrite(LEDPin_StandbyGreen, LOW); // active LED off
  digitalWrite(LEDPin_StandbyRed, HIGH); // standby LED on
}
1 Like

Thank you so much!! I will try this in the next few Days. The code makes sense.