Commit ac4e4521 by satsob

nRF_Robot

parent 5369df60
/* Include libraries */
#include <SPI.h>
#include <RF24.h>
#include <BTLE.h>
/* Include servo */
#include <Servo.h>
//#include <nRF24L01.h> // Maybe needed, commented out right now!
/* Define */
#define rightServo 5 // Define Servo right pin
#define leftServo 6 // Define Servo left pin
/* Global variables */
Servo rightWheel;
Servo leftWheel;
RF24 radio(9,10); // Create RF24 object to work with the RF24 library.
BTLE btle(&radio); // Create BTLE object to work with the BTLE library.
/* Replace! "CustomName" to the name of your device. Password - maximum length (14)! characters.
It's for recognizing the correct data packets. */
#define PASSWORD "CustomName"
/* Define functions used for the robot to control */
/* Stop the robot */
void setWheelsStop() {
rightWheel.writeMicroseconds(1500);
leftWheel.writeMicroseconds(1500);
}
/* Drive forward */
void setWheelsForward(){
rightWheel.write(1300);
leftWheel.write(1700);
}
/* Drive right */
void setWheelsRight(){
rightWheel.write(1700);
leftWheel.write(1700);
}
/* Drive left */
void setWheelsLeft(){
rightWheel.write(1300);
leftWheel.write(1300);
}
/* Drive backward */
void setWheelsBackward(){
rightWheel.write(1700);
leftWheel.write(1300);
}
void setup() {
/* Start the Serial and btle. */
Serial.begin(9600);
btle.begin("");
/* Attach servos to digital pins defined earlier */
rightWheel.attach(rightServo);
leftWheel.attach(leftServo);
/* Put servos to standstill */
//setWheels();
}
void loop() {
String str = ""; // For collecting a string of values ​​from different types of variables
/* Start listening. If data is received it is stored in the "buffer".
Then the received data is decoded and checked (CRC). */
if (btle.listen()) {
// Serial.print("Got payload: "); // If data is received, print "Got payload: "
/* Store the received data into the buffer */
for (uint8_t i = 2; i < (btle.buffer.pl_size)-6; i++) {
str += (char (btle.buffer.payload[i]));
// Serial.print(btle.buffer.payload[i],HEX); Serial.print(" "); // To receive data in HEX format
// Serial.print(char (btle.buffer.payload[i])); Serial.print(" "); // To receive data in char format
}
/* (Spam protection) only if string starts with "CustomName" */
if (str.startsWith(PASSWORD)) {
str.replace(PASSWORD, ""); // (remove / replace) PASSWORD
/* According to what data was sent, print the direction */
if ((char (str.charAt(2))) == 7) {
Serial.println("RIGHT");
setWheelsRight();
delay(100);
}
else if ((char (str.charAt(2))) == 6) {
Serial.println("LEFT");
setWheelsLeft();
delay(100);
}
else if ((char (str.charAt(2))) == 5) {
Serial.println("FORWARD");
setWheelsForward();
delay(100);
}
else if ((char (str.charAt(2))) == 4) {
Serial.println("BACKWARD");
setWheelsBackward();
delay(100);
}
else {
setWheelsStop();
delay(100);
}
}
}
/* Hop channels through 37-39 to scan for data transmissions */
btle.hopChannel();
}
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