Serial Port Collides

Hello everyone ...
I hope that you are good.

I am using a serial0 i.e., RX0 and TX0 and my serial port collides sometimes it shows voltages and sometimes it shows the joystick percentage values. I want both of these to work independently at the same time; I tried changes in my cod but still I am unsuccessful.

I am using an arduino mega on both sides transmitter and receiver.

My results are perfect, I want that both of these work independently at the same time.

Here is the code at the transmitter side that I am using joystick to control the motors.

int VRx = A1; // ANALOG READING FOR LEFT AND RIGHT MOVEMENT FOR X-AXIS
int VRy = A0; // ANALOG READING FOR UPWARD AND DOWNWARD MOVEMENT FOR Y-AXIS
int Sw = 2; //
float Aval; //VALUE OF VOLTAGE AT A1PORT
float Bval; //VALUE OF VOLTAGE AT A0 PORT
unsigned int MAXIMUM = 1023; //MAXIMUM RESOLUTION
unsigned int MID = 512;
unsigned int MAX = 1023;


char buff[60];
int flag = 0;
float voltage = 0;


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "math.h"
#include <SPI.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete


unsigned int previousMillis = 0;        // will store last time LED was updated


const long interval = 10000;           // interval at which to blink (milliseconds) 5000
unsigned int currentMillis = 0;



void setup() {

  Serial.begin(4800);
  pinMode(VRx, INPUT);
  pinMode(VRy, INPUT);
  pinMode(Sw, INPUT);
  inputString.reserve(200);
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.display();
  delay(2000); //add this again
  // Clear the buffer.
  display.clearDisplay();

}

