Hello all
My project uses two Arduinos, one with a pair of joysticks and a 433Mhz transmitter and a receiver and two DC motors controlled by an L293D IC on the other.
The code is simple, the data transfer part was perfected by jremmington last week. And now, as it stands, the joystick 1 and 2, X and Y data is transferred to the second Arduino and displays ok on the serial monitor. When the joysticks are moved, the displayed data adjusts as expected.
My problem is that only motor no.1 works and I have to rem out the code for motor no.2 for that to happen! The Y axis centre point values of the two joysticks are used for zero motor speed. Greater than 512 is 0 - 255 speed clockwise and less than 512 is used for 0 - 255 reverse speed. (They're not actually exactly 512 but you get what I mean)
If I upload the code for motor no.2 then all four values display as 0 and motor no.1 runs backwards at full speed.
If I flip the motor control chip the symptoms are the same, so the hard ware is connected correctly. I've triple checked the code and re-written it from scratch but the symptom persists!
The first version used some y = M x + C maths to calculate the 8 bit motor speed value, but that version only had one motor and it worked. The current version uses the Map() function. Is there a quirk of the Map() function that I need to be aware of or have I typed a schoolboy error into my code?
I would be grateful if someone could shed some light please.
Graeme
/*
*
* The Bait Boat
* Rev 03_02_Receiver - Receiver Controlling Two DC motors
* 25/04/24
*
* Description
* Prototype to test the Transmitter/Receiver Pair. Joystick controlling the Transmitter and Receiver controlling an L293D IC and DC Motors
*
* Rev History
* Rev 01.01 - DC Motor control
* Rev 02.01_Transmitter - With text message string
* Rev 02.02_Transmitter - Transmitter string text message
* Rev 02.02_Receiver - Receiver string text message
* Rev 02.03_Transmitter - Transmitter with Joystick position string
* Rev 02.03_Receiver - Receiver
* Rev 02.04_Transmitter - Transmitter with Dual Joysticks and 2 x 4 character string
* Rev 02.04_Receiver - Receiver
* Rev 02.05_Transmitter - jremington
* Rev 02.05_Receiver - jremington
* Rev 03_01_Transmitter - Transmitter Controlled by Joystick
* Rev 03_01_Receiver - Receiver Controlling a DC motor
* Rev 03_02_Transmitter - Transmitter Controlled by Two Joysticks
* Rev 03_02_Receiver - Receiver Controlling Two DC motors
*
*/
// Libraries
#include <RadioHead.h>
#include <RH_ASK.h>
#include <SPI.h>
// Variables
int JoyStick1XVal;
int JoyStick1YVal;
int JoyStick2XVal;
int JoyStick2YVal;
int JoyStick[4];
int Speed1Pin = 5; // Motor No1 Left - EN1(1)
int Direction1Pin1 = 4; // Motor Black - IN1(2)
int Direction1Pin2 = 3; // Motor Red - IN2(7)
int Motor1Speed;
int Speed2Pin = 9; // Motor No2 Right - EN2(9)
int Direction2Pin1 = 8; // Motor Black - IN3(10)
int Direction2Pin2 = 7; // Motor Red - IN4(15)
int Motor2Speed;
int LoopDelay = 200;
// Create RH Driver
RH_ASK rf_driver;
void setup()
{
Serial.begin (9600);
rf_driver.init(); // Arduino Pin 11
pinMode (Speed1Pin, OUTPUT);
pinMode (Direction1Pin1, OUTPUT);
pinMode (Direction1Pin2, OUTPUT);
pinMode (Speed2Pin, OUTPUT);
pinMode (Direction2Pin1, OUTPUT);
pinMode (Direction2Pin2, OUTPUT);
}
void loop()
{
uint8_t JoySticklen = sizeof JoyStick;
if (rf_driver.recv((uint8_t *) JoyStick, &JoySticklen))
{
JoyStick1XVal = JoyStick[0];
JoyStick1YVal = JoyStick[1];
JoyStick2XVal = JoyStick[2];
JoyStick2YVal = JoyStick[3];
}
if (JoyStick1YVal < 503)
{
digitalWrite (Direction1Pin1, LOW); // Pin1 High - Pin2 Low - Clockwise
digitalWrite (Direction1Pin2, HIGH); // Pin2 High - Pin1 Low - Anti Clockwise
Motor1Speed = map(JoyStick1YVal,503,0,0,250);
analogWrite (Speed1Pin,Motor1Speed);
}
if (JoyStick1YVal >= 503)
{
digitalWrite (Direction1Pin1, HIGH); // Pin1 High - Pin2 Low - Clockwise
digitalWrite (Direction1Pin2, LOW); // Pin2 High - Pin1 Low - Anti Clockwise
Motor1Speed = map(JoyStick1YVal,503,1023,0,250);
analogWrite (Speed1Pin,Motor1Speed);
}
/*
if (JoyStick2YVal < 530)
{
digitalWrite (Direction2Pin1, HIGH); // Pin1 High - Pin2 Low - Clockwise
digitalWrite (Direction2Pin2, LOW); // Pin2 High - Pin1 Low - Anti Clockwise
Motor2Speed = map(JoyStick2YVal,530,0,0,250);
analogWrite (Speed2Pin,Motor2Speed);
}
if (JoyStick2YVal >= 530)
{
digitalWrite (Direction2Pin1, LOW); // Pin1 High - Pin2 Low - Clockwise
digitalWrite (Direction2Pin2, HIGH); // Pin2 High - Pin1 Low - Anti Clockwise
Motor2Speed = map(JoyStick2YVal,530,1023,0,250);
analogWrite (Speed2Pin,Motor2Speed);
}
*/
Serial.print(JoyStick1XVal);
Serial.print(" - ");
Serial.print(JoyStick1YVal);
Serial.print(" - ");
Serial.print(JoyStick2XVal);
Serial.print(" - ");
Serial.println(JoyStick2YVal);
delay(LoopDelay);
}
// EoF