Index>Robot Kit>PiCar-A WiFi 3WD Smart Robot Car Kit for Raspberry>Lesson 2 Adeept Ultimate Starter Kit for Arduino UNO R3, LCD1602, Servo Motor, Relay, Processing an

Lesson 2 Adeept Ultimate Starter Kit for Arduino UNO R3, LCD1602, Servo Motor, Relay, Processing an

445

In this article, we’ll show you how to control the 4-wheel smart car to go forward, backward and veer with the two joysticks of the remote control. To realize the control, we just need to tackle the following steps: firstly achieve the communication between the remote control and the car; secondly read the data of the two joysticks on the remote control; thirdly use UNO to control the movement of the servo; fourthly use UNO to control the movement of two DC motors.

NOW WE SOLVE THEM ONE BY ONE.


/***********************************************************
File name:  AdeeptRemoteControl.ino
Description:
1.Move the sticker rightside left and right to control the direction of the car, and forward and backward to control the left and right turning of the ultrasonic module.
2.Move the sticker leftside forward and backward to control the speed of the car going forward and backward.
3.Spin the R1 potentiometer to control the startup speed of the DC motor.
4.Spin the R6 potentiometer to fine tune the direction of the car.
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2019/03/28
***********************************************************/
#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10);            // define the object to control NRF24L01
byte addresses[5] = "00007";  // define communication address which should correspond to remote control
int data[9];                  // define array used to save the communication data
int mode = 0;
const int pot6Pin = 7;        // define R6
const int pot5Pin = 6;        // define R1
const int led1Pin = 6;        // define pin for LED1 which is close to NRF24L01 and used to indicate the state of NRF24L01
const int MotorPin = 2;       // define pin for direction X of joystick U2
const int ServoPin = 1;       // define pin for direction Y of joystick U1
const int ultrasonicPin = 3;  // define pin for direction x of joystick U2

void setup() {

  radio.begin();                      // initialize RF24
  radio.setRetries(0, 15);            // set retries times
  radio.setPALevel(RF24_PA_LOW);      // set power
  radio.openWritingPipe(addresses);   // open delivery channel
  radio.stopListening();              // stop monitoring
  pinMode(led1Pin, OUTPUT);   // set led1Pin to output mode
  data[3] = 0;
  data[4] = 1;
}

void loop() {
  // put the values of rocker, switch and potentiometer into the array
  data[0] = analogRead(MotorPin);
  data[1] = analogRead(ServoPin);
  data[6] = analogRead(pot5Pin);
  data[7] = analogRead(pot6Pin);
  data[8] = analogRead(ultrasonicPin);
  // send array data. If the sending succeeds, open signal LED
  if (radio.write( data, sizeof(data) ))
  digitalWrite(led1Pin,HIGH);

  // delay for a period of time, then turn off the signal LED for next sending
  delay(2);
  digitalWrite(led1Pin,LOW);
}