Hi, I am tring to put the sketch below onto arduino leonardo to control 4 wheel drive car with wifi camera. The tutorial where i recieved code was for arduino uno. I am aware of a change in serial to serial1 or something along these lines with leonardo. However I am completely new to this and lost as to how to adapt this code for leonardo. I have the project built now and need to get working this week. Could anyone help me get this working on Leonardo please?
Elecrow_WIFI_Car.ino (2.94 KB)
pert
January 16, 2018, 11:11pm
2
You should be fine if you just change all instances of Serial in the code to Serial1.
Thanks very much for the reply, I was thinking this but doesnt seem to work. Should I change any place it says serial to serial1. For example "byte serialIn = 0;" to "byte serial1In = 0;" or just where serial is on its own like " Serial.begin(9600); " to " Serial1.begin(9600);"
Sorry for the possibly stupid questions I have been thrown in the deep end here.
I did not look at the code, but it should only be Serial.begin, Serial.read and Serial.print/write.
No you only need to change the name of the Serial instance:
#include <Servo.h>
int MotorLeft1 =8; //IN1
int MotorLeft2 = 11; //IN2
int MotorRight1 = 12; //IN3
int MotorRight2 = 13; //IN4
int EA = 9; //enable out1
int EB = 10; //enable out2
//Status of four motor
uint8_t MotorLeft1Status = LOW;
uint8_t MotorLeft2Status = LOW;
uint8_t MotorRight1Status = LOW;
uint8_t MotorRight2Status = LOW;
Servo servoX;
Servo servoY;
byte serialIn = 0;
byte commandAvailable = false;
String strReceived = "";
//current speed of motor
byte leftspeed = 0;
byte rightspeed = 0;
void setup()
{
pinMode(MotorLeft1, OUTPUT);
pinMode(MotorLeft2, OUTPUT);
pinMode(MotorRight1, OUTPUT);
pinMode(MotorRight2, OUTPUT);
Serial1.begin(9600);
}
void loop()
{
getSerialLine();
if (commandAvailable) {
processCommand(strReceived);
strReceived = "";
commandAvailable = false;
}
}
void getSerialLine()
{
while (serialIn != '\r')
{
if (!(Serial1.available() > 0))
{
return;
}
serialIn = Serial1.read();
if (serialIn != '\r') {
if (serialIn != '\n'){
char a = char(serialIn);
strReceived += a;
}
}
}
if (serialIn == '\r') {
commandAvailable = true;
serialIn = 0;
}
}
void processCommand(String input)
{
byte iscommand = true;
int val;
if (command == "MD_Qian")
{
advance();
}
else if (command == "MD_Hou")
{
back();
}
else if (command == "MD_Zuo")
{
left();
}
else if (command == "MD_You")
{
right();
}
else if (command == "MD_Ting")
{
Stop();
}
else if (command == "MD_SD")
{
val = getValue(input, ' ', 1).toInt();
leftspeed = val;
val = getValue(input, ' ', 2).toInt();
rightspeed = val;
}
else if (command == "DJ_CS")
{
void advance(void)
{
MotorLeft1Status = LOW;
MotorLeft2Status = HIGH;
MotorRight1Status = LOW;
MotorRight2Status = HIGH;
SetEN();
}
void back(void)
{
MotorLeft1Status = HIGH;
MotorLeft2Status = LOW;
MotorRight1Status = HIGH;
MotorRight2Status = LOW;
SetEN();
}
void right(void)
{
MotorLeft1Status = LOW;
MotorLeft2Status = HIGH;
MotorRight1Status = HIGH;
MotorRight2Status = LOW;
SetEN();
}
void left(void)
{
MotorLeft1Status = HIGH;
MotorLeft2Status = LOW;
MotorRight1Status = LOW;
MotorRight2Status = HIGH;
SetEN();
}
void Stop(void)
{
leftspeed = 0;
rightspeed = 0;
MotorLeft1Status = LOW;
MotorLeft2Status = LOW;
MotorRight1Status = LOW;
MotorRight2Status = LOW;
SetEN();
}
void SetEN(){
analogWrite(EA, leftspeed);
analogWrite(EB, rightspeed);
digitalWrite(MotorLeft1, MotorLeft1Status);
digitalWrite(MotorLeft2, MotorLeft2Status);
digitalWrite(MotorRight1, MotorRight1Status);
digitalWrite(MotorRight2, MotorRight2Status);
}
void SendStatus(){
String out = "";
out += MotorLeft1Status;
out += ",";
out += MotorLeft2Status;
out += ",";
out += MotorRight1Status;
out += ",";
out += MotorRight2Status;
out += ",";
out += leftspeed;
out += ",";
out += rightspeed;
out += ",";
out += servoXPoint;
out += ",";
out += servoYPoint;
SendMessage(out);
}
void SendMessage(String data){
Serial1.println(data);
}