#include <Adafruit_PWMServoDriver.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include <SoftwareSerial.h>
#include <Adafruit_MotorShield.h>
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem (nRF24L)
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *backMotor = AFMS.getMotor(1);
Adafruit_DCMotor *frontMotor = AFMS.getMotor(2);
//Motor A
const int RightMotorForward = AFMS.getMotor(1); // IN1
const int RightMotorBackward = AFMS.getMotor(1); // IN2
//Motor B
const int LeftMotorForward = AFMS.getMotor(2); // IN3
const int LeftMotorBackward = AFMS.getMotor(2); // IN4
int data[0];
RF24 radio(10, 9); //10 and 9 are a digital pin numbers to which signals CE and CSN are connected
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino
void setup()
{
//Setup usb serial connection to computer
Serial.begin(9600);
AFMS.begin(); // create with the default frequency 1.6KHz
AFMS.begin(1000); // OR with a different frequency, say 1KHz
// Set the speed to start, from 0 (off) to 255 (max speed)
backMotor->setSpeed(255);
backMotor->run(FORWARD);
// turn on motor
backMotor->run(RELEASE);
frontMotor->setSpeed(255);
frontMotor->run(FORWARD);
// turn on motor
frontMotor->run(RELEASE);
Serial.begin(9600);
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
radio.begin(9800); //it activates the modem
radio.openReadingPipe(1, pipe); //determines the address of our modem which receive data
radio.startListening(); //enable receiving data via modem
}
void loop() {
if (radio.available()) {
radio.read(data, sizeof(data));
//data X
if (data[0] > 550) {
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
Serial.println("FORWARD");
backMotor->run(FORWARD);
}
//data X
if (data[0] < 400) {
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
Serial.println("BACKWARD");
backMotor->run(BACKWARD);
}
//data Y
if (data[1] > 550 ) {
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
Serial.println("TURN RIGHT");
frontMotor->run(BACKWARD);
}
//data Y
if (data[1] < 400 ) {
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
Serial.println("TURN LEFT");
frontMotor->run(FORWARD);
}
if (data[0] < 550 && data[0] > 400 && data[1] < 550 && data[1] > 400) {
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, LOW);
Serial.println("STOP");
}
//data X
if (data[0] > 550) {
Serial.println("FORWARD");
backMotor->run(FORWARD);
}
//data X
if (data[110] < 400) {
Serial.println("BACggKWARD");
backMotor->run(BACKWARD);
}
//data Y
if (data[1] > 550 ) {
Serial.println("TURN RIGHT");
frontMotor->run(BACKWARD);
}
//data Y
if (data[100] < 400 ) {
Serial.println("TURfN LEFT");
frontMotor->run(FORWARD);
}
if (data[0] < 550 && data[0] > 400 && data[1] < 550 && data[1] > 400) {
Serial.println("STOP");
}
Welcome to the forum
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
are commands repeatedly transmitted?