void loop() {



  serialEvent();

  currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    voltage = 0;

    delay(1); //1 ms delay for stability


    display.clearDisplay();
    display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
    display.setTextSize(1.5);             // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE);        // Draw white text
    display.setCursor(10, 5);
    display.println ("ABC System");
    display.setCursor(10, 15);
    display.println ("Started");
    display.setCursor(10, 25);
    display.println("V:");

    display.setCursor(10, 35);
    display.println("ABC");

    display.setCursor(10, 45);
    display.setTextSize(1.5);
    display.println(voltage);
    delay(1500);

    display.display();
    inputString = "";

  }


  unsigned long tempA = 0; // MOTOR A motor speed value start from zero
  unsigned long tempA1 = 0; //FORMULA
  unsigned long tempA2 = 0; //FORMULA
  Aval = analogRead(VRx);
  tempA = Aval;




  unsigned long tempB = 0; // MOTOR B motor speed value start from zero
  unsigned long tempB1 = 0; // Formula
  unsigned long tempB2 = 0; // Formula
  Bval = analogRead(VRy);
  tempB = Bval;



  //FORWARD DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB > 520) && (tempA > 480 && tempA < 520)) {
    tempB1 = (((tempB - MID) * 100) / MID);
    char st[20]; // Send command to the buffer Vx as a statement for motor A
    char st1[20]; // Send command  Vy as a statement for motor B
    sprintf(st, "<A+%03lu>", tempB1);
    sprintf(st1, "<B+%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);
  }


  //BACKWARD DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB < 480) && (tempA > 480 && tempA < 520)) {
    tempB1 = (((MID - tempB) * 100) / MID);
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempB1);
    sprintf(st1, "<B-%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }

  //NEUTRAL DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB > 480 && tempB < 520) && (tempA > 480 && tempA < 520)) {
    char st[20];
    Serial.println ("<A+000>");
    Serial.println ("<B+000>");
  }

  ////PURE LEFT MOVEMENT MOTOR // DONE SUCCESSFULLY
  ////===========================================================================//
  //if ((tempA<450) && (tempB>450 && tempB<550)){
  //  tempA1=(((MID-tempA)*100)/MID);
  // char st[20];
  // char st1[20];
  // sprintf(st1,"<B+%03lu>",tempA1);
  // Serial.println (st1);
  // }
  //
  // //PURE RIGHT MOVEMENT MOTOR // DONE SUCCESSFULLY
  ////===========================================================================//
  // if ((tempA>550) && (tempB>450 && tempB<550))
  //  {
  //    tempA1 = (((tempA-MID)*100)/MID);
  //
  //  char st[20];
  // char st1[20];
  // sprintf(st,"<A+%03lu>",tempA1);
  // Serial.println (st);
  // }


  // FORWARD RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH AND RIGHT MOTOR SPEED IS LOW //DONE SUCCESSFULLY
  //=============================================================================================================//
  if ((tempA > 520) && tempB > 520)
  {
    tempA1 = ((tempB * 100) / MAX);
    tempB1 = (((tempA - MID) * 100) / (MAX - MID));
    tempB2 = (tempA1 -  ((tempB1 * tempA1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A+%03lu>", tempA1);
    sprintf(st1, "<B+%03lu>", tempB2);
    Serial.println (st);
    Serial.println (st1);
  }

  // FORWARD LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW AND RIGHT MOTOR SPEED IS HIGH //DONE SUCCESSFULLY
  //==========================================================================================================//
  if ((tempA < 480) && (tempB > 520))
  {
    tempB1 = ((tempB * 100) / MAX );
    tempA1 = (100 - ((tempA * 100) / (MID)));
    tempA2 = (tempB1 -  ((tempA1 * tempB1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A+%03lu>", tempA2);
    sprintf(st1, "<B+%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }



  // REVERSE RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE) AND RIGHT MOTOR SPEED IS LOW (MOVING CLOCKWISE)
  //====================================================================================================================================//

  if (tempA > 520 && tempB < 480)
  {
    tempA1 = (((MID - tempB) * 100) / (MID) );
    tempB1 = (((tempA - MID) * 100) / (MAX - MID));
    tempB2 = (tempA1 - ((tempB1 * tempA1) / 100 ));
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempA1);
    sprintf(st1, "<B-%03lu>", tempB2);
    Serial.println (st);
    Serial.println (st1);

  }


  // REVERSE LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW (MOVING CLOCKWISE) AND RIGHT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE)
  // ===================================================================================================================================//

  if (tempA < 480 && tempB < 480)
  {

    tempB1 = (((MID - tempB) * 100) / (MID));
    tempA1 = (100 - ((tempA * tempB1) / (MID)));
    tempA2 = (tempB1 - ((tempA1 * tempB1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempA2);
    sprintf(st1, "<B-%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }




  delay(100);
}








void serialEvent() {
  //  while (Serial.available()) {
  //    char inChar = (char)Serial.read();
  //    inputString += inChar;
  //    if (inChar == '\n') {
  //      stringComplete = true;
  //    }

  if (Serial.available() > 0) {
    inputString = Serial.readString();
    previousMillis = currentMillis;
    flag = 1;
  }


  String newstr = inputString.substring(inputString.indexOf(',') + 1, inputString.indexOf('V'));
  Serial.println(inputString);
  Serial.println(newstr);

  //Vstr.toCharArray(buff,12);
  voltage = newstr.toFloat();
  Serial.println(voltage);
  //  counter++;

  delay(1); //1 ms delay for stability

  display.clearDisplay();
  display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
  display.setTextSize(1.5);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10, 5);
  display.println ("ABC System");
  display.setCursor(10, 15);
  display.println ("Started");
  display.setCursor(10, 25);
  display.println("V:");
  //
  display.setCursor(10, 35);
  display.println("ABC");

  display.setCursor(10, 45);
  display.setTextSize(1.5);
  display.println(voltage);
  delay(1500);


  display.display();
  inputString = "";



}

Here is the code at the receiver side Motors with battery

#include <Servo.h>
const int m_1 = 5;
const int m_2 = 6;
const int LED = 13;
String strID = "";

int BaudRate = 4800;
Servo ESC_1; // create servo object to control the ESC 1
Servo ESC_2; // create servo object to control the ESC 2
#define FWD 0
#define BWD 1




//BATTERY READINGS VARIABLES
const int Battery = A0;
float Voltage = 0.0; //input battery voltage
float Voltage1 = 0.0; //output voltage for analog pin A0
float R1 = 1572; // R1 =47k ohms and 27k ohms resistors 7400, 1581, 1597
float R2 = 1000; // R2 =10k ohms resistor 10000, 1000, 1000
int readValue = 0;
long previousMillis_battery = 0;
byte Bat_start_timer = 0;
int battery_wait = 0;
unsigned long currentMillis = 0; // VALID FOR ALL FUNCTIONS(COUNTER)


//BATTERY FUNCTION
void battery()
{
  readValue = analogRead(Battery);
  Voltage1 = readValue * (5.0 / 1023.0);
  Voltage = Voltage1 / (R2 / (R1 + R2));
  if (Bat_start_timer == 0) battery_wait = 1500;
  if (currentMillis - previousMillis_battery > battery_wait) {
    Serial.print("ABC,"); Serial.print(Voltage); Serial.print("V");Serial.print('\n');
    previousMillis_battery = currentMillis;
    battery_wait = 1500; Bat_start_timer = 1;
  }
  currentMillis = millis(); // to monitor/print voltages on a serial monitor
  
} // battery FUNCTION END


unsigned int FORWARD = 2000;
unsigned int NEUTRAL = 1500;
unsigned int REVERSE = 1000;

unsigned int APwm = NEUTRAL;
unsigned int BPwm = NEUTRAL;

boolean ADirection = FWD;
boolean BDirection = FWD;
unsigned int AVelocity = 0; // CURRENT VELOCITY OF M1 in 0-100%
unsigned int BVelocity = 0; // CURRENT VELOCITY OF M2 in 0-100%



String inputString = "";
int TaskNumber = 0;

uint32_t msgTimeout = 0ul;
uint32_t timeNow;

unsigned int GetValue(void)// fetch three digit number value from received string and convert to INTEGER
{
  unsigned int Val = 0;
  Val = (inputString[3] - 48) * 100;
  Val = Val + (inputString[4] - 48) * 10;
  Val = Val + (inputString[5] - 48);
  return (Val);
}




void setup()
{
  Serial.begin(BaudRate); //RX0 for radiolink controlling two motors
  ESC_1.attach(m_1);// (pin, min pulse width, max pulse width in microseconds)
  ESC_2.attach(m_2);// (pin, min pulse width, max pulse width in microseconds)
  pinMode(LED, OUTPUT);
  pinMode(Battery, INPUT);
  Serial.println("ABC System Started");
  ESC_1.writeMicroseconds(APwm);
  ESC_2.writeMicroseconds(BPwm);

}



boolean AFwdFlag = 0; //makes sure that the serial only prints once the state
boolean BFwdFlag = 0; //makes sure that the serial only prints once the state



void loop()
{
  
 for (int i = 0; i < 10; i++) {

    battery();
    
    }
  






  timeNow = millis();



  serialEvent();


  if ( TaskNumber == 0 )
{
  //if we've gone 5-seconds...
  if ( (timeNow - msgTimeout) >= 5000ul )
    {
      //then all stop
      ESC_1.writeMicroseconds(NEUTRAL);
      ESC_2.writeMicroseconds(NEUTRAL);
      digitalWrite(LED, HIGH );  //<---- add this line

    }//if

  }//if

  if ( TaskNumber )
{
  //each pass where we see a non-zero task #, reset the msgTimeout value
  msgTimeout = millis();
    digitalWrite(LED, LOW );

    if (TaskNumber == 1) Serial.println("OK");
    if (TaskNumber == 2 || TaskNumber == 4) {
      ESC_1.writeMicroseconds(NEUTRAL);
    }
    if (TaskNumber == 3 || TaskNumber == 4) {
      ESC_2.writeMicroseconds(NEUTRAL);
    }


    if (TaskNumber == 5 || TaskNumber == 7) {
      if (ADirection == FWD) {
        // MOTOR A INCREMENT FOR FORWARD DIRECTION
        if (AVelocity > 0 && AVelocity <= 10) {
          APwm = 1535 ;
        }
        else if (AVelocity > 10 && AVelocity <= 20) {
          APwm = 1540;
        }
        else if (AVelocity > 20 && AVelocity <= 30) {
          APwm = 1545;
        }
        else if (AVelocity > 30 && AVelocity <= 40) {
          APwm = 1550;
        }
        else if (AVelocity > 40 && AVelocity <= 50) {
          APwm = 1555;
        }
        else if (AVelocity > 50 && AVelocity <= 60) {
          APwm = 1560;
        }
        else if (AVelocity > 60 && AVelocity <= 70) {
          APwm = 1565;
        }
        else if (AVelocity > 70 && AVelocity <= 80) {
          APwm = 1570;
        }
        else if (AVelocity > 80 && AVelocity <= 90) {
          APwm = 1575;
        }
        else if (AVelocity > 90 && AVelocity <= 100) {
          APwm++ ;
        }
        else {
          APwm = 1500;
        }
        ESC_1.writeMicroseconds(APwm);
        AFwdFlag = 1;
      }

      else {
        if (AFwdFlag == 1)
        {
          ESC_1.writeMicroseconds(NEUTRAL); delay(100);
          ESC_1.writeMicroseconds(REVERSE); delay(100);
          ESC_1.writeMicroseconds(NEUTRAL); delay(100);
          AFwdFlag = 0;
        }
        // MOTOR A DECREMENT FOR BACKWARD DIRECTION
        if (AVelocity > 0 && AVelocity <= 10) {
          APwm = 1430 ;
        }
        else if (AVelocity > 10 && AVelocity <= 20) {
          APwm = 1420;
        }
        else if (AVelocity > 20 && AVelocity <= 30) {
          APwm = 1410;
        }
        else if (AVelocity > 30 && AVelocity <= 40) {
          APwm = 1400;
        }
        else if (AVelocity > 40 && AVelocity <= 50) {
          APwm = 1390;
        }
        else if (AVelocity > 50 && AVelocity <= 60) {
          APwm = 1380;
        }
        else if (AVelocity > 60 && AVelocity <= 70) {
          APwm = 1370;
        }
        else if (AVelocity > 70 && AVelocity <= 80) {
          APwm = 1360;
        }
        else if (AVelocity > 80 && AVelocity <= 90) {
          APwm = 1350;
        }
        else if (AVelocity > 90 && AVelocity <= 100) {
          APwm-- ;
        }
        else {
          BPwm = 1500;
        }
        ESC_1.writeMicroseconds(APwm);


      }//ELSE



    }










    if (TaskNumber == 6 || TaskNumber == 7) {
      if (BDirection == FWD) {
        // MOTOR B INCREMENT FOR FORWARD DIRECTION
        if (BVelocity > 0 && BVelocity <= 10) {
          BPwm = 1535 ;
        }
        else if (BVelocity > 10 && BVelocity <= 20) {
          BPwm = 1540;
        }
        else if (BVelocity > 20 && BVelocity <= 30) {
          BPwm = 1545;
        }
        else if (BVelocity > 30 && BVelocity <= 40) {
          BPwm = 1550;
        }
        else if (BVelocity > 40 && BVelocity <= 50) {
          BPwm = 1555;
        }
        else if (BVelocity > 50 && BVelocity <= 60) {
          BPwm = 1560;
        }
        else if (BVelocity > 60 && BVelocity <= 70) {
          BPwm = 1565;
        }
        else if (BVelocity > 70 && BVelocity <= 80) {
          BPwm = 1570;
        }
        else if (BVelocity > 80 && BVelocity <= 90) {
          BPwm = 1575;
        }
        else if (BVelocity > 90 && BVelocity <= 100) {
          BPwm++ ;
        }
        else {
          BPwm = 1500;
        }

        ESC_2.writeMicroseconds(BPwm);
        BFwdFlag = 1;

      }
      else {
        if (AFwdFlag == 1)
        {
          ESC_2.writeMicroseconds(NEUTRAL); delay(100);
          ESC_2.writeMicroseconds(REVERSE); delay(100);
          ESC_2.writeMicroseconds(NEUTRAL); delay(100);
          BFwdFlag = 0;
        }
        // MOTOR B DECREMENT FOR BACKWARD DIRECTION
        if (BVelocity > 0 && BVelocity <= 10) {
          BPwm = 1430 ;
        }
        else if (BVelocity > 10 && BVelocity <= 20) {
          BPwm = 1420;
        }
        else if (BVelocity > 20 && BVelocity <= 30) {
          BPwm = 1410;
        }
        else if (BVelocity > 30 && BVelocity <= 40) {
          BPwm = 1400;
        }
        else if (BVelocity > 40 && BVelocity <= 50) {
          BPwm = 1390;
        }
        else if (BVelocity > 50 && BVelocity <= 60) {
          BPwm = 1380;
        }
        else if (BVelocity > 60 && BVelocity <= 70) {
          BPwm = 1370;
        }
        else if (BVelocity > 70 && BVelocity <= 80) {
          BPwm = 1360;
        }
        else if (BVelocity > 80 && BVelocity <= 90) {
          BPwm = 1350;
        }
        else if (BVelocity > 90 && BVelocity <= 100) {
          BPwm-- ;
        }
        else {
          BPwm = 1500;
        }
        ESC_2.writeMicroseconds(BPwm);


      }//ELSE
    }


    TaskNumber = 0;


  }



}



void serialEvent() { //radiolink for controlling two motors by command Serial port 0

  while (Serial.available()) {

    char inChar = (char)Serial.read();
    inputString += inChar;

    if (inChar == '\r' || inChar == 10)
    {

      if (inputString[0] == '<')
      {

        if (inputString[1] == 'A' && inputString[2] == 'T' && inputString[3] == '>' ) TaskNumber = 1; // AT

        if (inputString[2] == 'N' && inputString[3] == '>' )
        {
          if ( inputString[1] == 'A') TaskNumber = 2; // Set Motor A to Neutral
          if ( inputString[1] == 'B') TaskNumber = 3; // Set Motor B to Neutral
          if ( inputString[1] == 'C') TaskNumber = 4; // Set Both Motors to Neutral
        }

        if (inputString[6] == '>' )
        {

          if ( inputString[1] == 'A') {
            if (inputString[2] == '+') ADirection = FWD; else ADirection = BWD;
            AVelocity = GetValue();
            TaskNumber = 5;
          }

          if ( inputString[1] == 'B') {
            if (inputString[2] == '+') BDirection = FWD; else BDirection = BWD;
            BVelocity = GetValue();
            TaskNumber = 6;
          }

          if ( inputString[1] == 'C') {
            if (inputString[2] == '+') {
              ADirection = FWD;
              BDirection = FWD;
            }
            else {
              ADirection = BWD;
              BDirection = BWD;
            }
            AVelocity = GetValue();
            BVelocity = GetValue();
            TaskNumber = 7;
          }

        }

      }
      inputString = "";
    }
  }
}

Hi Mubashira,

I can't look inside your head.
It is not clear to me what you really want the code to do.

This is a contradicting statement. Is it working "perfect" or not?
As you have two Arduino Mega why do you use the serial0-interface for the communication between the two arduino megas? Why don't you use serial1 or serial2?
serial0 is for uploading your code and for send/receive data from the serial monitor

For a second serial communication-channel use serial1 or serial2
And there will be no more conflicts.
If you mean something different you should post more information.

How does your send/receive between the two Arduino megas work in principle ?

Post an example of how you want the communication towards serial monitor to work.
Write an example how a typical receiving of

does look like?

If you find it difficult to write this in english use google-translate
best regards Stefan

I am controlling motors via wireless radiolink through a joystick without any problem. After that I have connected an OLED and tried to receive the voltages from the battery via wireless communication, I do not received it properly and it collides with the motor when I added a delay of 1500 in the OLED coding before display.display()

then this delay of 1500 is automatically added in the motors which it takes time to run those motors.

I'm sorry,

what is your native language?

Your description is not detailed enough to understand what your problem is and what you want to do.

If it is difficult for you to write in english write in your native language and let do google translate the translation.

best regards Stefan

Please see this image below ...


s

This is the result of the image above I got continuous motors values and my controlling is smooth when I run those motors via joystick

My OLED does not give me a voltages it shows 0 volts on the screen
When I add a delay of 1500 in the end of an OLED code to get a voltage display

display.setCursor(10, 45);
    display.setTextSize(1.5);
    display.println(voltage);
    delay(1500); // this one delay

    display.display();
    inputString = "";

then it shows a very good display of the voltages that I wirelessly got via from the battery but this delay is interrupted to start my motors when I try to control it via joystick then it is not running smoothly and it delayed in starting and I dont know how can I resolve this issue ...

Please see the image of this interrupted delay below

I want to display the voltages on my OLED without any interruption with my motors but I don't know how to do it .... Please help me in this regard ...

Thank you

Now please see the details that i have posted ... I hope you understand well

first of all all variables that are used somehow with function millis() should ALWAYS be of type
unsigned long. The function millis() returns values of type unsigned long.

You have two places that print to the OLED-display one place is inside your function

void serialEvent() {

one place is below the call of function serialEvent() inside loop

void loop() {
  serialEvent();
  currentMillis = millis();
  
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    voltage = 0; // AFTER receiving the last serial string you assign value 0
                 // that is what your code is then printing 
                 
    delay(1); //1 ms delay for stability
//....
    display.setCursor(10, 45);
    display.setTextSize(1.5);
    display.println(voltage);  // <<<=== has value zero and is printing it
    delay(1500);               

    display.display();
    inputString = "";

below receiving the real value you are assigning voltage the value zero
and that is what gets printed. The delay(1500) does what its name says
delaying. This delay make the real value visible for 1,5 seconds from printing the value inside serialEvent

You should modify your code into functions where each function does

one single thing

and the name of the function should be self-descriptive about this thing.

So I recommend to write one function "printToOLED(voltage)" that does nothing else than printing to your OLED-display and needs the voltage as a parameter.
A parameter is naming a variable inside the parenthesis

The call of this function should be placed right below the receiving of the voltage-data

What is good is that you are updating the display only once every 10 seconds by using a non-blockling timer that you are using here:

  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

best regards Stefan

As a small side comment, you don't need to call serialEvent() explicitly. The function will be called automatically at the end of loop() if serial data is available.

As a result, that means that you don't need to check whether serial data is available within the function

Will you please help me to write the proper code ?

I also want that if the signal will be lost so that it would show me 0 volts on my OLED ...

Sure I help. But I do not write the first attempt.
You should write a first attempt combined with the questions you have about it.
What ever the questions might be - ask them.

best regards Stefan

Okay let me try it first :slight_smile:

Yes, I tried and I got the Voltages successfully ....

Here is the code ....

int VRx = A1; // ANALOG READING FOR LEFT AND RIGHT MOVEMENT FOR X-AXIS
int VRy = A0; // ANALOG READING FOR UPWARD AND DOWNWARD MOVEMENT FOR Y-AXIS
int Sw = 2; //
float Aval; //VALUE OF VOLTAGE AT A1PORT
float Bval; //VALUE OF VOLTAGE AT A0 PORT
unsigned int MAXIMUM = 1023; //MAXIMUM RESOLUTION
unsigned int MID = 512;
unsigned int MAX = 1023;


char buff[60];
int flag = 0;
float voltage = 0;


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "math.h"
#include <SPI.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete


unsigned int previousMillis = 0;        // will store last time LED was updated


const long interval = 10000;           // interval at which to blink (milliseconds) 5000
unsigned long currentMillis = 0;



void setup() {

  Serial.begin(4800);
  pinMode(VRx, INPUT);
  pinMode(VRy, INPUT);
  pinMode(Sw, INPUT);
  inputString.reserve(200);
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.display();
  delay(2000); //add this again
  // Clear the buffer.
  display.clearDisplay();

}

void loop() {


  currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
 //   voltage = 0;

    delay(1); //1 ms delay for stability


    display.clearDisplay();
    display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
    display.setTextSize(1.5);             // Normal 1:1 pixel scale
    display.setTextColor(SSD1306_WHITE);        // Draw white text
    display.setCursor(10, 5);
    display.println ("ABC System");
    display.setCursor(10, 15);
    display.println ("Started");
    display.setCursor(10, 25);
    display.println("V:");

    display.setCursor(10, 35);
    display.println("ABC");

    display.setCursor(10, 45);
    display.setTextSize(1.5);
    display.println(voltage);
    delay(1500); //this delay creates the interruption in the motors 

    display.display();
    inputString = "";

  }
  else
     voltage = 0;



  unsigned long tempA = 0; // MOTOR A motor speed value start from zero
  unsigned long tempA1 = 0; //FORMULA
  unsigned long tempA2 = 0; //FORMULA
  Aval = analogRead(VRx);
  tempA = Aval;




  unsigned long tempB = 0; // MOTOR B motor speed value start from zero
  unsigned long tempB1 = 0; // Formula
  unsigned long tempB2 = 0; // Formula
  Bval = analogRead(VRy);
  tempB = Bval;



  //FORWARD DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB > 520) && (tempA > 480 && tempA < 520)) {
    tempB1 = (((tempB - MID) * 100) / MID);
    char st[20]; // Send command to the buffer Vx as a statement for motor A
    char st1[20]; // Send command  Vy as a statement for motor B
    sprintf(st, "<A+%03lu>", tempB1);
    sprintf(st1, "<B+%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);
  }


  //BACKWARD DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB < 480) && (tempA > 480 && tempA < 520)) {
    tempB1 = (((MID - tempB) * 100) / MID);
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempB1);
    sprintf(st1, "<B-%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }

  //NEUTRAL DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB > 480 && tempB < 520) && (tempA > 480 && tempA < 520)) {
    char st[20];
    Serial.println ("<A+000>");
    Serial.println ("<B+000>");
  }

  ////PURE LEFT MOVEMENT MOTOR // DONE SUCCESSFULLY
  ////===========================================================================//
  //if ((tempA<450) && (tempB>450 && tempB<550)){
  //  tempA1=(((MID-tempA)*100)/MID);
  // char st[20];
  // char st1[20];
  // sprintf(st1,"<B+%03lu>",tempA1);
  // Serial.println (st1);
  // }
  //
  // //PURE RIGHT MOVEMENT MOTOR // DONE SUCCESSFULLY
  ////===========================================================================//
  // if ((tempA>550) && (tempB>450 && tempB<550))
  //  {
  //    tempA1 = (((tempA-MID)*100)/MID);
  //
  //  char st[20];
  // char st1[20];
  // sprintf(st,"<A+%03lu>",tempA1);
  // Serial.println (st);
  // }


  // FORWARD RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH AND RIGHT MOTOR SPEED IS LOW //DONE SUCCESSFULLY
  //=============================================================================================================//
  if ((tempA > 520) && tempB > 520)
  {
    tempA1 = ((tempB * 100) / MAX);
    tempB1 = (((tempA - MID) * 100) / (MAX - MID));
    tempB2 = (tempA1 -  ((tempB1 * tempA1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A+%03lu>", tempA1);
    sprintf(st1, "<B+%03lu>", tempB2);
    Serial.println (st);
    Serial.println (st1);
  }

  // FORWARD LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW AND RIGHT MOTOR SPEED IS HIGH //DONE SUCCESSFULLY
  //==========================================================================================================//
  if ((tempA < 480) && (tempB > 520))
  {
    tempB1 = ((tempB * 100) / MAX );
    tempA1 = (100 - ((tempA * 100) / (MID)));
    tempA2 = (tempB1 -  ((tempA1 * tempB1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A+%03lu>", tempA2);
    sprintf(st1, "<B+%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }



  // REVERSE RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE) AND RIGHT MOTOR SPEED IS LOW (MOVING CLOCKWISE)
  //====================================================================================================================================//

  if (tempA > 520 && tempB < 480)
  {
    tempA1 = (((MID - tempB) * 100) / (MID) );
    tempB1 = (((tempA - MID) * 100) / (MAX - MID));
    tempB2 = (tempA1 - ((tempB1 * tempA1) / 100 ));
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempA1);
    sprintf(st1, "<B-%03lu>", tempB2);
    Serial.println (st);
    Serial.println (st1);

  }


  // REVERSE LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW (MOVING CLOCKWISE) AND RIGHT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE)
  // ===================================================================================================================================//

  if (tempA < 480 && tempB < 480)
  {

    tempB1 = (((MID - tempB) * 100) / (MID));
    tempA1 = (100 - ((tempA * tempB1) / (MID)));
    tempA2 = (tempB1 - ((tempA1 * tempB1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempA2);
    sprintf(st1, "<B-%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }




  delay(100);
}








void serialEvent() {
  //  while (Serial.available()) {
  //    char inChar = (char)Serial.read();
  //    inputString += inChar;
  //    if (inChar == '\n') {
  //      stringComplete = true;
  //    }

  if (Serial.available() > 0) {
    inputString = Serial.readString();
    previousMillis = currentMillis;
    flag = 1;
  }


  String newstr = inputString.substring(inputString.indexOf(',') + 1, inputString.indexOf('V'));
  Serial.println(inputString);
  Serial.println(newstr);

  //Vstr.toCharArray(buff,12);
  voltage = newstr.toFloat();
  Serial.println(voltage);
  //  counter++;

  delay(1); //1 ms delay for stability

  display.clearDisplay();
  display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
  display.setTextSize(1.5);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10, 5);
  display.println ("ABC System");
  display.setCursor(10, 15);
  display.println ("Started");
  display.setCursor(10, 25);
  display.println("V:");
  //
  display.setCursor(10, 35);
  display.println("ABC");

  display.setCursor(10, 45);
  display.setTextSize(1.5);
  display.println(voltage);
  delay(1500); // this is the delay which causes the interruption in the motors


  display.display();
  inputString = "";



}

One problem I m facing right now is that my motors interrupted with the OLED Voltages and it does not run smoothly due to this delay (1500) that I added in the OLED code after println(voltage) , If I changed this delay to delay(10) then the motors are running smoothly but again the OLED does not show the voltages. Sometimes when it shows the voltages the there is a interruption in the motors which I don't want. I want to remove this interruption; I am trying to remove this interruption but I don't know how can I tackle this issue

Your new code-version still prints data in

two

places

inside loop
and inside function serialEvent()

it is only nescessary to print to the display at

one

place

post your decision

where

you want to print to the OLED-display

You haven't really posted a

specific question.

I will answer if you ask specific questions
not asking specific questions is too little initiative

Your specific questions can be about any detail how this or that is coded or can be a guessing how it might be coded

best regards Stefan

Yes, I want to print the voltages on an OLED ...

This is my last try

either you can post your decision

at which place

the doubled oled-printing shall be deleted
or I will just repeat to post a link to "tips how to improve your posting-style"

I want to place it in the serialEvent; not in the loop ....

Recently, I did some changes in my code but still the issue persists.

int VRx = A1; // ANALOG READING FOR LEFT AND RIGHT MOVEMENT FOR X-AXIS
int VRy = A0; // ANALOG READING FOR UPWARD AND DOWNWARD MOVEMENT FOR Y-AXIS
int Sw = 2; //
float Aval; //VALUE OF VOLTAGE AT A1PORT
float Bval; //VALUE OF VOLTAGE AT A0 PORT
unsigned int MAXIMUM = 1023; //MAXIMUM RESOLUTION
unsigned int MID = 512;
unsigned int MAX = 1023;


char buff[60];
int flag = 0;
float voltage = 0;


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "math.h"
#include <SPI.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

String inputString = "";         // a String to hold incoming data
bool stringComplete = false;  // whether the string is complete


unsigned int previousMillis = 0;        // will store last time LED was updated


const long interval = 10000;           // interval at which to blink (milliseconds) 5000
unsigned long currentMillis = 0;



void setup() {

  Serial.begin(4800);
  pinMode(VRx, INPUT);
  pinMode(VRy, INPUT);
  pinMode(Sw, INPUT);
  inputString.reserve(200);
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);

  display.setTextSize(1);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.display();
  delay(2000); //add this again
  // Clear the buffer.
  display.clearDisplay();

}

void loop() {



  unsigned long tempA = 0; // MOTOR A motor speed value start from zero
  unsigned long tempA1 = 0; //FORMULA
  unsigned long tempA2 = 0; //FORMULA
  Aval = analogRead(VRx);
  tempA = Aval;




  unsigned long tempB = 0; // MOTOR B motor speed value start from zero
  unsigned long tempB1 = 0; // Formula
  unsigned long tempB2 = 0; // Formula
  Bval = analogRead(VRy);
  tempB = Bval;



  //FORWARD DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB > 520) && (tempA > 480 && tempA < 520)) {
    tempB1 = (((tempB - MID) * 100) / MID);
    char st[20]; // Send command to the buffer Vx as a statement for motor A
    char st1[20]; // Send command  Vy as a statement for motor B
    sprintf(st, "<A+%03lu>", tempB1);
    sprintf(st1, "<B+%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);
  }


  //BACKWARD DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB < 480) && (tempA > 480 && tempA < 520)) {
    tempB1 = (((MID - tempB) * 100) / MID);
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempB1);
    sprintf(st1, "<B-%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }

  //NEUTRAL DONE SUCCESSFULLY
  //===========================================================================//
  if ((tempB > 480 && tempB < 520) && (tempA > 480 && tempA < 520)) {
    char st[20];
    Serial.println ("<A+000>");
    Serial.println ("<B+000>");
  }

  ////PURE LEFT MOVEMENT MOTOR // DONE SUCCESSFULLY
  ////===========================================================================//
  //if ((tempA<450) && (tempB>450 && tempB<550)){
  //  tempA1=(((MID-tempA)*100)/MID);
  // char st[20];
  // char st1[20];
  // sprintf(st1,"<B+%03lu>",tempA1);
  // Serial.println (st1);
  // }
  //
  // //PURE RIGHT MOVEMENT MOTOR // DONE SUCCESSFULLY
  ////===========================================================================//
  // if ((tempA>550) && (tempB>450 && tempB<550))
  //  {
  //    tempA1 = (((tempA-MID)*100)/MID);
  //
  //  char st[20];
  // char st1[20];
  // sprintf(st,"<A+%03lu>",tempA1);
  // Serial.println (st);
  // }


  // FORWARD RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH AND RIGHT MOTOR SPEED IS LOW //DONE SUCCESSFULLY
  //=============================================================================================================//
  if ((tempA > 520) && tempB > 520)
  {
    tempA1 = ((tempB * 100) / MAX);
    tempB1 = (((tempA - MID) * 100) / (MAX - MID));
    tempB2 = (tempA1 -  ((tempB1 * tempA1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A+%03lu>", tempA1);
    sprintf(st1, "<B+%03lu>", tempB2);
    Serial.println (st);
    Serial.println (st1);
  }

  // FORWARD LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW AND RIGHT MOTOR SPEED IS HIGH //DONE SUCCESSFULLY
  //==========================================================================================================//
  if ((tempA < 480) && (tempB > 520))
  {
    tempB1 = ((tempB * 100) / MAX );
    tempA1 = (100 - ((tempA * 100) / (MID)));
    tempA2 = (tempB1 -  ((tempA1 * tempB1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A+%03lu>", tempA2);
    sprintf(st1, "<B+%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }



  // REVERSE RIGHT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE) AND RIGHT MOTOR SPEED IS LOW (MOVING CLOCKWISE)
  //====================================================================================================================================//

  if (tempA > 520 && tempB < 480)
  {
    tempA1 = (((MID - tempB) * 100) / (MID) );
    tempB1 = (((tempA - MID) * 100) / (MAX - MID));
    tempB2 = (tempA1 - ((tempB1 * tempA1) / 100 ));
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempA1);
    sprintf(st1, "<B-%03lu>", tempB2);
    Serial.println (st);
    Serial.println (st1);

  }


  // REVERSE LEFT MOVEMENT BOTH MOTORS; LEFT MOTOR SPEED IS LOW (MOVING CLOCKWISE) AND RIGHT MOTOR SPEED IS HIGH (MOVING ANTICLOCKWISE)
  // ===================================================================================================================================//

  if (tempA < 480 && tempB < 480)
  {

    tempB1 = (((MID - tempB) * 100) / (MID));
    tempA1 = (100 - ((tempA * tempB1) / (MID)));
    tempA2 = (tempB1 - ((tempA1 * tempB1) / 100));
    char st[20];
    char st1[20];
    sprintf(st, "<A-%03lu>", tempA2);
    sprintf(st1, "<B-%03lu>", tempB1);
    Serial.println (st);
    Serial.println (st1);

  }




  delay(100);
}








void serialEvent() {
  //  while (Serial.available()) {
  //    char inChar = (char)Serial.read();
  //    inputString += inChar;
  //    if (inChar == '\n') {
  //      stringComplete = true;
  //    }

  if (Serial.available() > 0) {
    inputString = Serial.readString();
    previousMillis = currentMillis;
    flag = 1;
  }


  String newstr = inputString.substring(inputString.indexOf(',') + 1, inputString.indexOf('V'));
  Serial.println(inputString); //enable this
  Serial.println(newstr); // enable this

  //Vstr.toCharArray(buff,12);
  voltage = newstr.toFloat();
  Serial.println(voltage); //enable this option again if not working
   
  //  counter++;

  currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
 //   voltage = 0;



  delay(1); //1 ms delay for stability

  display.clearDisplay();
  display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
  display.setTextSize(1.5);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(10, 5);
  display.println ("ABC System");
  display.setCursor(10, 15);
  display.println ("Started");
  display.setCursor(10, 25);
  display.println("V:");
  //
  display.setCursor(10, 35);
  display.println("ABC");

  display.setCursor(10, 45);
  display.setTextSize(1.5);
  display.println(voltage);
  delay(1500);    //Still it causes an issue in the motors
//  delay(10);



  display.display();
  inputString = "";

 

}

else
     voltage = 0;
}