Hi,
I have a tank built with wifi controller. When I put the stick forward the motors go in that direction but they don't stop when the stick is centered or you've "let go" of the stick.
Am I blind? What am I missing here... I have 2 twin 3 year olds just DYING to get this going haha
Here's the Receiver Code:
/*
---- Receiver Code ----
Mert Arduino Tutorial & Projects (YouTube)
Please Subscribe for Support
*/
#include <Servo.h> //the library which helps us to control the servo motor
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem (nRF24L)
//define our L298N control pins
//Motor A
const int RightMotorForward = 2; // IN1
const int RightMotorBackward = 3; // IN2
//Motor B
const int LeftMotorForward = 4; // IN3
const int LeftMotorBackward = 6; // IN4
//define the servo name
Servo myServo;
RF24 radio(5,10); /*This object represents a modem connected to the Arduino.
Arguments 5 and 10 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.
int data[1];
void setup(){
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
//define the servo input pins
myServo.attach(14); //A0
radio.begin(); //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, 1);
if(data[0] < 11 && data[0] > 6){
// This is backward
// Set a Motor A backward
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
// Set a Motor B backward
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
}
if(data[0] > -1 && data[0] < 4){
// This is forward
// Set a Motor A forward
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
// Set a Motor B forward
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
}
if (data[0] == 5){
// Stop Motors
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
// This is Backward
// Set a Motor A Backward
if(data[0] < 21 && data[0] > 16){
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
// Set a Motor B Backward
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
}
// Turn Right
if(data[0] > 10 && data[0] < 14){
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
}
// Turn Left
if(data[0] == 15){
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
// for the servo motor
if(data[0] < 31 && data[0] > 21){
int potValue = data[0];
int potPos = map(potValue, 21, 30, 10, 170);
myServo.write(potPos);
}
}
}
Here's the Transmitter Code:
/*
---- Transmitter Code ----
Mert Arduino Tutorial & Projects (YouTube)
Please Subscribe for Support
*/
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem
//define the input pins
int x_axis = A0;
int y_axis = A1;
int potPin = A2;
//define variable values
int xValue;
int yValue;
int potValue;
int data[1];
RF24 radio(5,10); //5 and 10 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 Arduino.
void setup(void){
Serial.begin(9600);
radio.begin(); //it activates the modem.
radio.openWritingPipe(pipe); //sets the address of the receiver to which the program will send data.
}
void loop(){
//Send X-axis data
xValue = analogRead(x_axis);
xValue = map(xValue, 0, 1023, 0, 10);
data[0] = xValue;
radio.write(data, 1);
//Send Y-axis data
yValue = analogRead(y_axis);
yValue = map(yValue, 0, 1023, 11, 20);
data[0] = yValue;
radio.write(data, 1);
//Send Potentiometer data
potValue = analogRead(potPin);
potValue = map(potValue, 0, 1023, 21, 30);
data[0] = potValue;
radio.write(data, 1);
}