controlling servo with relay input

Howzit

Hope you’re all well in these trying times?

I’m new to Arduino and was hoping I could get some help with my project cos i’m just confusing myself more everytime i try a different approach to the code.

So here’s the deal…
Components
2 button remote and a receiver with a relay attached to it
Arduino Uno board
Servo

Desired outcome
At rest the relay should be open
If button A pressed, the relay should close and the servo should turn x degrees
If button B pressed, relay should return to open and servo rotate back to original position

Current situation
I’ve gone through so many different attempts that i don’t even know what’s happening regarding the code, but my test outputs are giving incorrect results. The input from the relay is changing everytime the loop runs without me even pressing any buttons. So i’m confident my code is a complete MESS.

I’ve set the relay input to Digital Pins 2 & 3, the Servo is hooked up to Dpin 9.

So, would anyone be able to help me get to some working code please? Is it even possible to do what i’m trying to with the bare minimum components i’ve got listed here?

Thanks for your time.

Regards,
Ivor

Image from Original Post so we don't have to download it. See this Simple Image Posting Guide

...R

ivordbijl:
I’ve gone through so many different attempts that i don’t even know what’s happening regarding the code, but my test outputs are giving incorrect results.

Please post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. Then we can focus on the parts you need help with rather than wasting time on things that you can do.

Please use the code button </> when posting code.

…R

What i’d like to do:
Start with the servo in the closed position. at anytime the user can open the gate by pressing button A. It should remain open until button B is pressed where the input triggers the servo to move to the closed position.

I hope that’s clear? The current code is below.

Thanks…

</>
/**
Note: Only pin 2 and 3 accepts interupts on arduino Uno/Nano that why they are used for this application
**/
#include <Servo.h>
const int buttonAPin = 2; // Button A on remote will be used to open gate
const int buttonBPin = 3; // Button B on remote will be used to close gate
const int servoPin = 9; // Pin for controlling servo to open/close gate
Servo gateMotor; // Variable for our servo object

void setup() {
Serial.begin(9600);
// initialize the pushbutton pin as an input:
pinMode(buttonAPin, INPUT);
pinMode(buttonBPin, INPUT);

// Attach an interrupt to the ISR vector
attachInterrupt(0, pin_ISR, CHANGE);

// Attaches the servo on servoPin to the servo object
gateMotor.attach(servoPin);

}

void loop() {
pin_ISR();
}

void openGate(){
//print (openGate);
int pos;
for (pos = 0; pos <= 90; pos += 1)
{
gateMotor.write(pos);
//delay(15);
}
}

void closeGate(){
//print (closeGate);
int pos;
for (pos = 90; pos >= 0; pos -= 1)
{
gateMotor.write(pos);
//delay(15);
}
}

void pin_ISR() {
if(digitalRead(buttonAPin) == 1){openGate();}
if(digitalRead(buttonBPin) == 1){closeGate();}
}
</>

For R

ivordbijl:
I hope that’s clear? The current code is below.

You have not told us what the current code actually does. Knowing that is an important first step before suggesting changes.

Also, please use the code button </> when posting code

so your
code looks like
this

…R

ivordbijl:
Desired outcome
At rest the relay should be open
If button A pressed, the relay should close and the servo should turn x degrees
If button B pressed, relay should return to open and servo rotate back to original position

You can combine two the following code: