Hi all, first of all introduce myself, my name is Eloi and I'm building a robot. These are the characteristics of the robot and its controller:
Controller:
- 1 Arduino Mega 2560 R3
- 1 Joystick
- 5 push buttons
- One lcd 40x4
- 1 matrix numbers
- One antenna rf
robot:
- 1 Arduino Mega 2560 R3
- 1 MD22 controller motors (0-255 analog pins)
- 4 Motors (two are coupled)
- One lcd 16x2
- One gps
- 1 bluetooth
- 1 Rf Antenna
The problem is this: The system sends and receives data from the joystick perfectly, but not how to do that with a single joystick to control the two motors perfectly.
The robot is as follows:
m1--- ---m2
| |
| |
m1--- ---m2
I need help with the code please, for the moment the robot code I have is this:
#include <LiquidCrystal.h>
#include <Wire.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#define md22Address 0x58 // address of md 22 (all mode switches on)
#define softReg 0x07 // Byte for reading software register
#define motor1 0x01 // Byte for first motor
#define motor2 0x02 // Byte for second motor
#define accelReg 0x03
void setup()
{
Serial.begin(19200);
Serial3.begin(19200);
//Pantalla
lcd.begin(16, 2);
}
void loop()
{
a13 = Serial3.parseInt();
// do it again:
a14= Serial3.parseInt();
if(((a13 >= 490 && a13 <= 560) && (a14 >= 490 && a14 <= 560)))
{
Wire.beginTransmission(md22Address); // Set first motor to a speed of 255
Wire.write(motor1);
Wire.write(128);
Wire.endTransmission();
Wire.beginTransmission(md22Address); // Set second motor to stop
Wire.write(motor2);
Wire.write(128);
Wire.endTransmission();
}
else
{
int x1 = map(a13, 1023, 0, 0, 255);
int x2 = map(a14, 1023, 0, 0, 255);
Wire.beginTransmission(md22Address); // Set first motor to speed 0
Wire.write(motor1);
Wire.write(x1);
Wire.endTransmission();
Wire.beginTransmission(md22Address); // Set second motor to stop
Wire.write(motor2);
Wire.write(x1);
Wire.endTransmission();
lcd.setCursor(0,1);
lcd.print(x1);
lcd.print(", ");
}
}