Hello all,
I finally got my RC tank working, thanks to some advice from others. My code works great, I just need to add some lights to it. I know this sounds very simple, and it may be, I am just having a hard time integrating the LED code to my current sketch. It seems every time I try to something goes wrong somewhere else. I think what makes it difficult is I am using the NRF24L01 radio transmitter, so telling the transmitter to do one thing and the receiver to do another always trips me up. I have spent a long time on this and thought someone else could more than likely just tell me in two or three lines and it would be easy for them. I just need the LED to be a button, but when you press the button it turns it ON then press again for OFF. Hopefully that makes sense, I mean like a toggle.
I am pretty new to Arduino, though working hard on it, I still struggle with adding new things to a sketch. Would anyone mind throwing down a simple sketch of how to do so on the transmitter and receiver side? This is the last thing I need to complete my first big project! Thank you all in advance.
Transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002"};
char receivedData[32] = "";
int joystick[5];
int button_pin = A2;
boolean button_state = 0;
void setup() {
pinMode(button_pin, INPUT); //////////////////////
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address[1]);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
joystick[0] = analogRead(A4);
joystick[1] = analogRead(A3);
joystick[2] = analogRead(A0), joystick[2] = map(joystick[2], 0, 1023, 0, 255);
joystick[3] = analogRead(A1), joystick[3] = map(joystick[3], 0, 1023, 128, 255);
joystick[4] = analogRead(A2);
radio.write( joystick, sizeof(joystick) );
{
}
}
Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
Servo Tilt;
Servo Pan;
#define enA 6
#define in1 7
#define in2 5
#define enB 3
#define in3 4
#define in4 2
RF24 radio(9, 10); // CE, CSN
const byte address[][6] = {"00001", "00002"};
char receivedData[32] = ""; /////this may be a factor
int xAxis, yAxis;
int motorSpeedA = 0;
int motorSpeedB = 0;
int joystick[5];
int servoAngleA = 0;
int servoAngleB = 0;
int servo_pin = 8;
int servo_pin2 = A1;
boolean button_state = 0; ////////////////////////////////////
int led_pin = A2; /////////////////////////////////////////
void setup()
{
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(A2, OUTPUT); ///////////////////////////////////////////
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address[1]);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
Tilt.attach(servo_pin);
Pan.attach(servo_pin2);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop()
{
if (radio.available()) {
radio.read( joystick, sizeof(joystick) );
//// radio.read(&receivedData, sizeof(receivedData));
yAxis = joystick[0];
xAxis = joystick[1];
servoAngleA = joystick[2];
servoAngleB = joystick[3];
button_state = joystick[4];
Serial.println(yAxis);
Serial.println(xAxis);
Serial.println(servoAngleA);
Serial.println(servoAngleB);
Serial.println(button_state);
}
if (yAxis < 470) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
}
else if (yAxis > 550) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
}
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
if (xAxis < 470) {
int xMapped = map(xAxis, 470, 0, 0, 255);
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
int xMapped = map(xAxis, 550, 1023, 0, 255);
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
Tilt.write(servoAngleA);
Pan.write(servoAngleB);
if(button_state == 1)
{
analogWrite(A2, HIGH);
Serial.println(button_state);
}
else
{
analogWrite(A2, LOW);
Serial.println(button_state);}
}