Hi .. I hope that you are doing well ..
I want to display battery voltages on OLED using a string command but don't know where I'm lacking ...
I'm using two Arduino one is the transmits and another is receiving a signal and displays it on OLED
Here is the circuit diagram for better understanding
here is the code of the receiver to show display on OLED
#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
void setup() {
Serial.begin(4800);
inputString.reserve(200);
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.display();
}
int i = 0;
void loop() {
if (i>=0) i++;
if (stringComplete) {
String strID = (inputString.substring(0, 4));
if (strID == "$ABC") {
Serial.print(inputString);
for (byte i = 0; i <= 35; i++) {
char c = inputString[i];
if (c == 'V') {
int index = inputString.indexOf("V");
String Volts = inputString.substring(index - 6, index + 1);
Serial.println(Volts);
display.clearDisplay();
display.drawRect(0, 0, 128, 64, SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(25, 5);
display.println ("ABC System");
display.setCursor(25, 15);
display.println ("Started");
display.setTextSize(2);
display.setCursor(24, 35);
display.println(Volts);
display.display();
}
}
for ( ; ; ) {}
}
inputString = "";
stringComplete = false;
}
if (i>123) i = 0;
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
Here is the code of the transmitter that transmits
I want to implement the battery voltages with string to catch it but dont know where to start it at
//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 = 1590; // will use R1 =47k ohms and 27k ohms resistors
float R2 = 1000; // will use R2 =10k ohms resistor
int readValue = 0;
long previousMillis_battery = 0;
byte Bat_start_timer = 0;
int battery_wait;
unsigned long currentMillis = 0; // VALID FOR ALL FUNCTIONS(COUNTER)
//BATTERY FUNCTION
void battery()
{
readValue = analogRead(Battery);
Voltage1 = readValue * (5.0 / 1023);
Voltage = Voltage1 / (R2/(R1+R2));
if(Bat_start_timer == 0) battery_wait = 2000;
if(currentMillis - previousMillis_battery > battery_wait) {
// Serial.println("Battery Voltages,"); Serial.print(Voltage); Serial.print(",V,");
previousMillis_battery = currentMillis;
battery_wait = 2000; Bat_start_timer = 0; }
} // battery FUNCTION END
void setup()
{
Serial.begin(4800);
pinMode(Battery, INPUT);
Serial.println("Battery Voltages");
}
void loop()
{
int i = 0;
if (i >= 0) i++;{
Serial.println ("$ABC, 12.10V,\n");
// Serial.print(Voltage);
}
i = 0;
delay (100);
// currentMillis = millis();
// battery();
// delay(500);
}
Thank you so much.

