/**
* Project nRF24L01 receiver
*
* @author Gerd Bartelt - www.sebulli.com
*
* @brief Receives commands from a nRF24L01 and controls a motor.
* @brief Based on the RF24 example "GettingStarted"
*
* @copyright GPL2
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include "printf.h"
//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
RF24 radio(9,10);
// Set up motor on pin 4,5,6 and 7
int right01 = 4;
int right02 = 5;
int left01 = 6;
int left02 = 7;
int frontSensor = 1; // Analog pin 1
int msg[1];
// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = {
0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
void setup(void)
{
// Motor pins as output
pinMode(right01, OUTPUT);
pinMode(right02, OUTPUT);
pinMode(left01, OUTPUT);
pinMode(left02, OUTPUT);
pinMode(frontSensor, INPUT);
Serial.begin(57600);
printf_begin();
// Setup and configure rf radio
radio.begin();
// Increase the delay between retries & # of retries
radio.setRetries(15,15);
// Open pipes to other nodes for communication
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
// Start listening
radio.startListening();
// Dump the configuration of the rf unit for debugging
radio.printDetails();
}
void loop(void)
{
// Receive each packet, dump it out, and send it back
// if there is data ready
if ( radio.available() )
{
// Dump the payloads until we've gotten everything
char got_kore_info;
bool done = false;
while (!done)
{
//Distance sensor - If less than 10 cm to obj, turn around!
int read_frontSensor = analogRead(frontSensor);
int result_frontSensor = (4800/(read_frontSensor - 20));
send_distance(result_frontSensor);
stopper();
radio.powerDown();
delay(1000);
radio.powerUp();
// Fetch the payload, and see if this was the last one.
done = radio.read( &got_kore_info, sizeof(char) );
// Spew it
printf("Got command '%c'\n\r",got_kore_info);
// Delay just a little bit to let the other unit
// make the transition to receiver
delay(20);
// First, stop listening so we can talk
radio.stopListening();
// decode the received "køre info"
switch (got_kore_info)
{
case '8':
forwards();
printf("Forwards, Success",got_kore_info);
break;
case '2':
backwards();
printf("Backwards, Success",got_kore_info);
break;
case '5':
around();
printf("Turning Around, Success",got_kore_info);
break;
case '0':
stopper();
printf("Stopping, Success",got_kore_info);
break;
}
delay(2500);
}
// Send the final one back.
radio.write( &got_kore_info, sizeof(char) );
// Now, resume listening so we catch the next packets.
radio.startListening();
}
}
void send_distance(int result_frontSensor){
String theMessage = String(result_frontSensor);
int messageSize = theMessage.length();
for (int i = 0; i < messageSize; i++) {
int charToSend[1];
charToSend[0] = theMessage.charAt(i);
radio.write(charToSend,1);
}
msg[0] = 2;
radio.write(msg,1);
}
void forwards(){
printf("Forwards\n\r");
digitalWrite(right02, LOW);
digitalWrite(right01, HIGH);
digitalWrite(left02, LOW);
digitalWrite(left01, HIGH);
}
void backwards(){
printf("Backwards\n\r");
digitalWrite(right01, LOW);
digitalWrite(right02, HIGH);
digitalWrite(left01, LOW);
digitalWrite(left02, HIGH);
}
void around(){
printf("Around\n\r");
digitalWrite(right01, LOW);
digitalWrite(right02, HIGH);
digitalWrite(left02, LOW);
digitalWrite(left01, HIGH);
}
void stopper(){
printf("Stop\n\r");
digitalWrite(right01, LOW);
digitalWrite(right02, LOW);
digitalWrite(left02, LOW);
digitalWrite(left01, LOW);
}