Hello,
I am working a current project in which my Arduino will receive strings (that are numbers) via Bluetooth from my phone. . I want my variable (sec) to be equal to the number in the string so I can display it on an OLED. That way the OLED won't have to clear every time a new string comes in, the variable will just update and be displayed. My problem is that I can't find a way to convert the string.I have tried to use the "toInt()" command but not sure if I'm using it right because even though there are no errors the OLED keeps displaying zero. Here is a piece of the code I'm using for this.
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SPI.h>
#include <SFE_MicroOLED.h>
#include <SoftwareSerial.h>
#define PIN_RESET 9
#define PIN_DC 8
#define PIN_CS 10
#define DC_JUMPER 0
SoftwareSerial BT(2,3);
String state;
int sec ;
MicroOLED oled(PIN_RESET, PIN_DC, PIN_CS);
void setup() {
BT.begin(9600);
Serial.begin(9600);
oled.begin();
oled.display();
oled.clear(ALL);
delay(1000);
}
//-----------------------------------------------------------------------//
void loop() {
while (BT.available()){
char c = BT.read();
state += c;
}
if (state.length() > 0) {
if (state.startsWith("."))
{
Serial.println(state);
oled.setFontType(1);
oled.setCursor(0,0);
oled.print(sec=state.toInt()));
oled.display();
state = "";
}
If you could someone could think of why this is happening and a way to fix it I would be very grateful.
Thank you