Trying to implement serial data transmission using two digital pins other than rx and tx between arduino and esp32

297 views Asked by At

I am developing a car robot for that I am using arduino and motor driving shield. I am trying to send commands from esp32 to arduino(as I am getting commands to esp32 from internet). commands like forward, backward, right, left, autonomous, manual.

as rx and tx are used by motor driving shield, I decided to transfer data from esp32 to arduino through custom serial communication(by sending pulses for certain amount of durations)

arduino code:

void setup() {
  Serial.begin(9600);
  Serial.println("started");
  pinMode(cmd_pin, INPUT);
  pinMode(data_validator, OUTPUT);
  digitalWrite(data_validator, HIGH);
}

String check(){
  //String s = "";
  int serial_data = digitalRead(cmd_pin);
  if(serial_data==LOW){
    delay(10);
    digitalWrite(data_validator, LOW);
    unsigned long high_dur = pulseIn(cmd_pin, HIGH);
    unsigned long low_dur = pulseIn(cmd_pin, LOW);
    Serial.println("HIGH ");
    Serial.println(high_dur);
    Serial.println("low ");
    Serial.println(low_dur);
    if(high_dur<=5000 && low_dur>24000){
      automatic = false;
      return "Forward";
    }
    else if(high_dur<=10000 && low_dur>19000){
      automatic = false;
      return "Backward";
    }
    else if(high_dur<=15000 && low_dur>14000){
      automatic = false;
      return "Turing right";
    }
    else if(high_dur<=20000 && low_dur>9000){
      automatic = false;
      return "Turing left";
    }
    else if(high_dur<=25000 && low_dur>4000){
      automatic = true;
      return "autonomous";
    }
    else if(high_dur<=30000 && low_dur>1900){
      automatic = false;
      return "manual";
    }
    digitalWrite(data_validator, HIGH);
  }
}

void loop(){
  command = check(); 
  //ruuning command
  Serial.println(command);
//  if (command=="autonomous")
//    auton();
//  else if (command=="manual")
//    moveStop();
//  else if (command=="Turing left")
//    turnLeft();
//  else if (command=="Turing right")
//    turnRight();
//  else if (command=="Forward")
//    moveForward();
//  else if (command=="Backward")
//    moveBackward();
//  else{
//    if (automatic)
//      auton();
//}
}

  1. information is transferred through cmd_pin,.
  2. when low occurs in cmd_pin Arduino set data validator to low denotes the esp32 to send data
  3. high_duration and low_duration are measure and decision is taken based on the duration.

esp32 code:

void manual(){
  serial_busy = true;
  digitalWrite(cmd_pin, LOW);
  while (digitalRead(data_validator)!=LOW);
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(30);
  digitalWrite(cmd_pin, LOW);
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(1);
  digitalWrite(cmd_pin,LOW);
  delay(2);
  digitalWrite(cmd_pin,HIGH);
  
  serial_busy = false;
  delay(10);
}

void forward(){
  
  serial_busy = true;
  Serial.println("Forward");
  digitalWrite(cmd_pin, LOW);
  while (digitalRead(data_validator)!=LOW);
  
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(5);
  digitalWrite(cmd_pin, LOW);
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(1);
  digitalWrite(cmd_pin,LOW);
  delay(25);
  digitalWrite(cmd_pin,HIGH);
  
  serial_busy = false;
  delay(10);
}

void backward(){
  
  serial_busy = true;
  Serial.println("Backward");
  digitalWrite(cmd_pin, LOW);
  while (digitalRead(data_validator)!=LOW);
  
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(10);
  digitalWrite(cmd_pin, LOW);
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(1);
  digitalWrite(cmd_pin,LOW);
  delay(20);
  digitalWrite(cmd_pin,HIGH);
  
  serial_busy = false;
  delay(10);
}

void right(){
  
  serial_busy = true;
  Serial.println("Right");
  digitalWrite(cmd_pin, LOW);
  while (digitalRead(data_validator)!=LOW);
  
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(15);
  digitalWrite(cmd_pin, LOW);
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(1);
  digitalWrite(cmd_pin,LOW);
  delay(15);
  digitalWrite(cmd_pin,HIGH);
  
  serial_busy = false;
  delay(10);
}

void left(){
  
  serial_busy = true;
  Serial.println("Left");
  digitalWrite(cmd_pin, LOW);
  while (digitalRead(data_validator)!=LOW);
  
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(20);
  digitalWrite(cmd_pin, LOW);
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(1);
  digitalWrite(cmd_pin,LOW);
  delay(10);
  digitalWrite(cmd_pin,HIGH);
  
  serial_busy = false;
  delay(10);
}
void auton(){
  serial_busy = true;
  Serial.println("going autonomusly");
  digitalWrite(cmd_pin, LOW);
  while (digitalRead(data_validator)!=LOW);
  
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(25);
  digitalWrite(cmd_pin, LOW);
  delay(1);
  digitalWrite(cmd_pin,HIGH);
  delay(1);
  digitalWrite(cmd_pin,LOW);
  delay(5);
  digitalWrite(cmd_pin,HIGH);
  
  serial_busy = false;
  delay(10);
}

void setup(){
  Serial.begin(115200);
  pinMode(cmd_pin, OUTPUT);
  pinMode(data_validator, INPUT);
  digitalWrite(cmd_pin, HIGH);
}
void loop(){
  //call the functions left or right ..
}


I am calling the functions left,right,.... based on my websocket output(Not Included here)

where I was wrong, or is there any other methods available?

constrains: Pins available in arduino : 2,13, A3,A4 ,A5 (all other pins are used by other components)

I am expecting the best way to transfer data between arduino and esp32 loss-less transmission using the availble pins only.

1

There are 1 answers

0
Rohit Sardessai On BEST ANSWER

There is a software serial library available that you can use to enable serial communication over any GPIO pins. Arduino Software Serial