Need some array help

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

Post your full code please.

Full code has been posted

Your problem lies in your IF statement. if(temperaturearray[ i ] > hottest) Your storing the first element of the array into hottest "hottest = temperaturearray[0]; " and your telling your code to go through and see which is the highest value, but your not telling it what to do when the highest value is at temperaturearray[0]. So by changing "if(temperaturearray[ i ] > hottest)" to "if(temperaturearray[ i ] >= hottest)", it does what you want it to.

The following is how I tested your code.

float temperaturearray[]  = {480.0, 30.0, 500.0, 74.0, 80.0}; // 2
float temperaturearray1[]  = { 74.0, 80.0,480.0, 30.0, 500.0}; // 4
float temperaturearray2[]  = {500.0, 74.0, 80.0,480.0, 30.0}; // 0
/* greatest temperature stored in largest */
float hottest = 0;
int currentlargestindex = 0;
int previouslargestindex = 0;

void setup()
{
  Serial.begin(9600);
  highestTemperature(temperaturearray);
  highestTemperature(temperaturearray1);
  highestTemperature(temperaturearray2);
  
}

void loop() { }

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

Output:
Before.
if(temperaturearray[ i ] > hottest)

previous largest index = 0
current largest index = 2
previous largest index = 2
current largest index = 4
previous largest index = 4
current largest index = 4

After:
if(temperaturearray[ i ] >= hottest)

previous largest index = 0
current largest index = 2
previous largest index = 2
current largest index = 4
previous largest index = 4
current largest index = 0

Thank you very much. That equal sign caused far to much stress.

I have another question about the stepper motor. With the above code it seems that the stepper motor will only ever go up to STEPS2. It won't go further then that. Once it does up to STEPS2 number of steps it continues on with the rest of the program. Now I have an easy work around, just calling it to do STEPS2 and STEPS1 however many times I need to do in places of STEPS3, STEPS4, and STEPSH but that obviously isn't ideal. Another error is that when ever I make those constant values integers it only rotates a short time and stops (much shorter then even STEPS1). I don't have that problem when using floats.

Can anyone offer some insight into those issues, as to why the happen, and how to fix in a nicer way then what I have done.

I don't have that problem when using floats.

Then you should be printing out the values in the variables before using them. The number of steps SHOULD be int. There is no way to step 0.47 steps.

  /* enables pulse width modulation */

No, it doesn't.

You don't need 5 gotoXXXX functions. You need 1. Each does exactly the same thing (or should) with the exception of which number of steps to end up at.

Using arrays instead of numbering discrete variables would make stepping from one position ("I'm at 3") to another ("I need to be at 0") a lot easier. Look up the previous position and the new position. Subtract one from the other. Step that number of times.

The stepper motor setup stuff was pulled straight from an online tutorial. I probably should have gone over it more rather then just accepted it as working.

However would something like this work as the code for the single gotosensor statement? If I am understanding what you are saying correctly. At least for making it one function rather then 5. Would need to work a bit with not using discrete numbers.

void gotosensor()
{
whereto = currentlargestindex - previouslargestindex;
  if(whereto == 0)
  {
     delay(1000);
  }
  else if(whereto == 1)
  {
    myStepper.step(STEP1);
  }  
  else if(whereto == 2)
  {
    myStepper.step(STEP2);
  }  
  else if(whereto == 3)
  {
myStepper.step(STEP3);
  }  
  else if(whereto == 4)
  {
myStepper.step(STEP4);
  }  
 else if(whereto == -1)
  {
myStepper.step(-STEP1);
  } 
 else if(whereto == -2)
  {
myStepper.step(-STEP2);
  } 
 else if(whereto == -3)
  {
myStepper.step(-STEP3);
  } 
 else if(whereto == -4)
  {
myStepper.step(-STEP-4);
  } 
}

I don't think the -STEP-4 part is what you want, and I'd use a switch statement instead of all those ifs, but, yeah that looks a lot better.