Amazing code to control a servo only with a push button very easy

Hi guys I just made this code myself and work great for me. Using this code you can control 2 servo motors with just 2 push buttons. Hit the push buttons once the servo turn 180 hit it again it comes back to base. This project requires a nrf24 module for more info check out Robin 2's example on how to use nrf24. Also you need 2 push buttons and servos also we will be using a pull up switch. Hope it works for you.

#include <SPI.h>      //make sure you have all of these libraries to use this code. If you dont check out Robin 2's example he has the libraries in arduino forum.
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10); // CE, CSN 
//this delcared the CE and CSN pins from you arduino they can be anything. FYI: Every arduino has different SPI pins check out the table in how to mechantronics videos to find your spi pins.

#define pushButtonPin1 A2 //declaring the scope of the buttons.
#define pushButtonPin2 A3
boolean buttonState1 = 0;//adding a boolean to the code to make it easier to write.
boolean buttonState2 = 0;
const byte address[6] = "00001"; //constant address. THIS NEEDS TO ME THE SAME ON BOTH ENDS.

void setup() {
  radio.begin();      //Begins the radio conection and starts all ends to transmit using the openWritinPipe fuction and set PALevel and stopListening.
  radio.openWritingPipe(address);// sets as a transmitter.
  radio.setPALevel(RF24_PA_MIN); //this is used for how much power is required if the nrf24 modules are further apart then set it to RF24_PA_MAX.
  radio.stopListening();//Tells the arduino that this is not going to be reciveing.
  pinMode(pushButtonPin1,INPUT_PULLUP); //declaring the wiring type for both push buttons. FYI: You need 2 10k resitors.
  pinMode(pushButtonPin2,INPUT_PULLUP);
}

void loop() {
  
 if(digitalRead(pushButtonPin1) == HIGH){//this is for making the statment that if one button is pressed then do this.
   buttonState1 = digitalRead(pushButtonPin1);  //if the button is pressed change it to a boolean so it can be easier for it to be transmitted.
   radio.write(&buttonState1, sizeof(buttonState1));  // this tells the radio to send the button state through the other side.

    if(digitalRead(pushButtonPin2) == HIGH){//this is just a repeat from above but just for the other push button.
     buttonState2 = digitalRead(pushButtonPin2);
     radio.write(&buttonState2, sizeof(buttonState2));
    }
 }
}

also i will be posting the receiver code shortly. After I make the comments.

Please also add your project block diagram and schematic so that new users can follow the assembly of your project.

For boolean variables use true or false, not numeric values.

digitalRead returns an "int", not a "bool".

Why bother reading each pin twice, mere microseconds apart?

could you clarify your question? The boolean is there to make it easier to transmit the code over to the other nrf24 module. Without the boolean there could be issues with transmitting.

also i have some questions about this receiver code. I made the necessary adjustments to the receiver code. The problem for this code is that when I run it and press the buttons it keeps on saying
Button pressed 1
Button Pressed 2
and I only hit button 1 could you give me a insight on why it isn't working?

#include <Servo.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int angle =1;    // initial angle  for servo (beteen 1 and 179)
Servo myServo1;
Servo myServo2;  
int buttonPushed1 =0;
int buttonPushed2 =0;
int rot = 0;
int add = 180;
RF24 radio(9, 10); // CE, CSN

const byte address[6] = "00001";
boolean buttonState1 = 0;
boolean buttonState2 = 0;
void setup() {
  Serial.begin(9600);
  Serial.println("Welcome to the Test Zone!");
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  myServo1.attach(3);  // attaches the servo on pin 3 to the servo object
  myServo1.write(angle);//initial position
  myServo2.attach(5);
  myServo2.write(angle);
}

void loop() {
 if(radio.available()){
   radio.read(&buttonState1, sizeof(buttonState1));
   if (buttonState1 == HIGH) {
    Serial.println("This is button 1");
    rot = rot + add;
   myServo1.write(rot);
   }
   radio.read(&buttonState2, sizeof(buttonState2));
   if (buttonState2 == HIGH){
     Serial.println("This is button 2");
     rot = rot + add;
     myServo2.write(rot);
   }
  }
}

how do i add a block diagram. Could you give me a good web page of software.

You're using INPUT_PULLUP, but also doing things when the input is high. With input pullups and a low-when-pressed button, your button is pushed when the input is LOW.

