2018/07/15

NRF24L01 遙控裝置


NRF24L01 遙控裝置




使用NRF24L01製作一個遙控裝置

剛好逛到 https://www.thingiverse.com/thing:2993529 ,有這個盒子就可以將前些時候測試的 NRF24L01,找一個家。

剛好可以放進 Arduino UNO板子,僅需修整一下。

有了好用的外盒,測試起來順手多了,於是把舊的程式再拿出來改一改。

發射端程式:

#include <SPI.h>
#include "RF24.h"
RF24 myRadio (7, 8);
const byte addr[] = "1Node";
const byte pipe = 1; // 指定通道編號
struct package
{
int X=512;
int Y=512;
};
typedef struct package Package;
Package data;
void setup()
{
Serial.begin(9600);
delay(100);
myRadio.begin();
myRadio.setChannel(83);
myRadio.openWritingPipe( addr);
myRadio.setPALevel(RF24_PA_MAX);
myRadio.setDataRate( RF24_250KBPS ) ;
delay(100);
}
void loop()
{
// Serial.print("X:");
// Serial.print(data.X);
// Serial.print(" Y");
// Serial.println(data.Y);
data.X = analogRead(A0);
data.Y = analogRead(A1);
myRadio.write(&data, sizeof(data));
delay(100);
}

接收端小車的程式:

#include <SPI.h> 
#include "RF24.h"
RF24 myRadio (7, 8);
boolean Debug_flag = false;
struct package
{
int X=512;
int Y=512;
};
const byte addr[] = "1Node";
const byte pipe = 1; // 指定通道編號
int OUT1 = 5;
int OUT2 = 6;
int OUT3 = 9;
int OUT4 = 10;

typedef struct package Package;
Package data;
void setup()
{
Serial.begin(9600);
myRadio.begin();
myRadio.setChannel(83);
myRadio.setPALevel(RF24_PA_MAX);
myRadio.setDataRate( RF24_250KBPS ) ;
myRadio.openReadingPipe(pipe, addr);
myRadio.startListening();
pinMode(OUT1, OUTPUT);
pinMode(OUT2, OUTPUT);
pinMode(OUT3, OUTPUT);
pinMode(OUT4, OUTPUT);
analogWrite(OUT1, 0);
analogWrite(OUT2, 0);
analogWrite(OUT3, 0);
analogWrite(OUT4, 0);
}

void loop()
{
if ( myRadio.available(&pipe))
{
myRadio.read( &data, sizeof(data) );
if(Debug_flag) {
Serial.print("X:");
Serial.print(data.X);
Serial.print(" Y:");
Serial.println(data.Y);
}
int X = data.X;
int Y = data.Y;
int backward = map(X,519,1023,30,250); // backward
int foward = map(X,519,0,30,255); // foward
int right = map(Y,508,1023,30,200);
int left = map(Y,508,0,30,200);
if(Y < 524 && Y > 500 && X < 519){
analogWrite(OUT4, 0);
analogWrite(OUT3, foward);
analogWrite(OUT2, 0);
analogWrite(OUT1, foward);
}else if(Y < 524 && Y > 500 && X > 519){
analogWrite(OUT4, backward);
analogWrite(OUT3, 0);
analogWrite(OUT2, backward);
analogWrite(OUT1, 0);
}else if(X < 524 && X > 500 && Y < 524 && Y > 500){
analogWrite(OUT4, 0);
analogWrite(OUT3, 0);
analogWrite(OUT2, 0);
analogWrite(OUT1, 0);
}else if(X < 524 && X > 500 && Y < 500){
analogWrite(OUT4, left);
analogWrite(OUT3, 0);
analogWrite(OUT2, 0);
analogWrite(OUT1, left);
}else if(X < 524 && X > 500 && Y > 524){
analogWrite(OUT4, 0);
analogWrite(OUT3, right);
analogWrite(OUT2, right);
analogWrite(OUT1, 0);
}else if(Y < 500 && X < 524){
analogWrite(OUT4, 0);
analogWrite(OUT3, foward-left);
analogWrite(OUT2, 0);
analogWrite(OUT1, foward);
}else if(Y > 508 && X < 524){
analogWrite(OUT4, 0);
analogWrite(OUT3, foward);
analogWrite(OUT2, 0);
analogWrite(OUT1, foward-right);
}else if(X > 519 && Y > 508){
analogWrite(OUT4, backward);
analogWrite(OUT3, 0);
analogWrite(OUT2, backward-right);
analogWrite(OUT1, 0);
}else if(X > 519 && Y < 508){
analogWrite(OUT4, backward-left);
analogWrite(OUT3, 0);
analogWrite(OUT2, backward);
analogWrite(OUT1, 0);
}
delay(100);
}
}

歡迎取回測試,如果有任何指教也請您不吝指教