I need some help with an array I am making. From what I have tested out with the code below I can never get back to an index array value of 0.
For example. The first time I call this it works fine. Previouslargestindex is 0 and currentlargestindex is 1 (second spot in array). But when I want to go back to the first position the next time I populate the array it has previouslargestindex = 1 (which is correct), and currentlargestindex = 1 (which is incorrect, it should be 0).
Can someone take a look at my code and see what I am doing wrong? Note that getDeviceCount is equal to 5.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Stepper.h>
/* array of temperatures all intizlized at zero, if the number of sesnors are changed this must be manually updated */
float temperaturearray[] = {0, 0, 0, 0, 0};
/* greatest temperature stored in largest */
float hottest = 0;
int currentlargestindex = 0;
int previouslargestindex = 0;
const float STEPS = 200;
const float STEPS0 = STEPS * 0;
const float STEPS1 = STEPS * 80.64;
const float STEPS2 = STEPS * 161.28;
const float STEPS3 = STEPS * 241.92;
const float STEPS4 = STEPS * 322.56;
const float STEPSH = STEPS * 350;
Stepper myStepper(STEPS, dirA, dirB);
#define ONE_WIRE_BUS 2
OneWire ourWire(ONE_WIRE_BUS);
DallasTemperature sensors(&ourWire);
DeviceAddress Probe01 = { 0x28, 0x86, 0x66, 0x67, 0x04, 0x00, 0x00, 0x43 };
DeviceAddress Probe02 = { 0x28, 0x49, 0x36, 0x67, 0x04, 0x00, 0x00, 0x6D };
DeviceAddress Probe03 = { 0x28, 0xC7, 0x6B, 0x67, 0x04, 0x00, 0x00, 0xED };
DeviceAddress Probe04 = { 0x28, 0x56, 0x40, 0x67, 0x04, 0x00, 0x00, 0xE3 };
DeviceAddress Probe05 = { 0x28, 0xCB, 0x3D, 0x68, 0x04, 0x00, 0x00, 0x03 };
void setup()
{
Serial.begin(9600);
Serial.print("Initializing Temperature Control Library Version ");
Serial.println(DALLASTEMPLIBVERSION);
sensors.begin();
sensors.setResolution(Probe01, 12);
sensors.setResolution(Probe02, 12);
sensors.setResolution(Probe03, 12);
sensors.setResolution(Probe04, 12);
sensors.setResolution(Probe05, 12);
myStepper.setSpeed(60);
/* enables pulse width modulation */
pinMode(pwmA, OUTPUT);
digitalWrite(pwmA, HIGH);
pinMode(pwmB, OUTPUT);
digitalWrite(pwmB, HIGH);
/* disables brakes */
pinMode(brakeA, OUTPUT);
digitalWrite(brakeA, LOW);
pinMode(brakeB, OUTPUT);
digitalWrite(brakeB, LOW);
gohome();
Serial.println("at home");
Serial.begin(9600);
}
void loop()
{
fillarray(temperaturearray);
Serial.println("array filled");
highestTemperature(temperaturearray);
Serial.println("highest temp ");
Serial.println(hottest);
/* finds which index value contains the hottest temperature then calls the function that will move the fan to that index value */
if (hottest == temperaturearray[0])
{
Serial.println("at 1st");
gotofirstsensor();
}
else if (hottest == temperaturearray[1])
{
Serial.println("at 2nd");
gotosecondsensor();
}
else if (hottest == temperaturearray[2])
{
Serial.println("at 3rd");
gotothirdsensor();
}
else if (hottest == temperaturearray[3])
{
Serial.println("at 4th");
gotofourthsensor();
}
else
{
Serial.println("at 5th");
gotofifthsensor();
}
delay(10000);
}
/* highestTemperature function finds the largest number in the array, meaning the highest temperature */
float highestTemperature(float temperaturearray[])
{
int arraylength = 5;
hottest = temperaturearray[0];
previouslargestindex = currentlargestindex;
Serial.print("previous largest index = ");
Serial.println(previouslargestindex);
for(int i = 0; i < arraylength; i++) //loops through the array
{
if(temperaturearray[i] > hottest) //looks for any numbers larger then the current largest
{
hottest = temperaturearray[i]; //if any numbers higher then current largest, it makes that vaule the new largest value
currentlargestindex = i; //saves the index of the largest number for use in determining where to send the motor.
}
}
Serial.print("current largest index = ");
Serial.println(currentlargestindex);
}
/* moves the motor from the current sensor to the first temperature sensor, which is the hottest */
/* the code that sent us here tells us what the current index value is, using the previous index value we know how far to move the fan */
void gotofirstsensor()
{
if(previouslargestindex == 0)
{
Serial.println("doing nothing at 1");
delay(1000);
}
else if(previouslargestindex == 1)
{
Serial.println("going to 1 from 2");
myStepper.step(-STEPS1); // steps the motor towards the first sensor
}
else if(previouslargestindex == 2)
{
Serial.println("going to 1 from 3");
myStepper.step(-STEPS2); // steps the motor towards the first sensor
}
else if(previouslargestindex == 3)
{
Serial.println("going to 1 from 4");
myStepper.step(-STEPS3); // steps the motor towards the first sensor
}
else
{
Serial.println("going to 1 from 5");
myStepper.step(-STEPS4); // steps the motor towards the first sensor
}
}
/* moves the motor from the current sensor to the second temperature sensor, which is the hottest */
/* the code that sent us here tells us what the current index value is, using the previous index value we know how far to move the fan */
void gotosecondsensor()
{
if(previouslargestindex == 0)
{
Serial.println("going to 2 from 1");
myStepper.step(STEPS1); // steps the motor towards the second sensor
}
else if(previouslargestindex == 1)
{
Serial.println("doing nothing at 2");
delay(1000);
}
else if(previouslargestindex == 2)
{
Serial.println("going to 2 from 3");
myStepper.step(-STEPS1); // steps the motor towards the second sensor
}
else if(previouslargestindex == 3)
{
Serial.println("going to 2 from 4");
myStepper.step(-STEPS2); // steps the motor towards the second sensor
}
else
{
Serial.println("going to 2 from 5");
myStepper.step(-STEPS3); // steps the motor towards the second sensor
}
}
/* moves the motor from the current sensor to the third temperature sensor, which is the hottest */
/* the code that sent us here tells us what the current index value is, using the previous index value we know how far to move the fan */
void gotothirdsensor()
{
if(previouslargestindex == 0)
{
Serial.println("going to 3 from 1");
myStepper.step(STEPS2); // steps the motor towards the third sensor
}
else if(previouslargestindex == 1)
{
Serial.println("going to 3 from 2");
myStepper.step(STEPS1); // steps the motor towards the third sensor
}
else if(previouslargestindex == 2)
{
Serial.println("doing nothing at 3");
delay(1000);
}
else if(previouslargestindex == 3)
{
Serial.println("going to 3 from 4");
myStepper.step(-STEPS1); // steps the motor towards the third sensor
}
else
{
Serial.println("going to 3 from 5");
myStepper.step(-STEPS2); // steps the motor towards the third sensor
}
}
/* the code that sent us here tells us what the current index value is, using the previous index value we know how far to move the fan */
void gotofourthsensor()
{
if(previouslargestindex == 0)
{
Serial.println("going to 4 from 1");
myStepper.step(STEPS3);
}
else if(previouslargestindex == 1)
{
Serial.println("going to 4 from 2");
myStepper.step(STEPS2);
}
else if(previouslargestindex == 2)
{
Serial.println("going to 4 from 3");
myStepper.step(STEPS1);
}
else if(previouslargestindex == 3)
{
Serial.println("doing nothing at 4");
delay(1000);
}
else
{
Serial.println("going to 4 from 5");
myStepper.step(-STEPS1); // steps the motor towards the fourth sensor
}
}
/* moves the motor from the current sensor to the fifth temperature sensor, which is the hottest */
/* the code that sent us here tells us what the current index value is, using the previous index value we know how far to move the fan */
void gotofifthsensor()
{
if(previouslargestindex == 0)
{
Serial.println("going to 5 from 1");
myStepper.step(STEPS4);
}
else if(previouslargestindex == 1)
{
Serial.println("going to 5 from 2");
myStepper.step(STEPS3);
}
else if(previouslargestindex == 2)
{
Serial.println("going to 5 from 3");
myStepper.step(STEPS2);
}
else if(previouslargestindex == 3)
{
Serial.println("going to 5 from 4");
myStepper.step(STEPS1);
}
else
{
Serial.println("doing nothing at 5");
delay(1000);
}
}
void gohome()
{
myStepper.step(-STEPS1);
}
float fillarray(float temperaturearray[])
{
sensors.requestTemperatures();
temperaturearray[0] = sensors.getTempC(Probe01);
temperaturearray[1] = sensors.getTempC(Probe02);
temperaturearray[2] = sensors.getTempC(Probe03);
temperaturearray[3] = sensors.getTempC(Probe04);
temperaturearray[4] = sensors.getTempC(Probe05);
}