Variable changing value unexpectedly

This is some code being brought over from an arduino to control fermentation temps. it reades temps from some onewire sensors all of which works fine.

My confusion comes with respect to the SetTempHigh variable. Why does its value change? I set it to 68 and it stays that way for a cycle then set1 changes to 1000000000 and 2and 3 to 0

You will notice I have much of the code blocked of to limit complexity.
also if I move
test =SetTempHigh[1];
test2=SetTempHigh[2];

to inside the loop the values of test and test2 change.

you can view the ubidots dashboard here

thanks steve

#include <Ubidots.h>

// This #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature.h"

// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>


// This #include statement was automatically added by the Particle IDE.
#include <flashee-eeprom.h>


//#pragma SPARK_NO_PREPROCESSOR


#define TOKEN "A1E-5jwFy81e8SllpBzA3PY5vET9MbhXhi"  // Put here your Ubidots TOKEN

Ubidots ubidots(TOKEN);

// data line to pin 2
#define ONE_WIRE_BUS 2

//const int relay11h = 0;
const int relay11c = 3;
//const int relay12h = 1;
const int relay12c = 4;
//const int relay13h = 7;
const int relay13c = 5;
const int chiller = 6;

int SetTempLow[16];
int SetTempHigh[16];
float ActualTemp[16];
char incoming[41];
String teststring;
int deviceID = 0;
double FermTemp[4];
int test;
int test2;


// 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 (relay11h, OUTPUT);
  pinMode(relay11c, OUTPUT);
  //pinMode(relay12h, OUTPUT);
  pinMode(relay12c, OUTPUT);
  // pinMode(relay13h, OUTPUT);
  pinMode(relay13c, OUTPUT);
  pinMode(chiller, OUTPUT);

  //digitalWrite(relay11h, LOW);
  digitalWrite(relay11c, LOW);
  //digitalWrite(relay12h, LOW);
  digitalWrite(relay12c, LOW);
  //digitalWrite(relay13h, LOW);
  digitalWrite(relay13c, 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];
  SetTempHigh[1] = 68;
  SetTempHigh[2] = 68;
  SetTempHigh[3] = 68;
  SetTempHigh[4] = 68;
  SetTempHigh[5] = 68;


  // 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);


  Spark.variable("ThermoOne", &FermTemp[1], DOUBLE);
  //Spark.variable("ThermoTwo", &ActualTemp[2], DOUBLE);
  //Spark.variable("ThermoThree", &ActualTemp[3], DOUBLE);
  //Spark.variable("ThermoFour", &ActualTemp[4], DOUBLE);
  //Spark.variable("ThermoFour", SetTempHigh[1], DOUBLE);
  test = SetTempHigh[1];
  test2 = SetTempHigh[2];
}


// function to print a device address
void loop()
{

  // if (Serial.available() >= 10) {
  //               // read the incoming byte:
  //              for(int i=0;i<10; i++){
  //               incoming[i] = Serial.read();
  //                             }


  //             SetTempHigh[1]=((int((incoming[0]-48))*10)+int((incoming[1]-48)));
  //             SetTempHigh[2]=((int((incoming[2]-48))*10)+int((incoming[3]-48)));
  //            SetTempHigh[3]=((int((incoming[4]-48))*10)+int((incoming[5]-48)));
  //             SetTempHigh[4]=((int((incoming[6]-48))*10)+int((incoming[7]-48)));
  //             SetTempHigh[5]=((int((incoming[8]-48))*10)+int((incoming[9]-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]);
  //temp[1]=68;
  //temp[2]=68;
  //temp[3]=68;
  //temp[4]=68;
  //temp[5]=68;

  //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);

  //Serial.println();
  //relays();
  // SendSerial();
  delay(5000);
}

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]);
    FermTemp[i] = 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()
{
  test2 = test;
  ubidots.add("Fermenter1", ActualTemp[1]);
  ubidots.add("Fermenter2", ActualTemp[2]);
  ubidots.add("Fermenter3", ActualTemp[3]);
  ubidots.add("Fermenter4", ActualTemp[4]);
  ubidots.add("set1", SetTempHigh[1]);
  ubidots.add("set2", SetTempHigh[2]);
  ubidots.add("set3", SetTempHigh[3]);
  ubidots.add("temp", test);
  ubidots.add("temp2", test2);
  ubidots.sendAll();

  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[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;
  }
  else {
    digitalWrite(chiller, LOW);
  }

}

Have you tried reducing your code to a bare minimum working example? That often filters out the mistakes, or makes you reconsider what could be going wrong. If nothing else, it cleans up the code, and allows you to find mistakes more easily without having to figure out what the code is trying to do.

You’re also still using some ancient syntax, like the ‘spark’ calls instead of ‘particle’. If you were to make a nice clean minimal example, these sorts of things would be resolved as well.

1 Like

yes i did.

And what did that do, because the code above certainly isn’t minimal? If only you’d remove all unnecessary parts (such as commented out chunks) that’d make it more readable to begin with. Also, the ‘spark’ calls definitely aren’t updated.

Also, since you already have array, why not just use a loop to iterate over the arreay instead just copy pasting the same lines of code multiple times
e.g.

  SetTempHigh[1] = 68;
  SetTempHigh[2] = 68;
  SetTempHigh[3] = 68;
  SetTempHigh[4] = 68;
  SetTempHigh[5] = 68;
// or
  ubidots.add("Fermenter1", ActualTemp[1]);
  ubidots.add("Fermenter2", ActualTemp[2]);
  ubidots.add("Fermenter3", ActualTemp[3]);
  ubidots.add("Fermenter4", ActualTemp[4]);
  ubidots.add("set1", SetTempHigh[1]);
  ubidots.add("set2", SetTempHigh[2]);
  ubidots.add("set3", SetTempHigh[3]);
// or
  if (ActualTemp[1] > SetTempHigh[1]) {
    digitalWrite(relay11c, HIGH);
  }
  else
  {
    digitalWrite(relay11c, LOW);
  }

i commented all the sections related to the variable in question eeprom etc

normally those values are set via a eeprom read. I commented that section and just set the manually to 68.

The statement still stands for the filling from EEPROM, as you do this there

  //SetTempHigh[1] = value[1];
  //SetTempHigh[2] = value[2];
  //SetTempHigh[3] = value[3];
  //SetTempHigh[4] = value[4];
  //SetTempHigh[5] = value[5];

copy/pasting

And as Jordy already said, due to all this clutter (commented or not) we can’t clearly see the code and hence can’t (easily) find the cause of the issue.

1 Like

im cleaning it up will post again soon thanks for wour help and suggestions

s

ok the values now dont change.

often the best teachers lead rather than tell thanks guys

ill start adding stuff back

s

4 Likes