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