nrf24l01-servo

NRF24L01 ile Uzaktan Servo Motor Kontrolü

Arduino ile en çok zorlandığım konulardan biri kablosuz iletişim ama internette bulduğum ve denediğim bu proje ile korkumun yersiz olduğunu farkettim.

Bu projemizde iki adet arduino ile servo motorun iki buton ile kontrolünü göreceğiz. Butonlardan biri servoyu 0-180 derece arasında hareket ettirirken diğeri ise servoyu durduruyor. Proje geliştirmeye tamamen açık istediğiniz değişiklikleri yaparak istediğiniz cihazı kontrol edebilmeniz mümkün.

 

Kullanılan malzemeler

  1. İki adet Arduino
  2. 2 adet 10Kohm direnç
  3. 2 adet NRF24L01
  4. 2 Adet Buton

 

Devre şeması:

Alıcı:

layout_receive

Verici:

layout_transmit

Programımız

Alıcı

#include <SPI.h>
#include "RF24.h" // This is the RF24 library that may need to be installed through the Manage Libraries feature in the IDE.

#include <Servo.h>//Include and create a servo object for controlling the servo motor
Servo servo;

RF24 radio(9, 10);//Create a commuications object for talking to the NRF24L01
const uint64_t send_pipe=0xB01DFACECEL;//These are just arbitrary 64bit numbers to use as pipe identifiers
const uint64_t recv_pipe=0xDEADBEEFF1L;//They must be the same on both ends of the communciations

//As in the transmit code these are the possible motor codes.
#define CODE_NONE 0
#define CODE_MOTOR_START 1
#define CODE_MOTOR_STOP 2
#define CODE_MOTOR_EXPLODE 3

#define SERVO_PIN 6//This is obviously the pin which tells the servo what to do

//These three variables are for controlling the motor's sweeping back and fourth
bool motor_running=false;
float position=90;
int direction=1;

void setup()
{
  Serial.begin(9600);//Set up comm with the IDE serial monitor
  Serial.println("Ready for commands");
  radio.begin();//Start up the radio object
  radio.setRetries(15,15);//This will improve reliability of the module if it encounters interference
  radio.setPALevel(RF24_PA_LOW);//This sets the power low. This will reduce the range. RF24_PA_MAX would increase the range
  radio.openWritingPipe(send_pipe);//Thses are the reverse of the transmit code.
  radio.openReadingPipe(1,recv_pipe);
  radio.startListening();//Give the module a kick
  servo.attach(SERVO_PIN);//Spool up the servo
}

void loop()
{
  unsigned long motor_code=CODE_NONE;

  if( radio.available())//Keep checking on each loop to see if any data has come in
  {
    while(radio.available())//Loop while there is incoming data. The packets are one unsigned long in total so it shoudl only loop once
    {
      radio.read(&motor_code, sizeof(unsigned long));//Stuff the incoming packet into the motor_code variable
    }
    radio.stopListening();//We have heard so now we will send an ack
    radio.write(&motor_code, sizeof(unsigned long));//Turn the motor code around and send it back
    radio.startListening();//Go back to listening as this is what this mostly does

    //set the motor_running varible depending upon the code received
    if(motor_code==CODE_MOTOR_START)
    {
      Serial.println("THE MOTOR HAS STARTED");
      motor_running=true;
    }
    else if(motor_code==CODE_MOTOR_STOP)
    {
      Serial.println("THE MOTOR HAS STOPPED");
      motor_running=false;
    }
    else if(motor_code==CODE_MOTOR_EXPLODE)
    {
      Serial.println("*** BOOM !!! *** THE MOTOR HAS EXPLODED");
      motor_running=false;
    }
  }
  if(motor_running==true) //If any of the above code set the motor_running to true then, run the motor
  {
    //This just sweeps the motor back and fourth.
    position+=0.4f*(float)direction;
    if(position>170.0f || position<10.0f){direction*=-1;}
    servo.write(position);
    delay(10);
  }
}

Verici

#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10); // Establish a new RF24 object
const uint64_t send_pipe=0xB01DFACECEL;//This will be this device
const uint64_t recv_pipe=0xDEADBEEFF1L;//This will be the other device

#define WHITE_PIN 2
#define RED_PIN 3

void setup()
{
  Serial.begin(9600); // Set up communcations with the serial monitor in the arduino IDE
  
  pinMode(WHITE_PIN, INPUT); // Prepare the pins for input
  pinMode(RED_PIN, INPUT);

  Serial.println("Start");
  radio.begin();// Basically turn on communications with the device
  radio.setPALevel(RF24_PA_LOW);//RF24_PA_MAX is max power
  radio.setRetries(15,15);//This will improve reliability
  radio.openWritingPipe(recv_pipe);//Set up the two way communications with the named device
  radio.openReadingPipe(1,send_pipe);
  
  radio.startListening();// Start listening for data which gives the device a kick
}
//These are the four codes that we will work with, the final three being those that are transmitted
#define CODE_NONE 0
#define CODE_MOTOR_START 1
#define CODE_MOTOR_STOP 2
#define CODE_MOTOR_EXPLODE 3
unsigned long message_code=CODE_NONE;//This is where the code to be sent will reside
void loop()
{
  //Check the status of the two buttons
  int white_pin_status=digitalRead(WHITE_PIN);
  int red_pin_status=digitalRead(RED_PIN);
  
  if(white_pin_status==1 && red_pin_status==1)//If both are pushed then send explode code
  {
    message_code=CODE_MOTOR_EXPLODE;
  }
  else if(white_pin_status==1)//Send start code if white is pushed
  {
    message_code=CODE_MOTOR_START;
  }
  else if(red_pin_status==1)//Send stop if red is pushed
  {
    message_code=CODE_MOTOR_STOP;
  }
  else// If nothing is pushed then, well, do nothing.
  {
    message_code=CODE_NONE;
  }
  if(message_code!=CODE_NONE)//If something is to be done then send the code
  {
    radio.stopListening();//We are sending not listening for now
    if(!radio.write(&message_code, sizeof(unsigned long)))// Send the message_code and check to see if it comes back false
    {
      Serial.println("Failed");
    }

    radio.startListening();//Go back to listening and wait for the ack signal.

    unsigned long started_waiting_at=micros();//This notes the time 
    boolean timeout=false;//Assume for the moment that there is no timeout

    while(!radio.available())//Keep looping while no ack has come in
    {
      if(micros()-started_waiting_at>200000)//If the loop has been going for more than 1/5th of a second then give up
      {
        timeout=true; //Note that it has failed
        break;
      }      
    }
    if(timeout==true)//If the previous looped marked as failure then 
    {
      Serial.println("Timeout");
    }
    else// If it didn't fail to ack then read in and printout the results.
    {
      unsigned long in_data;
      radio.read(&in_data, sizeof(unsigned long));
      Serial.println("In Data: "+String(in_data));
    }
    delay(1000);
  }
 
}


https://github.com/LessonStudio/Arduino_NRF24L01

yada

Bir Cevap Yazın

Time limit is exhausted. Please reload CAPTCHA.