Commit ca11ad07 by satsob

nRF24

parent 3660bce0
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// Include servo
#include <Servo.h>
// Defines
#define right_servo_pin 5 // Define Servo right pin
#define left_servo_pin 6 // Define Servo left pin
// Global variables
Servo g_left_wheel;
Servo g_right_wheel;
// Define slave address and string to save received data
const byte thisSlaveAddress[5] = {'S','t','a','r','t'}; // This is the slave address
RF24 radio(9, 10); // Radio pins for CS, CSN
char dataReceived[10]; // Sent message is stored in this
/**
* Define functions used for the robot to control
*/
// Stop the robot
void setWheels_Stop() {
// g_right_wheel.write(1500);
// g_left_wheel.write(1500);
g_right_wheel.writeMicroseconds(1500);
g_left_wheel.writeMicroseconds(1500);
}
// Drive forward
void setWheels_Forward(){
g_right_wheel.write(1300);
g_left_wheel.write(1700);
}
// drive right
void setWheels_Right(){
g_right_wheel.write(1700);
g_left_wheel.write(1700);
}
// Drive left
void setWheels_Left(){
g_right_wheel.write(1300);
g_left_wheel.write(1300);
}
// Drive backward
void setWheels_Backward(){
g_right_wheel.write(1700);
g_left_wheel.write(1300);
}
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
Serial.println(dataReceived);
}
/*else
Serial.println("Error!");*/
}
void setup() {
/* Start serial monitor */
Serial.begin(9600);
/* Attach servos to digital pins defined earlier */
g_left_wheel.attach(left_servo_pin);
g_right_wheel.attach(right_servo_pin);
/* Put servos to standstill */
//setWheels();
// Start the radio and start listening. Right now Acknowledgement is OFF.
radio.begin();
radio.setAutoAck(true); // Acknowledgement is OFF
radio.setDataRate(RF24_250KBPS); // Sets the data rate for the module, default 250kbps
radio.setRetries(3,5);
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
void loop() {
getData();
if(strcmp(dataReceived, "Forward") == 0) {
//Serial.print("Received\n");
Serial.print("Forward\n");
setWheels_Forward();
delay(100);
}
else if(strcmp(dataReceived, "Left") == 0) {
//Serial.print("Received\n");
Serial.print("Left\n");
setWheels_Left();
delay(100);
}
/*
//else if(dataReceived == "Backward") {
else if(strcmp(dataReceived, "Backward") == 0) {
//Serial.print("Received\n");
Serial.print("Backward\n");
Backward();
delay(100);
}*/
else if(strcmp(dataReceived, "Right") == 0) {
//Serial.print("Received\n");
Serial.print("Right\n");
setWheels_Right();
delay(100);
}
else {
//Serial.print("Stop\n");
setWheels_Stop();
delay(100);
}
strcpy(dataReceived, 0);
}
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
const byte slaveAddress[5] = {'S', 't', 'a', 'r', 't'};
RF24 radio(9, 10);
char rotation[10];
bool sent;
//boolean sendStatus;
//int sendData = 1;
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
void setup() {
Serial.begin(9600);
radio.begin();
radio.setAutoAck(true);
radio.setDataRate(RF24_250KBPS);
radio.setRetries(3, 5); // delay, count
radio.openWritingPipe(slaveAddress);
}
void loop() {
/*sendStatus = radio.write(&sendData, sizeof(int));
if(sendStatus == false) {
Serial.println("Data not sent!");
delay(500);
}*/
if ((analogRead(Y_pin) >= 1020 && analogRead(Y_pin) <= 1025) &&
(analogRead(X_pin) >= 490 && analogRead(X_pin) <= 515)) {
Serial.print("Forward\n");
strcpy(rotation, "Forward");
send();
delay(100);
}
else if ((analogRead(Y_pin) >= 220) && (analogRead(Y_pin) <= 800) &&
(analogRead(X_pin) <= 515) && (analogRead(X_pin) == 0)) {
Serial.print("Left\n");
strcpy(rotation, "Left");
send();
delay(100);
}
else if (((analogRead(X_pin) >= 490) && (analogRead(X_pin) <= 550)) &&
(analogRead(Y_pin) == 0)) {
Serial.print("Backward\n");
strcpy(rotation, "Backward");
send();
delay(100);
}
else if ((analogRead(X_pin) >= 1000) && (analogRead(X_pin) <= 1025)) {
Serial.print("Right\n");
strcpy(rotation, "Right");
send();
delay(100);
}
}
void send() {
sent = false;
sent = radio.write(&rotation, sizeof(rotation));
Serial.print("Data Sent ");
Serial.println(rotation);
if (sent) {
Serial.println("Data sent!");
}
else
Serial.println("Tx failed!");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment