I am working on a wireless refrigerator control project and have ran into some issues. The project has two slave bluetooth modules (on a arduino each ) and two master bluetooth modules( on one arduino). I want to display the temperature and humidity from each slave one at a time but i'm getting garbage values. Can anyone help? Source code is provided below.
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal_SR.h>
#include <LiquidCrystal_SR2W.h>
#include <LiquidCrystal_SR3W.h>
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial mySerial(10, 11); // RX, TX
SoftwareSerial mySecond(8, 9); // RX, TX
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
char c;
String string;
char x;
String slave2;
void setup() {
// put your setup code here, to run once:
Serial.begin(38400);
pinMode(5,OUTPUT); digitalWrite(5,HIGH);
pinMode(3,OUTPUT); digitalWrite(3,HIGH);
//Serial.println("Enter AT commands:");
mySerial.begin(38400);
mySecond.begin(38400);
lcd.begin(16,2);
lcd.setBacklight(HIGH);
lcd.home (); // go home
lcd.clear();
lcd.setCursor(0,0);
//lcd.print();
lcd.setCursor(0,1);
//lcd.print("Humidity: **");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
}
//Timeslice global variables
unsigned long TimeSlice_TC1_Interval = 1000;
unsigned long TimeSlice_TC2_Interval = 3000;
unsigned long TimeSlice_TC3_Interval = 1000;
unsigned long TimeSlice_TC1_Start_Time;
unsigned long TimeSlice_TC2_Start_Time;
unsigned long TimeSlice_TC3_Start_Time;
void loop()
{
//----------------------------------------------------------TimeSlice code-----------------------------------------------------------
while(1){
//-------------------
//Timeclass 1 20 ms
//-------------------
TimeSlice_TC1_Start_Time = millis();
// ---------------------------------------------------------User application code
mySerial.flush();
while (mySerial.available()){
if (mySerial.available())
{
c = mySerial.read();
string = string + c;
Serial.write(c);
}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("R1Humidity:");
lcd.print(string[9]);
lcd.print(string[10]);
lcd.print(string[11]);
lcd.print(string[12]);
lcd.print(string[13]);
lcd.setCursor(0,1);
lcd.print("R1Temp:");
lcd.print(string[27]);
lcd.print(string[28]);
lcd.print(string[29]);
lcd.print(string[30]);
lcd.print(string[31]);
lcd.print(string[32]);
string= "";
mySerial.flush();
//mySerial.write(Serial.read());
Serial.println();
// ---------------------------------------------------------End of user application code
// Toggle TC1 LED
//toggle_LED(1);
// check timeout
if (((millis() - TimeSlice_TC1_Start_Time) > TimeSlice_TC1_Interval)) // 20 ms
{
while(1){
//over_run_Fault(1); // abort program and loop forever and flash Time class 1 LED
}
}
else
{
while (((millis() - TimeSlice_TC1_Start_Time) <= TimeSlice_TC1_Interval)){ // remove to save MIPS but it removes precise timeclass execution
}
}
//-------------------
//Timeclass 2 100 ms
//-------------------
TimeSlice_TC2_Start_Time = millis();
// ----------------------------------------------------------User application code
while (mySecond.available()){
if (mySecond.available())
{
x = mySecond.read();
slave2 = slave2 + x;
Serial.write(x);
}
else
{
slave2 = x+"H";
}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print("R2Humidity:");
lcd.print(slave2[9]);
lcd.print(slave2[10]);
lcd.print(slave2[11]);
lcd.print(slave2[12]);
lcd.print(slave2[13]);
lcd.setCursor(0,1);
lcd.print("R2Temp:");
lcd.print(slave2[27]);
lcd.print(slave2[28]);
lcd.print(slave2[29]);
lcd.print(slave2[30]);
lcd.print(slave2[31]);
lcd.print(slave2[32]);
slave2="";
Serial.println();
// ----------------------------------------------------------End of user application code
// Toggle TC2 LED
//toggle_LED(2);
if (((millis() - TimeSlice_TC2_Start_Time) > TimeSlice_TC2_Interval)) // 100 ms
{
while(1){
//over_run_Fault(2); // abort program and loop forever flash Time class 2 LED
}
}
else
{
while (((millis() - TimeSlice_TC2_Start_Time) <= TimeSlice_TC2_Interval)){ // remove to save MIPS but it removes precise timeclass execution
}
}
//------------------
//Timeclass 3 500 ms
//------------------
TimeSlice_TC3_Start_Time = millis();
//------------------------------------------------------------------User application code
// -----------------------------------------------------------------End of user application code
// Toggle TC3 LED
//toggle_LED(3);
if (((millis() - TimeSlice_TC3_Start_Time) > TimeSlice_TC3_Interval)) // 500 ms
{
while(1){
//over_run_Fault(3); // abort program loop forever flash Time class 3 LED
}
}
else
{
while (((millis() - TimeSlice_TC3_Start_Time) <= TimeSlice_TC3_Interval)){ // remove to save MIPS but it removes precise timeclass execution
}
}
} // End of main while loop
}// end of main loop
I tried multitasking by using a time slice shell from another user on this forum but to no avail, thank you in advance.