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

ok I admit I have not been giving you up to date info but I have on side schematics with me, note i did not put nrf24 on the schematic because the software which i used didn't supply it, but the nrf24 is not the problem because i know it works so here it is.

Please. Put down the mouse and slowly walk away from the Fritzing program.

Pick up a pen and paper, and draw your circuit. Label everything.

Your inputs are "floating" when not pressed, will pick up random noise instead of switch actuations.

Probably, you too, would see that if it were a real drawing...

Also you show pull downs, not pull ups... the resistors are connected to ground.

Please elaborate?

I had issued before with nrf24 and it didn't work then but since then it started to work. And before i put anything together i tried out a simple "Hello World" code from online. And it worked so i know there is on connection problem there.

Do you know why?

Is there any even slight difference between the program that only says, "Hello World" and the one you wrote (I think you imply)?

Use the divide and conquer methodology or you will keep on fishing in the big picture for a long time or forever.

...and where is the schematic?

yes there was a mistake in the code mostly and you need a capacitor to the nrf

i am drawing it up right now

1 Like

Perfect. Bed time in my time zone.

yes i took some parts out of that code but I mostly wrote it i just used it as a reference.

give me 5 minutes its 11 at my time zone

Please, along with the most recent code, that is running on the same hardware shown in the forthcoming diagram. To be sure of the specific code action on the specific hardware detail.

Edit - also I interpreted the Fritzing wrong. Actually if you follow A2 and A3 they go directly to 5V. The effect of that when A2 and A3 are inputs, is you will read a logical "1" there always.

In fact there is no hurry for you to post, take some time on it.

ok finally made the transmitter not the prettiest but it will get the job done.

#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 = false;//adding a boolean to the code to make it easier to write.
boolean buttonState2 = false;
const byte address[6] = "00001"; //constant address. THIS NEEDS TO ME THE SAME ON BOTH ENDS.

void setup() {
  Serial.begin(9600);
  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); //declaring the wiring type for both push buttons. FYI: You need 2 10k resitors.
  pinMode(pushButtonPin2,INPUT);
}

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

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

here is the receiver code

#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 =true;
//int buttonPushed2 =false;
int buttonarr[2] = {0, 0}; 
int rot = 0;
int add = 180;
RF24 radio(9, 10); // CE, CSN

const byte address[6] = "00001";
boolean buttonState1 = 0;
boolean buttonState2 = 0;
bool boolarr[2] = {0, 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(&boolarr[0], sizeof(boolarr[1]));
   if (boolarr[0] == HIGH) {
    Serial.println(" Recived This is button 1");
    rot = rot + add;
   myServo1.write(rot);
   radio.stopListening();
   }
   
  radio.startListening();
   radio.read(&boolarr[1], sizeof(boolarr[1]));
   if (boolarr[1] == HIGH){
     Serial.println("This is button 2");
     rot = rot + add;
     myServo2.write(rot);
   }
  }
}

yeh the point was to give the A2 and A3 5v so that it will send. but i got the transmitter working on receiver.

If they're connected directly to 5V they will send continuously. However, the circuit shown is a correct pull down circuit.

Have you tried running a separate, simplified sketch that just samples the pins and sends the state to serial for example?

oh no they arn't connected directly only it will send 5v if the button is pressed. The button will connect the 5v line to the A2 and A3 lines.

ok i got the receiver


schematic ready here it is.

so what do you think?

So, to elaborate on something I said earlier, is it the case that you are monitoring only the entire pipeline? That always leaves open the possibility that either one or the other of the MCU's is responsible for the problem. In that case, you could spend eternity probing one of them and it would never do a thing because actually the problem was in the other unit.

You said, something about switch operation. Begin at the switch and verify only one stage at a time. This can be done with simple test sketches. It's important to isolate this kind of testing from the main program.

That shows the value of Fritzing. The drawing was correct.

yes i tried many different serial test and isolated the problem to the second microcontroller. And it was a code issue.