Hello!
I am working on modifying an Elegoo Smart Car to be controlled by a 2.4ghz remote control for a STEM camp I'm doing this summer. I've gotten it to allow me to control the servo but can't seem to work out the code to control the motors. I've included my current code and some links to pics below.
I could really use some help with the code and I know the students would appreciate it as well.
Thanks!
Kevin
Transmitter Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
int x_key = A0;
int y_key = A1;
int k_key = A2;
int z_key = A3;
int x_pos;
int y_pos;
int k_pos;
int z_pos;
const int frontButton = 6; //pressing button moves toy car forward
const int backButton = 5; //pressing button moves toy car backward
const int leftButton = 4; //pressing button moves toy car to left
const int rightButton = 3; //pressing button moves toy car to right
void setup() {
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
pinMode (x_key, INPUT) ;
pinMode (y_key, INPUT) ;
pinMode (k_key, INPUT) ;
pinMode (y_key, INPUT) ;
}
void loop() {
x_pos = analogRead (x_key) ;
y_pos = analogRead (y_key) ;
radio.write(&x_pos, sizeof(x_pos));
k_pos = analogRead (k_key) ;
z_pos = analogRead (z_key) ;
radio.write(&k_pos, sizeof(z_pos));
delay(100);
}
Receiver Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
const int IN1 = 7;
const int IN2 = 8;
const int IN3 = 9;
const int IN4 = 11;
Servo servo;
RF24 radio(A0, A1); // CE, CSN
const byte address[6] = "00001";
int servo_pin = 3;
void setup() {
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
Serial.begin(9600);
radio.begin();
Serial.println("checking if chip connected");
Serial.print("check-");
servo.attach (servo_pin ) ;
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
int k_pos ;
radio.read(&k_pos, sizeof(k_pos));
Serial.println(k_pos);
k_pos = map(k_pos, 0, 1023, 0, 180);
if (k_pos>400 && k_pos<600)
{
}
else{
servo.write (k_pos) ;
}
}
if (radio.available()) {
int message[4];
radio.read(&message, sizeof(message));
int i;
for (i = 0; i < 4; i = i + 1) {
Serial.print(message[i]);
}
Serial.println();
int sum = message[0]+message[1]+message[2]+message[3];
if (sum==4) {
//stop
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
Serial.println("LLLL");
}
if (sum==3) {
if (message[0]==0 && message[1]==1 && message[2]==1 && message[3]==1) {
//move forward
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
Serial.println("HLLH");
}
if (message[0]==1 && message[1]==0 && message[2]==1 && message[3]==1) {
//move backward
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
Serial.println("LHHL");
}
if (message[0]==1 && message[1]==1 && message[2]==0 && message[3]==1) {
//move forward
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
Serial.println("LLLH");
}
if (message[0]==1 && message[1]==1 && message[2]==1 && message[3]==0) {
//move left
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
Serial.println("HLLL");
}
}
if (sum<=2) {
//stop
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
Serial.println("LLLL");
}
}
}
Some Pics:
Servo_Plus_Motor_Remote_Test.ino (1.13 KB)
Servo_plus_Motor_Test.ino (3.32 KB)