That' might help.

No, it's not just a repeat. It's nested inside the other if statement. So button 2 will be unresponsive unless button 1 is pressed. Also, unless I'm mistaken, you have no state change logic.

I think your code is somewhat interesting, but it may be premature to call it "amazing".

how do i add a block diagram

If it's not extremely complicated, pen and paper still works. Some time with Google will show you many options for ECAD drawing.

Hi,
I tried removing the pull-up in the code and made my own pull down resistor but i keep getting this error on the other Arduino.
This is button 2

Recived This is button 1

This is button 2

Recived This is button 1

This is button 2

Recived This is button 1

This is button 2

Please don't try to serve word salad. Post a diagram. Pull ups are generally a better option so why are you doing that?

I don't think so, but how about "read the pin once, assign the result to a variable, and test that, and avoid all the unpleasantness and potential hair-loss" ?

1 Like

because if I used a pull up resistor and tried the circuit it wouldn't work right. This is because when the button is not pressed the pull up resistor will make it go to 5v and make the button high? I believe.

You're using INPUT_PULLUP, but also doing things when the input is high. With input pullups and a low-when-pressed button, your button is pushed when the input is LOW.

That' might help.

Input pull up is the norm in industrial/commercial/home electronics for several reasons:

  • it uses fewer parts, the pull up is often available via the processor itself
  • it uses fewer connections, one less
  • it avoids running power to the switches, an opportunity for it to be short circuited to ground somewhere

Inverting the program logic is a very small price to pay for those advantages.

ok I will change to a pull up resistor but @Delta_G When you said to use a array instead I see where you would be going there and its a good idea, but it still doesn't solve the problem when I try to push one button both of my servos move. The idea is that when I push one button one servo moves and when I hit the second button the second servo moves. But as I said that is not happening when I hit one button both of the servo moves. So do you know why this is happening? Also I tried to make it do is hit one push button the servo moves and it worked and when I tried to add that same process to the code again this is happening.

Change to no pull up resistor you mean... :slight_smile:

The way I work... when I have some problem and I find another one, I don't sit and try to figure out whether or whether not it causes the first one. I just fix it first. 9 times out of 10 it does fix the first problem anyway, even if I didn't understand it fully.

When I see strange behaviour, I often don't try to correct it directly. Instead, I try to make the program generally less strange by redesigning it. I think, if I can't find the problem, it means I have created something I don't really understand. When I make something I understand, it often just works, or sometimes reveals the cause of the problem so I can fix it.

So I don't know what is the cause of your servo problem. I'm attracted to problem spaces that are made attractive or at least tidy. I can't deal with 20 problems at once and I think few people can, I think that is really why you can't solve it yourself. You have to back off and learn to write clean, readable, robust code. You do that by incrementally progressing and not glossing over any skills.

By the way, I see that a wiring diagram has been requested and we don't have one yet. Pen and paper will do just fine.

Also, you made your job 10 times more difficult by including the wireless interface. You should try to make it work 100% on a single processor first. Now, you can't be sure if it's the wireless code, or the servo code, or the button code, or the...

1 Like

Yes I know my code is a little messy. I am more focused on trying to make the main problem worked. I have tried every thing that is why I am writing this forum. I have tried no pull up resistor. Still didn't work, I tried pull down too. Didn't work. I have been spending hrs on trying to make my code less and readable. I also have been trying to figure out what is the error. Because I know it is a problem in my code. I tried a serial monitor test and tried removing one servo and push button and when i did that the other one worked good. Vice versa. But when I add it together there is a problem. So I suspect the error is in my code formatting. So I have been trying to remove useless and repetitive parts in my code. Thats why I have been using array lately thinking it will be easier. And as for the diagram My circuitry is really difficult. So to make it as accurate I am trying to get a good software to do it on. because there are lots and lots of wires to connect and servos and stuff. So bare with me.

Thing is, you haven't really brought us up to date with the details of that. Just some words. So whenever you make some changes or improvements, you need to post code and schematics again to show what you did.

My circuitry is really difficult.

That is a reason to make a schematic before you even touch a component.

So bare with me.

Why? You've already been given a ton of slack, and we're moving very slowly here. You can show us that you're serious about solving your problem, by supplying the information we have requested.