Arduino Program
Arduino Program
h"
#define D1 2
// direction of motor rotation 1
#define M1 3
// PWM left motor
#define D2 4
// direction of motor rotation 2
#define M2 5
// PWM right motor
#define HORN 13
// additional channel 1
//#define autoOFF 2500 // milliseconds after which the robot stops when the con
nection
#define
#define
#define
#define
#define
#define
cmdL
cmdR
cmdH
cmdF
cmdr
cmdw
'L'
'R'
'H'
'F'
'r'
'w'
//
//
//
//
//
//
UART-command
UART-command
UART-command
UART-command
UART-command
UART-command
char incomingByte;
// incoming data
char
byte
char
byte
char
byte
char
byte
char
//
//
//
//
//
//
//
//
//
for
for
for
for
for
for
L_Data[4];
L_index = 0;
R_Data[4];
R_index = 0;
H_Data[1];
H_index = 0;
F_Data[8];
F_index = 0;
command;
left motor
right motor
additional channel (for example Horn)
EEPROM operation
EEPROM operation (read)
EEPROM operation (write)
left motor
L
right motor
R
additional channel
H
EEPROM
F
//
//
//
//
initialization UART
additional channel
output for motor rotation
output for motor rotation
void timer_init() {
uint8_t sw_autoOFF = EEPROM.read(0);
pping the car when losing connection"
if(sw_autoOFF == '1'){
char var_Data[3];
var_Data[0] = EEPROM.read(1);
var_Data[1] = EEPROM.read(2);
var_Data[2] = EEPROM.read(3);
autoOFF = atoi(var_Data)*100;
}
else if(sw_autoOFF == '0'){
autoOFF = 999999;
}
else if(sw_autoOFF == 255){
autoOFF = 2500;
e is 2.5 sec
}
// variable autoOFF ms
currentTime = millis();
ion start
}
void loop() {
if (Serial.available() > 0) {
//
incomingByte = Serial.read();
//
if(incomingByte == cmdL) {
//
command = cmdL;
//
memset(L_Data,0,sizeof(L_Data)); //
L_index = 0;
//
}
else if(incomingByte == cmdR) {
//
command = cmdR;
memset(R_Data,0,sizeof(R_Data));
R_index = 0;
}
else if(incomingByte == cmdH) {
//
nel
command = cmdH;
memset(H_Data,0,sizeof(H_Data));
H_index = 0;
}
else if(incomingByte == cmdF) {
//
command = cmdF;
memset(F_Data,0,sizeof(F_Data));
F_index = 0;
}
else if(incomingByte == '\r') command =
else if(incomingByte == '\t') command =
'e';
't';
// end of line
// end of line for EEPROM op
// store each byte in the arr
ay
L_index++;
// increment array index
}
else if(command == cmdR && incomingByte != cmdR){
R_Data[R_index] = incomingByte;
R_index++;
}
else if(command == cmdH && incomingByte != cmdH){
H_Data[H_index] = incomingByte;
H_index++;
}
else if(command == cmdF && incomingByte != cmdF){
F_Data[F_index] = incomingByte;
F_index++;
}
else if(command == 'e'){
// if we take the line end
Control4WD(atoi(L_Data),atoi(R_Data),atoi(H_Data));
delay(10);
}
else if(command == 't'){
// if we take the EEPROM line
end
Flash_Op(F_Data[0],F_Data[1],F_Data[2],F_Data[3],F_Data[4]);
}
lastTimeCommand = millis();
// read the time elapsed sinc
e application start
}
if(millis() >= (lastTimeCommand + autoOFF)){
// compare the current timer
//
//
//
//
set
set
set
set
speed for
speed for
direction
direction
left motor
right motor
of left motor rotation
of right motor rotation
digitalWrite(HORN, Horn);
// additional channel
}
void Flash_Op(char FCMD, uint8_t z1, uint8_t z2, uint8_t z3, uint8_t z4){
if(FCMD == cmdr){
// if EEPROM data read command
Serial.print("FData:");
// send EEPROM data
Serial.write(EEPROM.read(0));
// read value from the memory with 0 addre
ss and print it to UART
Serial.write(EEPROM.read(1));
Serial.write(EEPROM.read(2));
Serial.write(EEPROM.read(3));
Serial.print("\r\n");
// mark the end of the transmission of data EE
PROM
}
else if(FCMD == cmdw){
// if EEPROM data write command
EEPROM.write(0,z1);
// z1 record to a memory with 0 address
EEPROM.write(1,z2);
EEPROM.write(2,z3);
EEPROM.write(3,z4);
timer_init();
// reinitialize the timer
Serial.print("FWOK\r\n");
// send a message that the data is success
fully written to EEPROM
}
}