Combined Code with Circuit help

Greetings Community, I am working on school project, where the objective is control a motor. I have designed my code too allow me to initiate the motor for a set duration and end by the press of a bottom. I plan to update this in the near future to have the same button also be a kill switch incase i need to turn the motor off instantly as well as incorporate an lcd counter for the amount of cycles the motor has preformed. My main concern is I am not very electrical inclined, a mechanical background and I have created a circuit online before I go ahead and wire this up. Will this work?

int switch1 = 7;
int relay1 = 8;
int switch1State = LOW;

void setup() {
// put your setup code here, to run once:


void loop() {

// put your main code here, to run repeatedly:
if(switch1State == HIGH)
digitalWrite(relay1, HIGH);


Possibly OK...

BUT please test that switch! Many of those are really only one pole (switch) and if so the diagram you have will short out the power supply when you push it...

Alright thanks for the tip, these (refer to image attached) came with my starter kit. I can search around my lab possibly for a rocker switch and replace it with that. Anyways Thank you very much Ile post an update when i get around to building it this week.

No pull up or pull down resistor?

When the switch is NOT pushed, then the Arduino pin isn't connected to anything (floating).
Then the wires to the pin act like an aerial, picking up static, and the pin could 'float' to any state (high or low).
To stop that, we use a high value (10k) pull up or pull down resistor to 5volt or ground.

Fortunately the Arduino has build-in pull up resistors, so we don't need external ones.
They can be enabled in code, like this, in setup().

pinMode(switch1, INPUT_PULLUP); // enable internal pull up resistor

Now the switch must be connected the opposite direction of the pull UP, so between pin and GROUND.

We also need a small change in the sketch.

if(switch1State == HIGH)


if(switch1State == LOW) // if switch is pushed (connected to ground)