Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NRF24 based radio receiver for radio controlled tractors
- // by modelleicher
- // NRF24 Library: https://github.com/TMRh20/RF24
- // Inspiration by: "RC Tractor Guy" on youtube https://www.youtube.com/user/magicalmachines/videos
- // Also great thanks to Antony Cartwright for the best NRF24 tutorial on youtube (bare basics explanation) https://www.youtube.com/watch?v=Qn2YLSYz3WM
- // work in progress, just a start for now.
- // March 2017
- #include <SPI.h>
- #include "RF24.h"
- #include <Servo.h>
- // right now we are only receiving 5 bytes..
- uint8_t dataPackage[5];
- // initialize the RF24
- RF24 radio(9, 10);
- // again, the pipe. Important, Sender and Receiver have to use the same, obviously.
- const uint64_t pipe = 1;
- // the pins for the motor driver.
- // first two are used to select driving direction, third one uses PWM to control speed of the motor
- int pin_driveMotor_dir1 = 5;
- int pin_driveMotor_dir2 = 4;
- int pin_driveMotor_pwm = 3;
- // not used yet
- int dt = 0;
- int lastMillis = 0;
- // initialize the steering servo
- Servo servo1;
- void setup() {
- // serial for debug
- Serial.begin(9600);
- // begin radio
- radio.begin();
- // no need to set PA level anything higher than min as long as the communication is only one way.
- radio.setPALevel(RF24_PA_MIN);
- // debug
- Serial.println("started..");
- // since we are the receiver, open a reading pipe
- radio.openReadingPipe(1, pipe);
- // debug
- Serial.println("opened..");
- // start listening (now we are a receiver and listening for packets)
- radio.startListening();
- //debug
- Serial.println("listening..");
- // attach the steering servo at pin 7
- servo1.attach(7);
- }
- void loop() {
- // not used yet
- dt = millis() - lastMillis;
- // check if there are packets received. radio.availiable() will return true if there is anything in the buffer
- if (radio.available())
- {
- //read the data and store in dataPackage array.
- radio.read(dataPackage, sizeof(dataPackage));
- // current array index to axis:
- // 1 = X
- // 2 = Y
- // 3 = RX
- // 4 = RY
- // control drive motor
- if (dataPackage[4] > 135) // larger than 130 -> forwards
- {
- analogWrite(pin_driveMotor_pwm, map(dataPackage[4], 135, 255, 0, 255)); // write the PWM value
- // set direction:
- digitalWrite(pin_driveMotor_dir1, HIGH);
- digitalWrite(pin_driveMotor_dir2, LOW);
- }
- else if (dataPackage[4] < 115) // smaller than 120 -> backwards
- {
- analogWrite(pin_driveMotor_pwm, map(dataPackage[4], 115, 0, 0, 255)); // write PWM value
- // set direction:
- digitalWrite(pin_driveMotor_dir1, LOW);
- digitalWrite(pin_driveMotor_dir2, HIGH);
- }
- else // stop driving
- {
- analogWrite(pin_driveMotor_pwm, 0);
- digitalWrite(pin_driveMotor_dir1, LOW);
- digitalWrite(pin_driveMotor_dir2, LOW);
- }
- // steering trim on Deutz D16006 - min 50 max 120
- // map the steering value to servo min/max values
- dataPackage[1] = map(dataPackage[1], 0, 255, 50, 120);
- // debug
- Serial.println(dataPackage[4]);
- // set the servo value
- servo1.write(dataPackage[1]);
- // debug
- //Serial.println(dataPackage[1]);
- //Serial.println(dataPackage[2]);
- //Serial.println(dataPackage[3]);
- //Serial.println("----------------------");
- }
- else
- {
- // this is called each time the loop runs and there is no data in the buffer from the NRF24
- //Serial.println("Not Available!");
- }
- // not sure if the dalay is needet.
- delay(10);
- //Serial.println("Wait...");
- // not used yet
- lastMillis = millis();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement