inconsistent serial communications

I am building a temperature controller to control the temps of 4 fermentors in our brewery to be interfaced with a computer running processing.

the controller works fine by itself but the serial stream of points shuts down. Sometimes it will last for hours sometimes minutes. The arduino continues to function properly but I have to reset the connection with the computer.

I have tried multiple computers/usb cords/arduinos with the same result.

I would appreciate any suggestions

thank you

steve

#include <EEPROM.h>
//#include <Ethernet.h>
//#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>


// data line to pin 3
#define ONE_WIRE_BUS 3

const int relay14c = 43;

const int relay11h = 42;
const int relay11c = 35;
const int relay12h = 36;
const int relay12c = 37;
const int relay13h = 38;
const int relay13c = 39;
const int relay1rh = 40;
const int relay1rc = 41;
const int chiller = 34;

int SetTempLow[16];
int SetTempHigh[16];
float ActualTemp[16];
char incoming[41];
String teststring;
int deviceID = 0;


// setup onewire to communicate with any OW device
OneWire oneWire(ONE_WIRE_BUS);

// pass OW reference to Dallas temp
DallasTemperature sensors(&oneWire);

// array for addresses
DeviceAddress ThermoOne,ThermoTwo,ThermoThree,ThermoFour,ThermoFive,ThermoSix,ThermoSeven,ThermoEight;


void setup() 
{
  Serial.begin(4800);
  
  
  pinMode (relay14c, OUTPUT);
  pinMode (relay11h, OUTPUT);
  pinMode(relay11c, OUTPUT);
  pinMode(relay12h, OUTPUT);
  pinMode(relay12c, OUTPUT);
  pinMode(relay13h, OUTPUT);
  pinMode(relay13c, OUTPUT);
  pinMode(relay1rh, OUTPUT);
  pinMode(relay1rc, OUTPUT);
  pinMode(chiller, OUTPUT);

  digitalWrite(relay14c, LOW);
  digitalWrite(relay11h, LOW);
  digitalWrite(relay11c, LOW);
  digitalWrite(relay12h, LOW);
  digitalWrite(relay12c, LOW);
  digitalWrite(relay13h, LOW);
  digitalWrite(relay13c, LOW);
  digitalWrite(relay1rh, LOW);
  digitalWrite(relay1rc, LOW);
  digitalWrite(chiller, LOW);
  
 // read setpoints from eeprom
 
   
   int value[20];
   int i;
  for (int i = 0; i < 20; i++){
    value[i] = EEPROM.read(i);}
SetTempHigh[1] = value[1];
SetTempHigh[2] = value[2];
SetTempHigh[3] = value[3];
SetTempHigh[4] = value[4];
SetTempHigh[5] = value[5];




// start the library
sensors.begin();

// locate the devises on the bus

sensors.getAddress(ThermoOne, 0);
sensors.getAddress(ThermoTwo, 1);
sensors.getAddress(ThermoThree, 2);
sensors.getAddress(ThermoFour, 3);
sensors.getAddress(ThermoFive, 4);
}


// function to print a device address

void printAddress(DeviceAddress deviceAddress)
{
  for (int i = 0; i < 8; i++)
  {
    // zero pad the address if necessary
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
    //Serial.print(SetTemp[i]);
  }
}

// function to print the temp for a device
 
 void printTemperature (DeviceAddress deviceAddress)
{
  float tempC = sensors.getTempC(deviceAddress);     
  ActualTemp[deviceID]= DallasTemperature::toFahrenheit(tempC);
  
}

void SendSerial()
{
  Serial.print("B");
  for (int i = 1; i < 6; i++){ 
  Serial.print(ActualTemp[i]); 

  Serial.print(",");
  
  Serial.print(SetTempHigh[i]);
  
  Serial.print(",");
  
  }
  Serial.println();
  
    
    
  }


// function t print infomation about a device

void printData(DeviceAddress deviceAddress)
{
    printTemperature(deviceAddress);
  
}

// function to trip relays
void relays()
{
  
  
  if (ActualTemp[1] > SetTempHigh[1]) {
    
    digitalWrite(relay11c, HIGH);    
  }
  else
  {
    digitalWrite(relay11c, LOW); 
    
  }  
  
 
  if (ActualTemp[2] > SetTempHigh[2]) {
    
    digitalWrite(relay12c, HIGH);
  }
else
  {
    digitalWrite(relay12c, LOW); 
    
  }  
 
  if (ActualTemp[3] > SetTempHigh[3]) {
    
    digitalWrite(relay13c, HIGH);    
  }
  else
  {
    digitalWrite(relay13c, LOW); 
    
  }
  if (ActualTemp[5] > SetTempHigh[5]) {
    digitalWrite(relay14c, HIGH);
    
  }
  else
  {
        digitalWrite(relay14c, LOW); 
  }
  if (ActualTemp[4] < SetTempLow[4]) {
    digitalWrite(relay1rh, HIGH);
    
  }
  else
  {
    digitalWrite(relay1rh, LOW);
    
  }
    
    if (ActualTemp[1] > SetTempHigh[1]){
    digitalWrite(chiller, HIGH);
    return;    
  }
  
  if (ActualTemp[2] > SetTempHigh[2]){
    digitalWrite(chiller, HIGH);
    return;    
  }
  
 /* 
  if (ActualTemp[3] > SetTempHigh[3]){
    digitalWrite(chiller, HIGH);
    return;

if (ActualTemp[5] > SetTempHigh[5]){
    digitalWrite(chiller, HIGH);
    return;    
  } 
 */ 
  else {
        digitalWrite(chiller, LOW);
  }
  
}




void loop()
{
  
  if (Serial.available() > 0) {
                // read the incoming byte:
                for(int i=1;i<12; i++){
                incoming[i] = Serial.read();
                
               
                SetTempHigh[1]=((int((incoming[1]-48))*10)+int((incoming[2]-48)));
                SetTempHigh[2]=((int((incoming[3]-48))*10)+int((incoming[4]-48)));
                SetTempHigh[3]=((int((incoming[5]-48))*10)+int((incoming[6]-48)));
                SetTempHigh[4]=((int((incoming[7]-48))*10)+int((incoming[8]-48)));
                SetTempHigh[5]=((int((incoming[9]-48))*10)+int((incoming[10]-48)));

// write setTemps to eephrom
int temp[10];
temp[1] = int (SetTempHigh[1]) ;
temp[2] = int(SetTempHigh[2]);
temp[3] = int (SetTempHigh[3]) ;
temp[4] = int(SetTempHigh[4]);
temp[5] = int(SetTempHigh[5]);

for (int i = 0; i < 20; i++){  
    EEPROM.write(i, temp[i]);
          }

  }  
}

  
  // call sensor.requestTemperature() to issue a global Temp
 // request to all devices on the bus

 sensors.requestTemperatures();
 
 
 // print device information
 deviceID = 1;
 printTemperature(ThermoOne);
 deviceID = 2;
 printTemperature(ThermoTwo);
 deviceID = 3;
 printTemperature(ThermoThree);
 deviceID = 4;
 printTemperature(ThermoFour);
 deviceID = 5;
 printTemperature(ThermoFive);
 //Serial.println();
 relays();
 SendSerial();
 delay(60000);
}
 if (Serial.available() > 0) {
                // read the incoming byte:
                for(int i=1;i<12; i++){
                incoming[i] = Serial.read();

If there is at least one byte available, read all 11 of them. Does that really seem like a good idea?

Array indices start at 0. Why do all of yours start at 1?

"If there is at least one byte available, read all 11 of them. Does that really seem like a good idea??"

I don't understand this question

"Array indices start at 0. Why do all of yours start at 1?"
I know not what I do..... thanks for pointing it out

steve

I don't understand this question

Serial data transmission is slow, compared to how fast the Arduino is. As soon as one byte arrives, Serial.available() returns a non-zero value. You then proceed to read 11 values from the serial port, even though only one has arrived. I guess that asking if that was reasonable is what confused you. So, I'll state that it is not.

If you want to read 11 bytes, you need to wait for there to be 11 to read.

yes I assumed the answer was no.

I didn't understand the speed thing... thank you

How long a delay is needed between bits? .1 sec?

steve

You need to reorganize your code either by not starting to read anything until 11 bytes are present if (Serial.available >= 11) or by reading bytes only when at least one is actually available as in the code in Chapter 8 of this demo.

With either of these approaches the Arduino is not hanging around waiting for slow data to arrive.

...R

How long a delay is needed between bits? .1 sec?

Two weeks. Or none.