Hmm...Schematic and part number list please? When I have used a relays with a linear actuator, I used 2/4 on an opto isolated (so there's no electrical connection between the Arduino and relays) relay board like this
I like to use ATX PC Power supplies converted to project power supplies (it's much easier than you think, I promise), here's a guide to do exactly that:
https://www.dimensionengineering.com/appnotes/atxguide/atxguide.htm
In the application I'm thinking of that I made closest to what you're doing, I used a linear actuator to open and close a Hallowe'en treasure chest based on the input of 1/9 infrared "magic" wands. Since you have just a pushbutton it seems, it will be simpler.
Two things:
If that's a pushbutton, do you have a pulldown resistor? If not, you pin is floating (the Arduino doesn't know what HIGH or LOW look like in your project.
Also, you have no button debouncing. You should have that.
If you don't feel like adding a pulldown resistor, no problem, Arduino has your back.
Simply change this
to this
pinMode(2, INPUT_PULLUP); // also, for Pete's sake, name your pin assignments, man!
and invert your program logic (so if (switchState == LOW) {
will be
if (switchState == HIGH) {
and vice versa.
About that code I had, give it a look (and note how I posted it in the forum correctly, using code tags - that's the culture here) and see if it helps you figure stuff out. Oh, note that in this sketch rxChar
is literally a char read in from the central controller Arduino to activate various functions.
/*
+5V from arduino to VCC beside the input pins on relay module (for optoisolators)
pin 7 (orange), pin 6 (yellow). GND to chassis ground
relay module +12v from psu through buck converter to relay output 3 NC (ground) and relay output 4 NO (+12v)
jumpers from relay 3 NC to relay 4 NC and from relay 3 NO to relay 4 NO.
Relay 3 common to red motor wire, relay 4 common to black motor wire.
Jumper for JD-VCC/VCC removed (to enable optoisolation and so arduino doesn't have to power the module
electromagnetic coils. JD-VCC powered by PSU +5V rail
NB, these relays are active LOW, so LOW turns them on.
NO for devices that you want to switch on that are normally off, NC for devices that are usually on that
you might want to interrupt.
*/
char rxChar;
boolean newData = false;
const int forwards = 7; // pin for relay board
const int backwards = 6; // pin for relay board
const int readyLed = LED_BUILTIN;
void setup() {
pinMode(forwards, OUTPUT);//set relay as an output
pinMode(backwards, OUTPUT);//set relay as an output
pinMode(readyLed, OUTPUT);
digitalWrite(readyLed, LOW);
Serial.begin(115200);
}
void loop() {
recvOneChar();
showNewData();
switch (rxChar) {
************ full open-hold(some duration)-close functions ******
case ('b'):
normalLong();
break;
case ('o'):
normal();
break;
case ('p'):
halfShort();
break;
case ('u'):
halfMilli();
break;
case ('v'):
quarterShort();
break;
case ('w'):
quarterMilli();
break;
case ('x'):
eightShort();
break;
case ('y'):
eightTiny();
break;
case ('z'):
eightTease();
break;
/************ end full functions ***********************************/
/************ core functions/discrete operations ********************/
case ('s'):
stopAct();
break;
case ('h'):
holdAct();
break;
case ('f'):
fullExtend();
case ('e'):
extend();
break;
case ('a'):
halfE();
break;
case ('t'):
quarterE();
break;
case ('c'):
fullRetract();
break;
case ('r'):
retract();
break;
case ('l'):
halfR();
case ('d'):
quarterR();
break;
case ('k'):
holdLong();
break;
}
/******************** end discrete operations ************************/
/******************** end switch/case *********************************/
digitalWrite(readyLed, LOW);
}
/******************** end void loop(){} *******************************/
void recvOneChar() {
if (Serial.available() > 0) {
rxChar = Serial.read();
newData = true;
}
}
void showNewData() {
if (newData == true) {
newData = false;
}
}
/************************** COMBINED FUNCTIONS *********************/
void normalLong() { // 1 minute
extend();
holdLong();
retract();
}
void normal() { // hold 30 sec
extend();
holdAct();
retract();
}
void halfShort() { // hold 3 sec
halfE();
stopAct();
halfR();
}
void halfMilli() { // hold 1.6
halfE();
milliStop();
halfR();
}
void quarterShort() { // hold 3
quarterE();
stopAct();
quarterR();
}
void quarterMilli() { // hold 1.6
quarterE();
milliStop();
quarterR();
}
void eightShort() { // hold 3
eightOpen();
stopAct();
eightClose();
}
void eightTiny() { // hold 1.6
eightOpen();
milliStop();
eightClose();
}
void eightTease() { // hold 0.8
eightOpen();
microStop();
eightClose();
}
/************************* DISCRETE FUNCTIONS *******************************/
Activate the relay one direction, they must be different to move the motor
this particular actuator takes 20 seconds each way
/************************* OPENING FUNCTIONS *********************************/
void fullExtend() {
digitalWrite(backwards, HIGH);
digitalWrite(forwards, LOW);
digitalWrite(readyLed, HIGH);
delay(22000);
}
void extend() {
digitalWrite(backwards, HIGH);
digitalWrite(forwards, LOW);
digitalWrite(readyLed, HIGH);
delay(20000);
}
void halfE() {
digitalWrite(backwards, HIGH);
digitalWrite(forwards, LOW);
digitalWrite(readyLed, HIGH);
delay(10000);
}
void quarterE() {
digitalWrite(backwards, HIGH);
digitalWrite(forwards, LOW);
digitalWrite(readyLed, HIGH);
delay(5000);
}
void eightOpen() {
digitalWrite(backwards, HIGH);
digitalWrite(forwards, LOW);
digitalWrite(readyLed, HIGH);
delay(2500);
}
/************************** CLOSING FUNCTIONS ************************************/
void fullRetract() {
digitalWrite(backwards, LOW);
digitalWrite(forwards, HIGH);
digitalWrite(readyLed, HIGH);
delay(22000);
}
void retract() {
digitalWrite(backwards, LOW);
digitalWrite(forwards, HIGH);
digitalWrite(readyLed, HIGH);
delay(20000);
}
void halfR() {
digitalWrite(backwards, LOW);
digitalWrite(forwards, HIGH);
digitalWrite(readyLed, HIGH);
delay(10500);
}
void quarterR() {
digitalWrite(backwards, LOW);
digitalWrite(forwards, HIGH);
digitalWrite(readyLed, HIGH);
delay(5500);
}
void eightClose() {
digitalWrite(backwards, LOW);
digitalWrite(forwards, HIGH);
digitalWrite(readyLed, HIGH);
delay(2500);
}
/******************************** STOP/HOLD FUNCTIONS *******************************/
void stopAct() {
digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
digitalWrite(readyLed, HIGH);
delay(3000);
}
void milliStop() {
digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
digitalWrite(readyLed, HIGH);
delay(1600);
}
void microStop() {
digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
digitalWrite(readyLed, HIGH);
delay(800);
}
void holdAct() {
digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
digitalWrite(readyLed, HIGH);
delay(30000);
}
void holdLong() {
digitalWrite(forwards, HIGH);
digitalWrite(backwards, HIGH);//Deactivate both relays to brake the motor
digitalWrite(readyLed, HIGH);
delay(60000);
}
Final note: Serial char handling method courtesy of the legendary @Robin2, one of the real gurus here in the Arduino forum. The following page is worth learning from if Serial comms in Arduino is a bit of a mystery to you:
Good luck