Cant figure out what I am doing wrong

I am trying to do a temperature sensor that if a certain temp triggers a set of 5 led's and if its not that temp or higher it should trigger the other 5 led's I have tried a bunch of different ways I can think of and the led's are not lighting up how I am wanting. I am hoping someone can point me to where I am making my mistake in my code please. Thanks for any insight on this

int i;
int temperaturePin = 4;     // pin that the sensor for front cab is attached to
int ledPin[] = {
  4, 5, 6, 7, 8, 9, 10, 11, 12, 13};          
float getVoltage(int temperaturePin)
{
  return(analogRead(temperaturePin) * .004882814);   // converting from a 0 to 1024 digital range to 0 to 5 volts (each 1 reading equals - 5 milivolts
}


void setup() {
  
  // initialize serial communications:
  Serial.begin(57600);
}

void loop() {
  for(int i = 4; i <= 13; i++){
    pinMode(ledPin[i], OUTPUT);
  }
  float temperature = getVoltage(temperaturePin);
  temperature = (((temperature - .5) * 100)*1.8) + 32;
  if(temperature <= 79){
    digitalWrite(ledPin[9], HIGH);
    digitalWrite(ledPin[10], HIGH);
    digitalWrite(ledPin[11], HIGH);
    digitalWrite(ledPin[12], HIGH);
    digitalWrite(ledPin[13], HIGH);
    delay(2000);
  }

  else
  {
    digitalWrite(ledPin[4], HIGH);
    digitalWrite(ledPin[5], HIGH);
    digitalWrite(ledPin[6], HIGH);
    digitalWrite(ledPin[7], HIGH);
    digitalWrite(ledPin[8], HIGH);
    delay(2000);
  }

  // print the analog value:
  Serial.print("The temperature is ");
  Serial.println(temperature);
  delay(2000);

}

Your ledPin array is 0 based so the indexes are ledPin[0] to ledPin[9].

Not ledPin[4] to ledPin[13] as you are assuming.

Also you don't reset the other set of pins to LOW when things change, so they'll all end up stuck on HIGH.

I would move the pinMode loop into setup() since that's the natural place to do such configuration.

Also: It seems like you don't need the delay in the if / else statement. (since you are using the same delay in either case, it would make more sense leaving the delay until after the if/else statement.
:slight_smile: