Hi.
I have now build a RC car with a cabled controller. This is a full size RC car that we sit on and steer with a joystick module to arduino.
This is the code I use now
#include <SPI.h>
#include <Servo.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
//
// Hardware configuration
//
// Joystick Pins
int sensorPinX = A0;
int sensorPinY = A1;
int pwmOut = 6;
Servo myservo;
// Led Pins
int red = 4;
int green = 2;
RF24 radio(9,10);
const uint64_t pipes[1] = { 0xF0F0F0F0E1LL };
int sensorValueX = 0;
int oldSensorValueX = 0; // to keep track of the X value
int sensorValueY = 0;
int reposValue = 90; // default steady point for the servo
int inPin = 8;
int state = 0;
int reading;
int previous = 1;
long time = 0;
long debounce = 200;
int oldButtonState = LOW;
int x = 0;
int runcar = 0;
void setup(void)
{
pinMode(inPin, INPUT);
pinMode(pwmOut, OUTPUT);
Serial.begin(9600);
}
void sendOrder()
{
reading = digitalRead(inPin);
if (reading == 1 && previous == 0 && millis() - time > debounce) {
if (runcar == 0)
runcar = 1;
else
runcar = 0;
time = millis();
}
previous = reading;
Serial.println(runcar);
if (runcar == 1) {
sensorValueX = analogRead(sensorPinX);
if(oldSensorValueX > 35 && oldSensorValueX < 1000)
{
// If X axis is over the upper thresholds then we want to increment the value of the steady point of the servo
if(sensorValueX > 1000)
{
reposValue++;
}
// If X axis is under the lower thresholds then we want to decrement the value of the steady point of the servo
if(sensorValueX < 35)
{
reposValue--;
}
}
// we update the previous value of the X axis
oldSensorValueX = sensorValueX;
// Now we get the value of the Y axis
// The Y axis is used to drive the servo
sensorValueY = analogRead(sensorPinY);
// We have to map the Y value (from 0 to 1024) with the possible values for the servo (0 to 180)
sensorValueY = map(sensorValueY,0,1020,0,180);
sensorValueX = map(sensorValueX,1,1020,202,50);
myservo.write(sensorValueY);
analogWrite(pwmOut, sensorValueX);
Serial.println("Y:");
Serial.println(sensorValueY);
Serial.println("X:");
Serial.println(sensorValueX);
if(sensorValueY > (reposValue -4) && sensorValueY < (reposValue+4)) sensorValueY = reposValue;
} else {
analogWrite(pwmOut, 0);
} // else
} // void
void loop(void)
{
sendOrder();
}
I have taken this code from an other source on the Internet, I do not remember where.
I want to send Y: sensorValueY, X:sensorValueX and R:runcar to the receiver with NRF24 (transmitter)
I want to receive Y:sensorValueY X:sensorValueX R:runcar on the reciever. (NRF24)
Arduino who should read Y and set the servo, read X and set the speed, read R and decide to run the car or not.
I have just meet problems to try to make this wirelessly, I have tried to send both data to the device, but either I end up with an very unstable connection.
Can somebody please help me on my way? I have arduino uno and arduino micro.