Hi there, I am new to this and am a little stuck. I am following this YouTube tutorial for a year 12 project:
for this project I am trying to move a servo to push something wirelessly. I have set up the project exactly the same and it works fine (apart from the servo vibrating). the servo used in the video is too small so I have attached a MG995 servo. now that I have done this the servo continuously goes back and forth hitting the max degrees the servo can go, when I push the button it goes to one side like it's supposed to but when I release the button, it goes back to going back and forth. I want the servo to stay in one position until i press the button, can anybody let me know what to do or how to make it works. Here is the link to the code if that's helpful:
thanks ![]()
receiver code (which controls the servo):
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h> // include the Servo library
Servo myServo1;
Servo myServo2;
int data[2];
RF24 radio(9,10);//check your pin number on RF24 github check you have the right
//pin number for the arduino you're using. this pin is diffrent for diffrent arduino models.
const uint64_t pipe = 0xF0F0F0F0D2L;
void setup()
{
myServo1.attach(5);
myServo2.attach(6);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1, pipe);
radio.startListening();
}
void loop(){
if(radio.available()){
bool done = false;
while (!done) {
done = radio.read(data,sizeof(data));
if (data[1] == 170) {
myServo1.write(data[1]);
}else{
myServo1.write(20);
}
if (data[0] == 170) {
myServo2.write(data[0]);
}else{
myServo2.write(20);
}
}
Serial.println(data[0]);
Serial.println(data[1]);
}
}
transmitter code if needed:
#include <SPI.h>
#include <RF24.h>
#include <nRF24L01.h>
int data[2];
RF24 radio(9,10);//check your pin number on RF24 github check you have the right
//pin for the arduino you're using. this pin number is diffrent for diffrent arduino models.
const uint64_t pipe = 0xF0F0F0F0D2L;
int buttonPin1 = A1;
int buttonPin2 = A2;
int buttonState1 = 0;
int buttonState2 = 0;
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop()
{
buttonState1 = digitalRead(buttonPin1);
if (buttonState1 == HIGH)
{
data[1] = 170;
radio.write(data, sizeof(data));
}
if(buttonState1 == LOW)
{
data[1] = 20;
radio.write(data, sizeof(data));
}
buttonState2 = digitalRead(buttonPin2);
if (buttonState2 == HIGH)
{
data[0] = 170;
radio.write(data, sizeof(data));
}
if (buttonState2 == LOW)
{
data[0] = 20;
radio.write(data, sizeof(data));
}
Serial.println(data[0]);
Serial.println(data[1]);
}