Code not working as intended

This is my first Arduino code.

const int sw1Pin = 22; // switch to prime program
const int pump1Pin = 24; // the number pin output to pump1

int sw1State = 0;

void setup() {

pinMode (pump1Pin, OUTPUT);
pinMode (sw1Pin, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
sw1State = digitalRead(sw1Pin);
if (sw1State == HIGH) {
digitalWrite(pump1Pin, HIGH); // turn the pump on (HIGH is the voltage level)
delay(1000); // wait for a 5 mintues
digitalWrite(pump1Pin, LOW); // turn the pump off by making the voltage LOW
delay(1000); // wait for 23 hours and 55 mintues
} else {
digitalWrite (pump1Pin, LOW);
}
}

The problem is when i give the input a momentary high I get a 0101 on the output but then it just goes to zero. I want this switching to go on indefinitely. I tried removing the else statement, but it didn’t really help anything.

This is my first post, I’m new to the forum. thanks. 8)

It goes to 0 when you release the button but you want it to keep going? Is that what you’re saying?

When you say output, are you talking about the pump? It’s going on and off is the 1 and 0?

I want the input to get a 5v pulse from a momentary NO switch.
I want this to start the pump/ouput( a relay) I want the pump to turn on for 5 min and turn off for 24 hours. I changed it to delay(1000) to see if the program would cycle on and off every day. It worked when the code looked like this before i tried to integrate the input:

// pressing reset results in a 5 min output to dc pump through pin 22, pressing reset will result in the system activating at the same time the button was pressed the next day.
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin as an output for DC pump.
pinMode(22, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(22, HIGH); // turn the pump on (HIGH is the voltage level)
delay(300000); // wait for a 5 mintues
digitalWrite(22, LOW); // turn the pump off by making the voltage LOW
delay(86100000); // wait for 23 hours and 55 mintues
}

OK, so you have the code to turn the pump inside an if that tests if the input pin is HIGH. If the input pin is LOW then you won't run that code.

So instead of running the pump on and off when the pin is HIGH, set a boolean flag when the pin is HIGH and run the pump when that flag is set.

boolean flag = false;

void loop(){

if (digitalRead(pin) == HIGH){
    flag = true;
}

if (flag == true){
  // do the pump thing
}

That way when you release the button the flag is still high and the pump keeps doing its thing.

Ok I will attempt to implement that. Thanks for steering me in the right direction. I will report back on my progress.

I changed the code to this:

const int sw1Pin = 22; // switch to prime program
const int pump1Pin = 24; // the number pin output to pump1

int sw1State = 0;

void setup() {

pinMode (pump1Pin, OUTPUT);
pinMode (sw1Pin, INPUT);
}
boolean flag = false;

void loop() {
if (digitalRead(22) == HIGH){
flag = true;

}
if (flag == true){
digitalWrite(pump1Pin, HIGH); // turn the pump on (HIGH is the voltage level)
delay(1000); // wait for a 5 mintues
digitalWrite(pump1Pin, LOW); // turn the pump off by making the voltage LOW
delay(1000); // wait for 23 hours and 55 mintues
}
}

The output goes high 4 times then stops.

How are things wired? Powered? It sounds like you may be pulling too much current and resetting the board.

5v to switch to input (22)
(24) to relay.

I noticed the relay activates when I hover my hand over the switch. I think this switch might be doing something. Im going to try a different switch.

Do you have a pulldown resistor on the pin with the switch?

It is more common to wire the switch so that it connects the pin to ground when pressed. That way instead of a pulldown you can use the builtin pullup resistors with pinMode(pin, INPUT_PULLUP) That way a pressed button reads LOW and not pressed reads HIGH.

If you don't have a pulldown then the pin is floating when the switch isn't made and picking up whatever stray noise might be around.

Delta_G:
If you don’t have a pulldown then the pin is floating when the switch isn’t made and picking up whatever stray noise might be around.

That explains the activation with hovering hand, but not the behaviour a post or two earlier where OP says it activates 4 times. Once the flag is true, it will stay true regardless of the switch since there’s no code to reset the flag, so the activation will be indefinite.

OP you need to put some Serial.println() in, to print out the state of the switch and the flag maybe, and also to show when you’re in setup() to see if the board is resetting as suggested earlier.

Yeah. I'm thinking we have two separate issues here. The switch and the power.