Hi guys,
I have been using arduino for more then a year now, and currently I'm working on my school's science festival project , which is a remote controlled robot, where I used a joystick shield on an UNO,an nrf24lo1 wireless module and 2 leds,1 green and one red, and the robot itself contains an ultrasonic sensor,2 continuous rotation servos and a clone of the NANO.The robot is supposed to see if the distance away from the robot is near or far(farther than 30 cm), and if it sees itself near it will send a signal to the remote telling it to turn on a red led .I still didn't finish the remote cod as i had problems with the code earlier.
I've written a code for both using some help from previous questions in this forum, but I still have some issues with the code.
I wish the anyone could make improvements to my codes as this is an urgent matter.
pS.this the whole code not a snippet.Also,please be patient with me.I also used the joystick shield.
Here's the code for the remote:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9// pin 3
#define CSN_PIN 10//pin 4
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
const uint64_t pipe = 0xE8E8F0F0E1LL;//pipe
const byte PIN_ANALOG_X = A0;
const byte PIN_ANALOG_Y = A1;
const byte SELECT_BUTTON = 2;
const byte RIGHT_BUTTON = 3;
const byte UP_BUTTON = 4;
const byte DOWN_BUTTON = 5;
const byte LEFT_BUTTON = 6;
int x_axis;
int y_axis;
int select;
int right;
int left;
int down;
void setup() {
pinMode(SELECT_BUTTON,INPUT);
pinMode(RIGHT_BUTTON,INPUT);
pinMode(UP_BUTTON,INPUT);
pinMode(DOWN_BUTTON,INPUT);
pinMode(LEFT_BUTTON,INPUT);
radio.begin();
radio.openWritingPipe(pipe);
radio.openReadingPipe(1,pipe);
}
void loop() {
select = digitalRead(SELECT_BUTTON);
right = digitalRead(RIGHT_BUTTON);
down = digitalRead(DOWN_BUTTON);
left = digitalRead(LEFT_BUTTON);
x_axis = analogRead(PIN_ANALOG_X);
x_axis = map(x_axis,0,1023,0,360);
delay(1000);
radio.write(map(x_axis,0,1023,0,360));
delay(1000);
radio.write(analogRead(PIN_ANALOG_Y));
}
Here's the robot code:
#define Trigger 8
#define Echo A1
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9//pin 3
#define CSN_PIN 10//pin 4
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
int received[1];
#include <Servo.h>
Servo leftservo;
Servo rightservo;
int minrange = 30;
const int redled = 12;
const int greenled = 11;
long duration,distance;
int command_ = 0;
int x = 0;
int y = 0;
void setup() {
leftservo.attach(6);
rightservo.attach(7);
pinMode(redled,OUTPUT);
pinMode(greenled,OUTPUT);
pinMode(Trigger,OUTPUT);
pinMode(8, OUTPUT);
delay(1000);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.openWritingPipe(pipe);
radio.startListening();
}
void loop() {
digitalWrite(Trigger,LOW);
delayMicroseconds(2);
digitalWrite(Trigger,HIGH);
delayMicroseconds(10);
digitalWrite(Trigger,LOW);
duration=pulseIn(Echo,HIGH);
distance=duration/58.2;
if (distance <= 30){
radio.write("warning",sizeof("warning"));}
if (radio.available()){
radio.read(received,sizeof(received));
leftservo.write(x);
rightservo.write(x);
delay(800);
radio.read(received,sizeof(received));
if (radio.read(received,sizeof(received)) >= 520){
rightservo.write(x+10);
delay(500);
rightservo.write(x);}
if (y <= 505){
leftservo.write(x+10);
delay(500);
leftservo.write(x);}
}
}