Can anyone help with my problem. I'm sending some data with arduino to android via bluetooth and sometimes it just loses some parts of text and goes haywire for some time until it stabilizes.
Here is my loop;
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
unsigned long uptime = millis();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if(Serial.available()){
while(Serial.available())
{
char inChar = (char)Serial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
while (Serial.available() > 0)
{ junk = Serial.read() ; } // clear the serial buffer
if(inputString == "a"){ //in case of 'a' turn the LED on
digitalWrite(3, HIGH);
}else if(inputString == "b"){ //incase of 'b' turn the LED off
digitalWrite(3, LOW);
}
inputString = "";
}
Serial.flush();
// Data update trough bluetooth timer
if (uptime - lastupdate >= delayonupdate) {
lastupdate = uptime;
Publishing the whole code, with fewer blank lines, would be a a better start.
Sorry about that, it has been soo long ago when i last time did coding and it has always been horrible, and now mostly ctrl+c / ctrl+v. But here is the whole thing, with deleted extra blankies.
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to #define DHTTYPE DHT22 // DHT 22 (AM2302) #define led 3
String inputString="";
char junk;
int delayonupdate = 1500;
long lastupdate = 0;
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
unsigned long uptime = millis();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if(Serial.available()){
while(Serial.available())
{
char inChar = (char)Serial.read(); //read the input
inputString += inChar; //make a string of the characters coming on serial
}
while (Serial.available() > 0)
{ junk = Serial.read() ; } // clear the serial buffer
if(inputString == "a"){ //in case of 'a' turn the LED on
digitalWrite(3, HIGH);
}else if(inputString == "b"){ //incase of 'b' turn the LED off
digitalWrite(3, LOW);
}
inputString = "";
}
Serial.flush();
// Data update trough bluetooth timer
if (uptime - lastupdate >= delayonupdate) {
lastupdate = uptime;