Hardware components | ||||||
![]() |
| × | 1 | |||
| × | 2 | ||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 2 | |||
![]() |
| × | 4 | |||
![]() |
| × | 3 | |||
![]() |
| × | 6 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
| × | 2 | ||||
Software apps and online services | ||||||
![]() |
| |||||
![]() |
| |||||
Hand tools and fabrication machines | ||||||
![]() |
|
This rover is controlled with Radio transceivers.
The RF24 has a frequency of 2.4 GHz, which can interfere with a wireless mouse, Wifi, or other sources, so I have done my best to provide code and instructions on how to improve or optimize the best signal for maximum distance.
For example, the the code we set the PA level to HIGH or higher, and I included the channel, custom pipe numbers and the defining the data rate is essential for a reliable connection. The code is as raw as I could get it for beginners.
The RF24 with the antenna reaches up to 100m, I soldered a 10uF capacitor between VCC and GND on it, or you can buy the RF24 adaptors if desired.
DO NOT WIRE THE VCC PIN TO 5V, YOU WILL BLOW IT.
If you buy the adaptor, you can wire to 5V.
The 10uF capacitor works well with 3V input, and should provide a clear signal.
These are the pins that I used for the rover. You can see each one defined in the code.
I purchased a smart car which included a frame for the servo. The code was difficult to decipher, so I broke it down for beginners.
The L298N motor drivers have a maximum input voltage of 47V.
This project uses 6V gear motors, with two 3.7V 18650 batteries for each driver.
The L298N batteries can handle a maximum input voltage of 47 V.
Meaning if you build this rover, you can buy your own motors, wheels, and ramp up the voltage as desired. Making this project easy to upscale.
The controller sends and receives data provided in the code.
Enjoy building your own now.
I am still updating the page for this project.
But the code will do everything you need it to with at least one button and one joystick.
Remote control Radar, which also can be attached to a rover, or any vehicle.
nRF24L01, L298N, Arduino Mega, Arduino Nano, HC-SR04 Ultrasonic Sensor, Remote control. Which can also be found on my other tutorial if you want to make your own remote control.
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
RF24 radio(9, 10);
const byte addresses[][32] = {"13572" , "13513"};
// int data[10] = { "001", "002", "003", "004", "005", "006", "007", "008", "009", "010" };
int data[6] = { "001", "002", "003", "004", "005", "006" };
int dataIn[2] = {" ", " "};
const int J1xPin = A0;
const int J1yPin = A1;
const int J2xPin = A2;
const int J2yPin = A3;
//const int B1Pin = 2;
//const int B2Pin = 3;
const int B3Pin = 4;
const int B4Pin = 5;
//const int Pot1Pin = A6;
//const int Pot2Pin = A7;
//const int Jpush1 = A4;
//const int Jpush2 = A5;
void setup() {
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_HIGH);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
radio.setDataRate(RF24_250KBPS);
//radio.enableDynamicPayloads();
radio.setChannel(0x76);
// pinMode(Jpush1, INPUT);
// pinMode(Jpush2, INPUT);
// pinMode(B1Pin, INPUT_PULLUP);
// pinMode(B2Pin, INPUT_PULLUP);
pinMode(B3Pin, INPUT_PULLUP);
pinMode(B4Pin, INPUT_PULLUP);
// pinMode(Pot1Pin, INPUT);
// pinMode(Pot2Pin, INPUT);
}
void loop() {
delay(5);
radio.stopListening();
data[0] = map(analogRead(J1xPin), 0,1023, 255, 0);
data[1] = map(analogRead(J1yPin), 0,1023, 255, 0);
data[2] = map(analogRead(J2xPin), 0,1023, 255, 0);
data[3] = map(analogRead(J2yPin), 0,1023, 255, 0);
data[4] = map(digitalRead(B3Pin), 1, 0, 0, 1);
data[5] = map(digitalRead(B4Pin), 1, 0, 0, 1);
radio.write(&data, sizeof(data));
//Serial.println("Sending Joystick Data"); // use to debug, enable to verify data sent
delay(10);
radio.startListening();
while (radio.available() ) {
radio.read(&dataIn, sizeof(dataIn));
Serial.print(dataIn[0]);
Serial.print(",");
Serial.print(dataIn[1]);
Serial.print(".");
delay(10);
}
}
// 4 wheel rover, Elegoo Mega, RF24+, L298N x2
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h> // add these RF24 libraries
#include <SPI.h>
#include <printf.h>
#include <Servo.h> // include radar servo
RF24 radio(9, 10); // CE, CSN pins on Mega or Uno etc
const byte addresses[][32] = {"13572" , "13513"}; // create 2 addresses,
// 32 bytes max
// create 6 piece data array to receive from controller
int data[6] = { "001", "002", "003", "004", "005", "006" };
// create two piece array so we can send radar data to controller
int dataIn[2] = {"", ""};
int spdA = 2;
int out1 = 29; // MOTOR A (front)
int out2 = 27;
int spdB = 3;
int out3 = 25; // MOTOR B (front)
int out4 = 23; // these are motor control pins
// and speed control pins on L298N
int spdC = 4;
int out5 = 22; // MOTOR C (back)
int out6 = 24;
int spdD = 5;
int out7 = 26; // MOTOR D (back)
int out8 = 28;
const int trigPin = 47;
const int echoPin = 45; // pins for HC-SR04 Ultrasonic module
long duration; // integer for time (for sound wave)
int distance; // distance is measured by bouncing sound wave (doppler)
int i = 90; // integer for angle of servo (we start here, facing forward)
Servo RadarServo; // Servo (space) I named it "RadarServo"
// Notice we can identify the next integer within curly brackets
int calculateDistance() { // Function for calculating the // distance measured by the Ultrasonic sensor
//ultrasonic module will now perform distance test
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound // wave travel time in microseconds
distance= duration*0.034/2; // divide by 2 for there and back
return distance; // returns the distance value
}
////////////////////SETUP///////////////////////////////
void setup() {
Serial.begin(9600); // starts the serial monitor
radio.begin(); // starts the radio
radio.openWritingPipe(addresses[1]); // need both, writes on address 1
radio.openReadingPipe(1, addresses[0]); // reads on address 0
radio.setPALevel(RF24_PA_HIGH); // sets power level of rf24
radio.setDataRate(RF24_250KBPS); // data rate is essential
//radio.enableDynamicPayloads(); // not needed here
radio.setChannel(0x76); // setting the channel helps
pinMode(spdA, OUTPUT);
pinMode(spdB, OUTPUT); // sets spd PWM pins as outputs
pinMode(spdC, OUTPUT);
pinMode(spdD, OUTPUT);
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(out3, OUTPUT);
pinMode(out4, OUTPUT); // sets output pins on L298N for driving motors
pinMode(out5, OUTPUT);
pinMode(out6, OUTPUT);
pinMode(out7, OUTPUT);
pinMode(out8, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
RadarServo.attach(49); // Defines on which pin is the servo motor attached
RadarServo.write(90); // writes servo to forward at start (forward)
}
////////////////////////////LOOP/////////////////////////////////////////////
//////////////////CALCULATING DISTANCE FROM HC-SR04////////////////////
void loop() {
distance = calculateDistance(); // lets calculate distance with HCSR04
Serial.print(i); // now let's read those values
Serial.print(","); // i is the angle
Serial.print(distance); // distance is distance
Serial.println(".");
delay(5); // wait 5 ms
radio.startListening(); // start listening
if (radio.available() ) { // if radio is available
while (radio.available() ) { // while it is available
distance = calculateDistance(); // calculate distance here too
Serial.print(i);
Serial.print(","); // print these values
Serial.print(distance);
Serial.print(".");
if (distance >= 10) { // if the distance is more than 10cm
delay(5); // wait 5ms
radio.startListening(); // start listening
radio.read(&data, sizeof(data)); // read all in the dataset, and size
Serial.print("Drive mode");
Serial.print("\t");
Serial.print ("J1x: ");
Serial.print( data[0]);
Serial.print("\t");
Serial.print ("J1y: ");
Serial.println(data[1]); // print remote control data sent from controller
Serial.print("\t");
Serial.print ("J2x: ");
Serial.print( data[2]);
Serial.print("\t");
Serial.print ("J2y: ");
Serial.print(data[3]);
Serial.print("\t");
Serial.print ("B3: ");
Serial.print(data[4]);
Serial.print("\t");
Serial.print ("B4: ");
Serial.print(data[5]);
Serial.print("\t");
delay(10); // delay 10 ms for receiving
/////////////////////////DRIVING THE ROVER//////////////////////////////////
if ( data[1] == 129 ) { // joystick1 y data as it comes in untouched
analogWrite(spdA, 0);
analogWrite(spdB, 0); // no speed, no motion.
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
}
if ( data[2] == 0 ) { // if joystick 2 x data is left
analogWrite(spdA, 255);
analogWrite(spdB, 255);
analogWrite(spdC, 255);
analogWrite(spdD, 255);
digitalWrite(out1, HIGH); // TURN LEFT
digitalWrite(out2, LOW);
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
digitalWrite(out5, HIGH);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, HIGH);
}
if ( data[2] == 255 ) { // if joystick 2 x data is right
analogWrite(spdA, 255);
analogWrite(spdB, 255);
analogWrite(spdC, 255);
analogWrite(spdD, 255);
digitalWrite(out1, LOW); // TURN RIGHT
digitalWrite(out2, HIGH);
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, HIGH);
digitalWrite(out8, LOW);
}
if ( data[1] == 255 ) { // if joystick 1 y data is forward
analogWrite(spdA, 255);
analogWrite(spdB, 255);
analogWrite(spdC, 255);
analogWrite(spdD, 255);
digitalWrite(out1, HIGH);
digitalWrite(out2, LOW); // GO MOTORS
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
digitalWrite(out5, HIGH);
digitalWrite(out6, LOW);
digitalWrite(out7, HIGH);
digitalWrite(out8, LOW);
}
if ( data[1] == 0 ) { // if joystick 1 y data is down
analogWrite(spdA, 255);
analogWrite(spdB, 255);
analogWrite(spdC, 255);
analogWrite(spdD, 255);
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH); // BACKWARDS
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, LOW);
digitalWrite(out8, HIGH);
}
//////////////////////USE BUTTON TO CONTROL RADAR ///////////////////////
if ( data[4] == 1 ) { // IF BUTTON3 IS PRESSED, RUN RADAR, THEN STOP
// SO WE CAN DRIVE :)
delay(5);
radio.stopListening();
for ( int i=60; i<=120; i++ ) { // rotates the servo motor from 60 to 120 degrees
RadarServo.write(i++);
delay(30);
distance = calculateDistance(); // Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
dataIn[0] = i;
dataIn[1] = distance;
radio.write(&dataIn, sizeof(dataIn));
RadarServo.write(90);
}
}
}
////////////////////////IF DISTANCE <10CM BACK UP////////////////////////////
if (distance <= 10) {
analogWrite(spdA, 255);
analogWrite(spdB, 255);
analogWrite(spdC, 255);
analogWrite(spdD, 255);
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH); // BACKWARDS
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, LOW);
digitalWrite(out8, HIGH);
distance = calculateDistance();
delay(5);
radio.stopListening();
for ( int i=89; i<=92; i++ ) { // rotates the servo motor from 89 to 92 degrees
RadarServo.write(i);
delay(30);
distance = calculateDistance(); // Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
dataIn[0] = i;
dataIn[1] = distance;
radio.write(&dataIn, sizeof(dataIn));
RadarServo.write(90);
}
}
}
}
}
Arduino - Rover with Automation (SELF DRIVING)
ArduinoStill working to improve, may have bugs in corners
// 4 wheel rover, Elegoo Mega, RF24+, L298N x2
// This rover is self driving if you assign data[4] as an input and set it equal to the value of 1
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#include <printf.h>
#include <Servo.h> // radar servo
RF24 radio(9, 10); // CE, CSN
//const uint64_t pipe[][32] = {"0xE8E8F0F0E1LL", "0xF0F0F0F0E8E8"};
const byte addresses[][32] = {"13572" , "13513"};
int data[8] = { "001", "002", "003", "004", "005", "006", "007" , "008" };
int dataIn[2] = {"", ""};
int spdA = 2;
int out1 = 29; // MOTOR A (front)
int out2 = 27;
int spdB = 3;
int out3 = 25; // MOTOR B (front)
int out4 = 23;
int spdC = 4;
int out5 = 22; // MOTOR C (back)
int out6 = 24;
int spdD = 5;
int out7 = 26; // MOTOR D (back)
int out8 = 28;
const int trigPin = 47;
const int echoPin = 45;
long duration;
int distance;
int distance2;
int i = 90; // integer for angle of servo
int mode = 0; // int mode for self driving mode vs control mode.
Servo RadarServo;
// radar integer
int calculateDistance() { // Function for calculating the distance measured by the Ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
distance= duration*0.034/2;
return distance;
}
///////////////////////////////////////SETUP//////////////////////////////////////////
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.setPALevel(RF24_PA_HIGH);
radio.setDataRate(RF24_250KBPS);
// radio.enableDynamicPayloads();
radio.setChannel(0x76);
pinMode(spdA, OUTPUT);
pinMode(spdB, OUTPUT);
pinMode(spdC, OUTPUT);
pinMode(spdD, OUTPUT);
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(out3, OUTPUT);
pinMode(out4, OUTPUT);
pinMode(out5, OUTPUT);
pinMode(out6, OUTPUT);
pinMode(out7, OUTPUT);
pinMode(out8, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
RadarServo.attach(49); // Defines on which pin is the servo motor attached
RadarServo.write(90);
}
////////////////////////////////////////LOOP/////////////////////////////////////////////
void loop() {
distance = calculateDistance(); // lets calculate distance again
Serial.print(i); // print angle
Serial.print(","); // comma for processing string read
Serial.print(distance); // distance
Serial.println("."); // period for processing string read
delay(5);
radio.startListening(); // start listening
/**************************************************************************************/
if (radio.available() ) { // if the radio is available
while (radio.available() ) { // while it is available (required)
distance = calculateDistance(); // lets calculate distance again
Serial.print(i);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
//////////////////////////////////////////////////////////////////////////////////////////
delay(5);
radio.startListening(); // start listening again
radio.read(&data, sizeof(data)); // read all in the dataset, and size
Serial.print("Drive mode");
Serial.print("\t");
Serial.print ("J1x: ");
Serial.print( data[0]);
Serial.print("\t");
Serial.print ("J1y: ");
Serial.println(data[1]);
Serial.print("\t");
Serial.print ("J2x: ");
Serial.print( data[2]); // read the data for debugging, is in drive mode
Serial.print("\t"); // follow this format for easy data reading and testing
Serial.print ("J2y: ");
Serial.print(data[3]);
Serial.print("\t");
Serial.print ("B3: ");
Serial.print(data[4]);
Serial.print("\t");
Serial.print ("B4: ");
Serial.print(data[5]);
Serial.print("\t");
Serial.print ("Pot2: ");
Serial.print(data[6]);
Serial.print("\t");
Serial.print ("Jpush1: ");
Serial.print(data[7]);
Serial.print("\t");
////////////////////////////////DRIVING MODE/////////////////////////////////
RadarServo.write(data[6]); // use potentiometer for calibrating initial direction of servo
if (( data[1] == 129) || (data[1] == 128 )) {
analogWrite(spdA, 0); // added y axis here too
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
}
if ( data[2] == 0 ) {
analogWrite(spdA, 150);
analogWrite(spdB, 150);
analogWrite(spdC, 150);
analogWrite(spdD, 150);
digitalWrite(out1, HIGH); // TURN LEFT
digitalWrite(out2, LOW);
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
digitalWrite(out5, HIGH);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, HIGH);
}
if ( data[2] == 255 ) {
analogWrite(spdA, 150);
analogWrite(spdB, 150);
analogWrite(spdC, 150);
analogWrite(spdD, 150);
digitalWrite(out1, LOW); // TURN RIGHT
digitalWrite(out2, HIGH);
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, HIGH);
digitalWrite(out8, LOW);
}
if ( data[1] == 255 ) {
analogWrite(spdA, 200);
analogWrite(spdB, 200);
analogWrite(spdC, 200);
analogWrite(spdD, 200);
digitalWrite(out1, HIGH);
digitalWrite(out2, LOW); // GO MOTORS
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
digitalWrite(out5, HIGH);
digitalWrite(out6, LOW);
digitalWrite(out7, HIGH);
digitalWrite(out8, LOW);
}
if ( data[1] == 0 ) {
analogWrite(spdA, 255);
analogWrite(spdB, 255);
analogWrite(spdC, 255);
analogWrite(spdD, 255);
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH); // BACKWARDS
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, LOW);
digitalWrite(out8, HIGH);
}
//////////////////////////////COLLISION PREVENTION/////////////////////////////////////
if ( distance <= 25 ) { // if robot gets too close (10 cm)
analogWrite(spdA, 200);
analogWrite(spdB, 200);
analogWrite(spdC, 200);
analogWrite(spdD, 200);
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH); // BACKWARDS, with 6v motors you can drive full speed into walls
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, LOW);
digitalWrite(out8, HIGH);
delay(5);
radio.stopListening();
for ( int i=89; i<=92; i++ ) { // rotates the servo motor from 89 to 92 degrees
// a short scan range between 89 to 92 because
RadarServo.write(i); // we want to be able to control again
delay(30);
distance = calculateDistance(); // Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
dataIn[0] = i;
dataIn[1] = distance; // defined data array components, i is angle
radio.write(&dataIn, sizeof(dataIn)); // write the radar data to the controller
}
}
////////////////////////////SCAN RADAR IF B3 IS PRESSED//////////////////////////////////
if ( data[4] == 1 ) { // if button 3 is pressed
delay(5);
radio.stopListening();
for ( int i=90; i<=120; i++ ) { // rotates the servo motor from 90 to 120 degrees
RadarServo.write(i++);
delay(30);
distance = calculateDistance(); // Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
dataIn[0] = i;
dataIn[1] = distance;
radio.write(&dataIn, sizeof(dataIn));
}
for ( int i=120; i>=60; i-- ) { // rotates the servo motor from 120 to 60 degrees
RadarServo.write(i--);
delay(30);
distance = calculateDistance(); // Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
dataIn[0] = i;
dataIn[1] = distance;
radio.write(&dataIn, sizeof(dataIn));
}
for ( int i=60; i<=90; i++ ) { // rotates the servo motor from 60 to 90 degrees
RadarServo.write(i++);
delay(30);
distance = calculateDistance(); // Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
dataIn[0] = i;
dataIn[1] = distance;
radio.write(&dataIn, sizeof(dataIn));
delay(10);
}
}
////////////////////////////SCAN RADAR WIDER IF BUTTON 4 IS PRESSED ///////////////////
if ( data[5] == 1 ) { // if button 4 is pressed (longer radar scan)
delay(5);
radio.stopListening();
for ( int i=90; i<=165; i++ ) { // rotates the servo motor from 90 to 165 degrees
RadarServo.write(i++);
delay(30);
distance = calculateDistance(); // Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
dataIn[0] = i;
dataIn[1] = distance;
radio.write(&dataIn, sizeof(dataIn));
}
for ( int i=165; i>=20; i-- ) { // rotates the servo motor from 165 to 20 degrees
RadarServo.write(i--);
delay(30);
distance = calculateDistance(); // Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
dataIn[0] = i;
dataIn[1] = distance;
radio.write(&dataIn, sizeof(dataIn));
}
for ( int i=20; i<=90; i++ ) { // rotates the servo motor from 20 to 90 degrees
RadarServo.write(i);
delay(30);
distance = calculateDistance(); // Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree
dataIn[0] = i;
dataIn[1] = distance;
radio.write(&dataIn, sizeof(dataIn));
}
}
///////////////////SMART CAR////////////////////////////////////////////////////////////////
////////////////////////////////AUTO MODE////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
if ( data[7] == 0 ) { // if i press the first joystick down like a button
mode = 1; // changes mode to 1
while (mode == 1) { // while the mode is 1
distance = calculateDistance(); // Calls a function for calculating the distance measured by the Ultrasonic sensor for each degre
///////////////////////////////////////////////////////////////////////////////////////
for (int i=90; i>=30; i-=30) { // scans 90 to 30
Serial.print(i);
RadarServo.write(i);
delay(1000);
distance = calculateDistance();
///////////////////////////////////////////////////////////////////////////////
while ( i == 90 ) { // while it scans at 90 degrees....
if ( distance>=40) { // now we add if statements for distances
distance = calculateDistance();
RadarServo.write(90);
analogWrite(spdA, 200);
analogWrite(spdB, 200);
analogWrite(spdC, 200);
analogWrite(spdD, 200);
digitalWrite(out1, HIGH);
digitalWrite(out2, LOW); // GO MOTORS
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
digitalWrite(out5, HIGH);
digitalWrite(out6, LOW);
digitalWrite(out7, HIGH);
digitalWrite(out8, LOW);
} // end if dist > 40
if ( distance <= 25 ) {
analogWrite(spdA, 200);
analogWrite(spdB, 200);
analogWrite(spdC, 200);
analogWrite(spdD, 200);
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH); // BACKWARDS, with 6v motors you can drive full speed into walls
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, LOW);
digitalWrite(out8, HIGH);
delay(400);
analogWrite(spdA, 0); // added y axis here too
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
} // close if d <25
if (distance <= 40) {
analogWrite(spdA, 0); // added y axis here too
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
break;
}
} // end while 90
//////////////////////////////////////////////////////////////////////////
while ( i == 30 ) { // lots of rinsing and repeating.
if (distance>=40) {
analogWrite(spdA, 150);
analogWrite(spdB, 150);
analogWrite(spdC, 150);
analogWrite(spdD, 150);
digitalWrite(out1, LOW); // TURN RIGHT
digitalWrite(out2, HIGH);
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, HIGH);
digitalWrite(out8, LOW);
delay(500);
analogWrite(spdA, 0);
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
break;
}
if (distance<=40) {
analogWrite(spdA, 0); // added y axis here too
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
break;
}
} // end while
////////////////////////////////////////////////////////////////////////////
} // end for int
///////////////////////////////////////////////////////////////////////////////
for (int i=30; i<=150; i+=30) { // 30 degrees to 150
Serial.print(i);
RadarServo.write(i);
delay(1000);
distance = calculateDistance();
//////////////////////////////////////////////////////////////////////////////
while ( i == 30 ) {
if (distance>=40) {
analogWrite(spdA, 150);
analogWrite(spdB, 150);
analogWrite(spdC, 150);
analogWrite(spdD, 150);
digitalWrite(out1, LOW); // TURN RIGHT
digitalWrite(out2, HIGH);
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, HIGH);
digitalWrite(out8, LOW);
delay(500);
analogWrite(spdA, 0);
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
break;
}
if (distance<=40) {
analogWrite(spdA, 0); // added y axis here too
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
break;
}
} // end while 30
//////////////////////////////////////////////////////////////////////////////////////////////////
while ( i == 90 ) {
if ( distance>=40) {
distance = calculateDistance();
RadarServo.write(90);
analogWrite(spdA, 200);
analogWrite(spdB, 200);
analogWrite(spdC, 200);
analogWrite(spdD, 200);
digitalWrite(out1, HIGH);
digitalWrite(out2, LOW); // GO MOTORS
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
digitalWrite(out5, HIGH);
digitalWrite(out6, LOW);
digitalWrite(out7, HIGH);
digitalWrite(out8, LOW);
} // end if dist > 40
if ( distance <= 25 ) {
analogWrite(spdA, 200);
analogWrite(spdB, 200);
analogWrite(spdC, 200);
analogWrite(spdD, 200);
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH); // BACKWARDS, with 6v motors you can drive full speed into walls
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, LOW);
digitalWrite(out8, HIGH);
delay(400);
analogWrite(spdA, 0); // added y axis here too
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
} // close if d <25
if (distance <= 40) {
analogWrite(spdA, 0); // added y axis here too
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
break;
}
} // end while 90
//////////////////////////////////////////////////////////////////////////////////////
while (i ==150) {
distance = calculateDistance();
if ( distance>=40) {
analogWrite(spdA, 150);
analogWrite(spdB, 150);
analogWrite(spdC, 150);
analogWrite(spdD, 150);
digitalWrite(out1, HIGH); // TURN LEFT
digitalWrite(out2, LOW);
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
digitalWrite(out5, HIGH);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, HIGH);
delay(500);
analogWrite(spdA, 0); // added y axis here too
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
break;
} // end if dist > 40
if ( distance<=40) {
analogWrite(spdA, 150);
analogWrite(spdB, 150);
analogWrite(spdC, 150);
analogWrite(spdD, 150);
digitalWrite(out1, LOW); // TURN AROUND
digitalWrite(out2, HIGH);
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, HIGH);
digitalWrite(out8, LOW);
delay(2000);
analogWrite(spdA, 0);
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
break;
}
} // end while 150
////////////////////////////////////////////////////////////////////////
} // end for int
///////////////////////////////////////////////////////////////////////
for (int i=150; i>=90; i-=30) {
Serial.print(i);
RadarServo.write(i);
delay(1000);
distance = calculateDistance();
//////////////////////////////////////////////////////////////////////////
while ( i == 90 ) {
if ( distance>=40) {
distance = calculateDistance();
RadarServo.write(90);
analogWrite(spdA, 200);
analogWrite(spdB, 200);
analogWrite(spdC, 200);
analogWrite(spdD, 200);
digitalWrite(out1, HIGH);
digitalWrite(out2, LOW); // GO MOTORS
digitalWrite(out3, HIGH);
digitalWrite(out4, LOW);
digitalWrite(out5, HIGH);
digitalWrite(out6, LOW);
digitalWrite(out7, HIGH);
digitalWrite(out8, LOW);
} // end if dist > 40
if ( distance <= 25 ) {
analogWrite(spdA, 200);
analogWrite(spdB, 200);
analogWrite(spdC, 200);
analogWrite(spdD, 200);
digitalWrite(out1, LOW);
digitalWrite(out2, HIGH); // BACKWARDS, with 6v motors you can drive full speed into walls
digitalWrite(out3, LOW);
digitalWrite(out4, HIGH);
digitalWrite(out5, LOW);
digitalWrite(out6, HIGH);
digitalWrite(out7, LOW);
digitalWrite(out8, HIGH);
delay(400);
analogWrite(spdA, 0); // added y axis here too
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
} // close if d <25
if (distance <= 40) {
analogWrite(spdA, 0); // added y axis here too
analogWrite(spdB, 0);
analogWrite(spdC, 0);
analogWrite(spdD, 0);
digitalWrite(out1, LOW); // Idle, (not moving)
digitalWrite(out2, LOW); // See data received(CTRL + M.
digitalWrite(out3, LOW);
digitalWrite(out4, LOW);
digitalWrite(out5, LOW);
digitalWrite(out6, LOW);
digitalWrite(out7, LOW);
digitalWrite(out8, LOW);
delay(500);
break;
}
} // end while
////////////////////////////////////////////////////////////////////////
} // end for int
/////////////////////////////////////////////////////////////////////////
} // while mode =1
} // data 7
/////////////////////////////////////////////////////////////////////////
} // while rad avail
} // if rad avail
} // end loop
// to add sound file for blip https://poanchen.github.io/blog/2016/11/15/how-to-add-background-music-in-processing-3.0#:~:text=First%2C%20you%20need%20to%20have,wait%20for%20a%20few%20moments.
import processing.serial.*; // imports library for serial communication
import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
import java.io.IOException;
Serial myPort; // defines Object for Serial
String ang="";
String distance="";
String data="";
int angle, dist;
void setup() {
size (600, 400);
myPort = new Serial(this, Serial.list()[1], 9600); // starts the serial communication
// myPort = new Serial(this, Serial.list()[0], 9600); // 0 for other port
myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.' before calling serialEvent
background(0);
}
void draw() {
//for the blur effect
fill(0,5); //colour,opacity
noStroke();
rect(0, 0, width, height*0.93);
noStroke();
fill(0,255);
rect(0,height*0.93,width,height); // so that the text having angle and distance doesnt blur out
drawRadar();
drawLine();
drawObject();
drawText();
}
void serialEvent (Serial myPort) { // starts reading data from the Serial Port
// reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
data = myPort.readStringUntil('.');
data = data.substring(0,data.length()-1);
int index1 = data.indexOf(",");
ang= data.substring(0, index1);
distance= data.substring(index1+1, data.length());
angle = int(ang);
dist = int(distance);
System.out.println(angle);
}
void drawRadar()
{
pushMatrix();
noFill();
stroke(10,255,10); //green
strokeWeight(3);
translate(width/2,height-height*0.06);
line(-width/2,0,width/2,0);
arc(0,0,(width*0.125),(width*0.125),PI,TWO_PI);
arc(0,0,(width*0.25),(width*0.25),PI,TWO_PI);
arc(0,0,(width*0.375),(width*0.375),PI,TWO_PI);
arc(0,0,(width*0.5),(width*0.5),PI,TWO_PI);
arc(0,0,(width*0.625),(width*0.625),PI,TWO_PI);
arc(0,0,(width*0.75),(width*0.75),PI,TWO_PI);
arc(0,0,(width*0.875),(width*0.875),PI,TWO_PI);
arc(0,0,(width*0.995),(width*0.995),PI,TWO_PI);
line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
stroke(175,255,175);
strokeWeight(1);
line(0,0,(-width/2)*cos(radians(15)),(-width/2)*sin(radians(15)));
line(0,0,(-width/2)*cos(radians(45)),(-width/2)*sin(radians(45)));
line(0,0,(-width/2)*cos(radians(75)),(-width/2)*sin(radians(75)));
line(0,0,(-width/2)*cos(radians(105)),(-width/2)*sin(radians(105)));
line(0,0,(-width/2)*cos(radians(135)),(-width/2)*sin(radians(135)));
line(0,0,(-width/2)*cos(radians(165)),(-width/2)*sin(radians(165)));
popMatrix();
}
void drawLine() {
pushMatrix();
strokeWeight(9);
stroke(0,255,0);
translate(width/2,height-height*0.06);
line(0,0,(width/2)*cos(radians(angle)),(-width/2)*sin(radians(angle)));
popMatrix();
}
void drawObject() {
pushMatrix();
strokeWeight(9);
stroke(255,0,0);
translate(width/2,height-height*0.06);
float pixleDist = (dist/80.0)*(width/2.0); // covers the distance from the sensor from cm to pixels
float pd=(width/2)-pixleDist;
float x=-pixleDist*cos(radians(angle));
float y=-pixleDist*sin(radians(angle));
if(dist<=80) // limiting the range to 40 cms
{
//line(0,0,pixleDist,0);
line(-x,y,-x+(pd*cos(radians(angle))),y-(pd*sin(radians(angle))));
}
popMatrix();
}
void drawText()
{
pushMatrix();
fill(192,192,192);
textSize(10);
text("10cm",(width/2.3)+(width*0.115),height*0.93);
text("20cm",(width/2.3)+(width*0.1775),height*0.93);
text("30cm",(width/2.3)+(width*0.24),height*0.93);
text("40cm",(width/2.3)+(width*0.3025),height*0.93);
text("50cm",(width/2.3)+(width*0.365),height*0.93);
text("60cm",(width/2.3)+(width*0.4275),height*0.93);
text("70cm",(width/2.3)+(width*0.49),height*0.93);
text("80cm",(width/2.3)+(width*0.5525),height*0.93);
textSize(15);
text("Radar",width*0.08,height*0.99);
text("Angle :"+angle,width*0.45,height*0.99);
if(dist<=80) {
text("Distance :"+dist,width*0.7,height*0.99);
}
translate(width/2,height-height*0.06);
textSize(15);
text(" 30°",(width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
text(" 60°",(width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
text("90°",(width/2)*cos(radians(91)),(-width/2)*sin(radians(90)));
text("120°",(width/2)*cos(radians(123)),(-width/2)*sin(radians(118)));
text("150°",(width/2)*cos(radians(160)),(-width/2)*sin(radians(150)));
popMatrix();
}
Comments
Please log in or sign up to comment.