for loop confusion

I have a for loop that is only cycling through once and I cannot figure out why. I think it might have to do with some of the returns. I modified this code from the example in the OneWire Library. I hope to be getting the average of five temperature readings every 5 seconds. Thank you for your help.

#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(4);  // on pin 10 (a 4.7K resistor is necessary)
int x =0;
float fahrenheit[5];

void setup(void) {
  Serial.begin(9600);
}
void SoilTemp(){
  for(x=0; x<5; x++){  
        byte i;
        byte present = 0;
        byte type_s;
        byte data[12];
        byte addr[8];
        float celsius;
        
        if ( !ds.search(addr)) {
          ds.reset_search();
          delay(250);
          return;
        }
        
        if (OneWire::crc8(addr, 7) != addr[7]) {
            return;
        }
         // the first ROM byte indicates which chip
        switch (addr[0]) {
          case 0x10:
            type_s = 1;
            break;
          case 0x28:
            type_s = 0;
            break;
          case 0x22:
            type_s = 0;
            break;
          default:
            return;
        } 
      
        ds.reset();
        ds.select(addr);
        ds.write(0x44, 1);        // start conversion, with parasite power on at the end
        delay(150);     // maybe 750ms is enough, maybe not
        present = ds.reset();
        ds.select(addr);    
        ds.write(0xBE);         // Read Scratchpad
        for ( i = 0; i < 9; i++) {           // we need 9 bytes
          data[i] = ds.read();      
        }
      
        int16_t raw = (data[1] << 8) | data[0];
        if (type_s) {
          raw = raw << 3; // 9 bit resolution default
          if (data[7] == 0x10) {
            raw = (raw & 0xFFF0) + 12 - data[6];
          }
        } 
        else {
          byte cfg = (data[4] & 0x60);
          if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
          else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
          else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
        }
    celsius = (float)raw / 16.0;
    fahrenheit[x] = celsius * 1.8 + 32.0;
    Serial.println(fahrenheit[x]);
    Serial.println(x);
}
  float Avg_S_T =  (fahrenheit[0]+fahrenheit[1]+fahrenheit[2]+fahrenheit[3]+fahrenheit[4])/5.0;
  Serial.print("Average Soil Temperature: ");
  Serial.println(Avg_S_T);  
}
void loop() {
SoilTemp();
delay(5000);
}

I have a for loop

You have more than one. Which one is causing you grief?

I hope to be getting the average of five temperature readings every 5 seconds.

I don't think that is a reasonable hope.

PaulS:
You have more than one. Which one is causing you grief?

The outermost one. Everything inside is taken from the oneWire example and is working fine. I just want to loop through this code 5 times to get five readings.

PaulS:
I don’t think that is a reasonable hope.

the only reason I say this bit about the five seconds is because I call

void loop() {
SoilTemp();
delay(5000);
}

SoilTemp() every 5 seconds. What I am saying I would like to wait a while then enter the SoilTemp() loop and get these five readings and then go back to the void loop and do what I want with it for another five seconds.

Here is another example where I did this. I want this code to be similar in functionality but with this different sensor.

#include <Wire.h>
int t,l,x;
float Avg_A_T = 0;
float TempArray[5];
void setup()
{
Wire.begin();
Serial.begin(9600);
Serial.println("Begin");
}

void AirTemp(){
  Avg_A_T = 0;
  for(x = 0; x < 5; x++)
  {  
    Wire.requestFrom(0x4F, 2);
    while(Wire.available())
    {
    int8_t msb = Wire.read();
    int8_t lsb = Wire.read();
    // strip one bit of the lsb
    lsb = (lsb & 0x80 ) >> 7; // now lsb = 0 or 1
    // add to to form an float
    float T_C_A = msb + 0.5 * lsb;
    float T_F_A=(9.0/5.0)*T_C_A+32.0;
    TempArray[x] = T_F_A;
    //Serial.print(TempArray[x]);
    } 
  delay(1000);
  }
  Avg_A_T =  (TempArray[0]+TempArray[1]+TempArray[2]+TempArray[3]+TempArray[4])/5.0;
}

void loop()
{
AirTemp();
Serial.print("Average Temp F: ");
Serial.println(Avg_A_T);
delay(5000);
}

I think you need to show us the serial output from the sketch.

Anonymous printing sucks.

Serial.print("fahrenheit[");
Serial.print(x);
Serial.print("] = ");
Serial.println(fahrenheit[x]);

conveys a lot more information than what you are printing.

PaulS: I think you need to show us the serial output from the sketch.

Anonymous printing sucks.

Serial.print("fahrenheit[");
Serial.print(x);
Serial.print("] = ");
Serial.println(fahrenheit[x]);

conveys a lot more information than what you are printing.

Here you go. It is taking 5 seconds between each print so I believe that it is returning to the void loop between each printing instead of cycling through the outermost for loop.

fahrenheit[0] = 73.62
fahrenheit[0] = 73.62
fahrenheit[0] = 73.62
fahrenheit[0] = 73.62
fahrenheit[0] = 73.62

What does the fact that the index (X) in

fahrenheit[0] = 73.62

is always 0 suggest to you?

econjack:
What does the fact that the index (X) in

fahrenheit[0] = 73.62

is always 0 suggest to you?

It suggests that the for loop is being exited after only running through once. Is of the return statements that is kicking me out of the for loop?

https://www.arduino.cc/en/Reference/Return

How about adding

Serial.println(func);

into beginning of each function? Into loop() , AirTemp etc.

Should lead you to the problem.

By itself there is nothing to stop loop() to continue forever - so the spec "run 5 times " must be specified elsewhere else.

void loop() {

here

Serial.println(func);

AirTemp(); Serial.print("Average Temp F: "); Serial.println(Avg_A_T); delay(5000); }

I was able to get this to work by changing the location of the for loop. Here is a copy of my working code for closure.

#include <OneWire.h>

// OneWire DS18S20, DS18B20, DS1822 Temperature Example
//
// http://www.pjrc.com/teensy/td_libs_OneWire.html
//
// The DallasTemperature library can do all this work for you!
// http://milesburton.com/Dallas_Temperature_Control_Library

OneWire  ds(4);  // on pin 10 (a 4.7K resistor is necessary)
int x =0;
float fahrenheit[5], Avg_S_Temp;

void setup(void) {
  Serial.begin(9600);
}

void SoilTemp(){
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit[5];
    
  if ( !ds.search(addr)) {
    ds.reset_search();
    delay(250);
    return;
  }

  for(int x = 0; x<5; x++){
  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  
  delay(500);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  
  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad


  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
  }

  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  }
  celsius = (float)raw / 16.0;
  fahrenheit[x] = celsius * 1.8 + 32.0;
  Serial.print(fahrenheit[x]);
  Serial.println(" Fahrenheit");
  }
  Avg_S_Temp = (fahrenheit[0]+fahrenheit[1]+fahrenheit[2]+fahrenheit[3]+fahrenheit[4])/5.0;
  ds.reset_search();
}

void loop() {
  
SoilTemp();
Serial.println(Avg_S_Temp);
delay(5000);
}

The issue was that ds.search was returning 0 and returning early.