RF Remote Controlling a Servo Motor at Different Angles

Hello,

I am working on a project that includes an Arduino, a 4 button key fob, and a servo motor. Although i have 4 buttons i only need to use the A,B,C buttons. I am trying to make it so each button have a certain task. For “A” i want it to move 45 degrees, “B” i want it to go to 90 degrees, and “C” return back to 0. Any advise would be helpful.

Thanks

Post details of your keyfob and the corresponding receiver.
Post your program and tell us what it actually does and what it should do that is different.

You question is a bit like "I want to paint my girlfriend's (or boyfriend's) portrait, please advise"

...R

The keyfob is a 4-Button RF Remote Control - 315MHz and a simple RF M4 receiver from adafruit. Right now
my codes are below but i tried to put in another input but it wouldn’t let me. This code allows me to press the A and D button so when pressing A it allows it to move forward and for D allows it to go backwards. I need help adding another button B. Each would move at different positions so i would like A to move at 45 degrees and B move at 90 degrees and C will move back to 0 degrees.

#include <Servo.h>

byte pin = 8;
byte pin2 = 7;
int pos = 90;
Servo myservo;
void setup(){

pinMode(pin, INPUT); // initialize pins
pinMode(pin2, INPUT);

digitalWrite(pin, HIGH); // set internal pull up resistors
digitalWrite(pin2, HIGH);

myservo.attach(9);
}

void loop(){

int val = digitalRead(pin); // get the button values… low is pressed… high is not pressed
int val2 = digitalRead(pin2);

if (val == 0){
if (pos < 180 && pos > 0){

pos+=2;
} if (pos == 180){
pos = 178;
}

if (pos == 0){
pos = 2;
}
}

if (val2 == 0){
if (pos < 180 && pos > 0){

pos-=2;
}
if (pos == 180){
pos = 178;
}

if (pos == 0){
pos = 2;
}

}

myservo.write(pos);
delay(15);
}

studentdo:
This code allows me to press the A and D button so when pressing A it allows it to move forward and for D allows it to go backwards. I need help adding another button B.

I don’t see button A or D or B in that code so how am I supposed to know how to help?

Post a link to the datasheet for your keyfob and receiver.

And please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

…R

Do you see anything wrong with this:

pinMode(pin, INPUT); // initialize pins
pinMode(pin2, INPUT);

digitalWrite(pin, HIGH); // set internal pull up resistors
digitalWrite(pin2, HIGH);

Writing to input pins may be a problem.

Paul

Paul_KD7HB:
Do you see anything wrong with this:

pinMode(pin, INPUT); // initialize pins
pinMode(pin2, INPUT);

digitalWrite(pin, HIGH); // set internal pull up resistors
digitalWrite(pin2, HIGH);

Writing to input pins may be a problem.

Paul

But very useful for enabling pull ups.

AWOL:
But very useful for enabling pull ups.

Ok, I have always set them with initial definition.

Paul

Robin2:
I don’t see button A or D or B in that code so how am I supposed to know how to help?

Post a link to the datasheet for your keyfob and receiver.

And please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

…R

#include <Servo.h>

int  A = 8; // button A input
int  D = 7; // button D input
int  B = 4; // button B input
int  servoPin = 9;

int angle = 90;
int change = 15; // this value determines how much the angle changes each time through the loop

Servo myservo;

void setup(){

 pinMode(A, INPUT);  // initialize pins
 pinMode(D, INPUT);
 pinMode(B, INPUT);
 
 digitalWrite(A, HIGH); // set internal pull up resistors
 digitalWrite(D, HIGH);
 digitalWrite(B, HIGH);
 myservo.attach(servoPin);
}

void loop(){

 if( digitalRead(A) == LOW) { 
   // here if increment switch pressed
   angle = angle + change;
 }
 if( digitalRead(D) == LOW) {
   // here if decrement switch pressed
   angle = angle - change;
 }  
  if( digitalRead(B) == LOW) {
   // here if decrement switch pressed
   angle = angle + change;
 }  
 angle = constrain(angle, 0, 180); // limit value of angle 
 myservo.write(angle);
 delay(20); 
}

I do not have access to the keyfob and receiver datasheet. I can’t seem to get my input button B to work. It works fine with A and D button inputs. Also, how can I make the angles precise and stop the servo motor from locking up so I don’t have to keep resetting it?

studentdo:
I do not have access to the keyfob and receiver datasheet.

There is nothing I can suggest without seeing the datasheet. You, at least, have the advantage of having the hardware to experiment with

Why not write a separate short test program that just detects the different button presses and prints a short message on the Serial Monitor?

And this

int  A = 8; // button A input
int  D = 7; // button D input
int  B = 4; // button B input

is not a good system. Your program will be much easier to understand and debug if you do it like this

int  buttonApin = 8;
int  buttonDpin = 7;
int  buttonBpin = 4;

because then you don't need to write any comments to explain it.

...R