I have a sketch (first one) that runs two motors with a L298N and a joystick. This works on a breadboard perfectly like I want it.
However, I want to separate the joystick from the motors (the vehicle) with nRF24L01s.
I have two more sketches, which work, transmit and receive, that run a servo back and forth with a joystick. But being a newb to programming, I cannot figure out how to make the DC motor sketch with the wireless joystick of the 2nd one.
I can get the joystick readings on a receiver sketch, but I guess I'm not sure what to do with them.
Thank you, steve
/* L298N Motor Control Demonstration with Joystick
L298N-Motor-Control-Demo-Joystick.ino
Demonstrates use of Joystick control with Arduino and L298N Motor Controller
DroneBot Workshop 2017
http://dronebotworkshop.com
*/
// Motor A
int enA = 6;
int in1 = 8;
int in2 = 7;
// Motor B
int enB = 3;
int in3 = 5;
int in4 = 4;
// Joystick Input
int joyVert = A0; // Vertical
int joyHorz = A1; // Horizontal
// Motor Speed Values - Start at zero
int MotorSpeed1 = 0;
int MotorSpeed2 = 0;
// Joystick Values - Start at 512 (middle position)
int joyposVert = 512;
int joyposHorz = 512;
void setup()
{
Serial.begin(9600);
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// Start with motors disabled and direction forward
// Motor A
digitalWrite(enA, LOW);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Motor B
digitalWrite(enB, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
}
void loop() {
// Read the Joystick X and Y positions
joyposVert = analogRead(joyVert);
joyposHorz = analogRead(joyHorz);
Serial.print("X = ");
Serial.print(joyposVert);
Serial.print(" Y = ");
Serial.println(joyposHorz);
// Determine if this is a forward or backward motion
// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction
if (joyposVert < 460)
{
// This is Backward
// Set Motor A backward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B backward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
//Determine Motor Speeds
// As we are going backwards we need to reverse readings
joyposVert = joyposVert - 460; // This produces a negative number
joyposVert = joyposVert * -1; // Make the number positive
MotorSpeed1 = map(joyposVert, 0, 460, 0, 100);
MotorSpeed2 = map(joyposVert, 0, 460, 0, 100);
}
else if (joyposVert > 564)
{
// This is Forward
// Set Motor A forward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B forward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
//Determine Motor Speeds
MotorSpeed1 = map(joyposVert, 564, 1023, 0, 100);
MotorSpeed2 = map(joyposVert, 564, 1023, 0, 100);
}
else
{
// This is Stopped
MotorSpeed1 = 0;
MotorSpeed2 = 0;
}
// Now do the steering
// The Horizontal position will "weigh" the motor speed
// Values for each motor
if (joyposHorz < 460)
{
// Move Left
// As we are going left we need to reverse readings
joyposHorz = joyposHorz - 460; // This produces a negative number
joyposHorz = joyposHorz * -1; // Make the number positive
// Map the number to a value of 255 maximum
joyposHorz = map(joyposHorz, 0, 460, 0, 100);
MotorSpeed1 = MotorSpeed1 - joyposHorz;
MotorSpeed2 = MotorSpeed2 + joyposHorz;
// Don't exceed range of 0-255 for motor speeds
if (MotorSpeed1 < 0)MotorSpeed1 = 0;
if (MotorSpeed2 > 255)MotorSpeed2 = 255;
}
else if (joyposHorz > 564)
{
// Move Right
// Map the number to a value of 255 maximum
joyposHorz = map(joyposHorz, 564, 1023, 0, 100);
MotorSpeed1 = MotorSpeed1 + joyposHorz;
MotorSpeed2 = MotorSpeed2 - joyposHorz;
// Don't exceed range of 0-255 for motor speeds
if (MotorSpeed1 > 255)MotorSpeed1 = 255;
if (MotorSpeed2 < 0)MotorSpeed2 = 0;
}
// Adjust to prevent "buzzing" at very low speed
if (MotorSpeed1 < 20)MotorSpeed1 = 0;
if (MotorSpeed2 < 20)MotorSpeed2 = 0;
// Set the motor speeds
analogWrite(enA, MotorSpeed1);
analogWrite(enB, MotorSpeed2);
}
TRANSMIT
/*-----TRANSMIT-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
#define POT_X A0
#define POT_Y A1
//#define POT_A A2
//#define POT_B A3
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int pot[2];
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
}
void loop()
{
pot[0] = analogRead(POT_X);
pot[1] = analogRead(POT_Y);
//pot[2] = analogRead(POT_A);
//pot[3] = analogRead(POT_B);
radio.write( pot, sizeof(pot) );
Serial.print("X = ");
Serial.print(pot[0]);
Serial.print(" Y = ");
Serial.println(pot[1]);
}]
RECEIVER
/*-----RECEIVE-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN 9
#define CSN_PIN 10
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int pot[2]; // 2 element array holding Joystick readings
#include <Servo.h> // include the servo library
Servo servo1;
Servo servo2;
unsigned long prevMillis = 0;
long interval = 500;
int switchPin = 7;
int pos;
int pos2;
void setup()
{
Serial.begin(9600);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
;
// servo1.attach(5);
// servo2.attach(6);
pinMode(switchPin, INPUT_PULLUP);
}
void loop()
{
//if (digitalRead(switchPin) == HIGH)
// {
// Read the data payload until we've received everything
bool done = false;
if (!done)
{
int analogValue = pot[0]; // read the analog input
int analogValue2 = pot[1]; // read the analog input
int servoAngle = map(analogValue, 0, 1023, 50, 130);
int servoAngle2 = map(analogValue2, 0, 1023, 50, 130);
// Fetch the data payload
radio.read( pot, sizeof(pot) );
Serial.print("Xo = ");
Serial.print(pot[0]);
Serial.print(" Y = ");
Serial.println(pot[1]);
// servo1.write(servoAngle);
delay (20);
// servo2.write(servoAngle2);
delay (20);
}
}
/* else
{
unsigned long newMillis = millis();
if (newMillis - prevMillis > interval)
{
prevMillis = newMillis;
int pos = random(80, 100);
int pos2 = random(50, 130);
servo1.write(pos);
servo2.write(pos2);
}
}
}*/