'For' Loop

Hello.
I use 7 ultrasonic sensors for a project.
I want when ultrasonic sensor is out of range (returning int: 0) to make its value '100' instead of '0' (default)
I notice that the program doesn't function as expected. Still returns '0'

The senB, senBL, senBR, senML, senMR, senL, senR, has for value, the detected distance for each sensor

Here is the code.

      int convert[7] = {senB, senBL, senBR, senML, senMR, senL, senR};                                // If distance = 0  ->  distance = 100
      temp = 100;                                                  
    
      for(int i = 0; i == 6; i++) {                              
        if(convert[i] == 0) {                                   
          convert[i] = temp;            
        }
      }

Any suggestions?
Thanks

This:

  for(int i = 0; i == 6; i++)

was likely intended to be:

  for(int i = 0; i <= 6; i++)

Thanks wildbill for your reply.

I am still getting '0' instead of '100' :frowning:

This is the part of the code that I use

    NewPing sB(TRIGGER_PIN_B, ECHO_PIN_B, distance);       
    NewPing sBL(TRIGGER_PIN_BL, ECHO_PIN_BL, distance);    
    NewPing sBR(TRIGGER_PIN_BR, ECHO_PIN_BR, distance);  
    NewPing sML(TRIGGER_PIN_ML, ECHO_PIN_ML, distance);       
    NewPing sMR(TRIGGER_PIN_MR, ECHO_PIN_MR, distance);       
    NewPing sL(TRIGGER_PIN_L, ECHO_PIN_L, distance);       
    NewPing sR(TRIGGER_PIN_R, ECHO_PIN_R, distance);
    
    unsigned int SDB = sB.ping();        
    unsigned int SDBL = sBL.ping();      
    unsigned int SDBR = sBR.ping();      
    unsigned int SDML = sML.ping();      
    unsigned int SDMR = sMR.ping();     
    unsigned int SDL = sL.ping();       
    unsigned int SDR = sR.ping();       
  
    senB = SDB / US_ROUNDTRIP_CM;        
    senBL = SDBL / US_ROUNDTRIP_CM;      
    senBR = SDBR / US_ROUNDTRIP_CM;         
    senML = SDML / US_ROUNDTRIP_CM;         
    senMR = SDMR / US_ROUNDTRIP_CM;         
    senL = SDL / US_ROUNDTRIP_CM;   
    senR = SDR / US_ROUNDTRIP_CM;
   
    if (senB == 0 && senBL == 0 && senBR == 0 && senML == 0 && senMR == 0 && senL == 0 && senR == 0) {      // Check if sensors don't detect anything
      enabled = 7;
      
    } 
    else {
    
      int convert[7] = {senB, senBL, senBR, senML, senMR, senL, senR};                                // If distance = 0  ->  distance = 100
      temp = 100;                                                  
    
      for(int i = 0; i <= 6; i++) {                              
        if(convert[i] == 0) {                                   
          convert[i] = temp;            
        }
      }
      
      
      Serial.print(senB); // for debug
}

I am still getting '0' instead of '100' :frowning:

Not surprising, considering all this code:

      int convert[7] = {senB, senBL, senBR, senML, senMR, senL, senR};                                // If distance = 0  ->  distance = 100
      temp = 100;                                                  
    
      for(int i = 0; i <= 6; i++) {                              
        if(convert[i] == 0) {                                   
          convert[i] = temp;            
        }
      }

Is doing is replacing any 0 value in your array with 100. I think this assignment is backwards:

convert[i] = temp;

Is the variable temp getting changed somewhere else? temp is really a lousy name in this case because it is really being used as a constant. Better name would be something like hundred or cent.

So..I change the code to something like that

   int convert[7] = {senB, senBL, senBR, senML, senMR, senL, senR};                               
                                             
   for(int i = 0; i <= 6; i++) {                              
     if(convert[i] == 0) {                                   
       convert[i] = 100;            
     }
   }

value for senB remains '0'

EDIT:

I tried this code instead :

  int convert[7] = {senB, senBL, senBR, senML, senMR, senL, senR};                               
                                             
   for(int i = 0; i <= 6; i++) {                              
       convert[i] = 100;            
   }

Still nothing:(

I'm not surprised. This is not doing what you think:

      int convert[7] = {senB, senBL, senBR, senML, senMR, senL, senR};                                // If distance = 0  ->  distance = 100
      temp = 100;                                                  
    
      for(int i = 0; i <= 6; i++) {                              
        if(convert[i] == 0) {                                   
          convert[i] = temp;            
        }
      }
      
      
      Serial.print(senB); // for debug

What it is actually doing is:

  1. Copy the values from the ping sensors' readings into the array.
  2. Modify the contents of the array
  3. Display one ping sensor's value

At no point are you modifying the ping sensor's value, just the contents of the array.

Try printing convert[0] instead, and see what you get.

If you want to do it this way and modify the source variable, you will have to use pointers:

      int *convert[7] = {&senB, &senBL, &senBR, &senML, &senMR, &senL, &senR};                                // If distance = 0  ->  distance = 100
      temp = 100;                                                  
    
      for(int i = 0; i <= 6; i++) {                              
        if(*(convert[i]) == 0) {                                   
          *(convert[i]) = temp;            
        }
      }
      
      
      Serial.print(senB); // for debug

(untested).

Thanks guys for your help

@majenko that works!!! Thanks so much. :slight_smile:

Hello
Another problem comes up:(
Now, I have finally set up my sensors and when they are out of range, they return 100 (int).
I want to find the sensor which has detect an item closer from the other 6 sensors.

So the code is:

    NewPing sB(TRIGGER_PIN_B, ECHO_PIN_B, distance);       
    NewPing sBL(TRIGGER_PIN_BL, ECHO_PIN_BL, distance);    
    NewPing sBR(TRIGGER_PIN_BR, ECHO_PIN_BR, distance);   
    NewPing sML(TRIGGER_PIN_ML, ECHO_PIN_ML, distance);       
    NewPing sMR(TRIGGER_PIN_MR, ECHO_PIN_MR, distance);       
    NewPing sL(TRIGGER_PIN_L, ECHO_PIN_L, distance);       
    NewPing sR(TRIGGER_PIN_R, ECHO_PIN_R, distance);
    
    unsigned int SDB = sB.ping();        
    unsigned int SDBL = sBL.ping();      
    unsigned int SDBR = sBR.ping();      
    unsigned int SDML = sML.ping();      
    unsigned int SDMR = sMR.ping();      
    unsigned int SDL = sL.ping();        
    unsigned int SDR = sR.ping();        

    senB = SDB / US_ROUNDTRIP_CM;      
    senBL = SDBL / US_ROUNDTRIP_CM;      
    senBR = SDBR / US_ROUNDTRIP_CM;         
    senML = SDML / US_ROUNDTRIP_CM;         
    senMR = SDMR / US_ROUNDTRIP_CM;         
    senL = SDL / US_ROUNDTRIP_CM;   
    senR = SDR / US_ROUNDTRIP_CM;

    int *convert[7] = {&senB, &senBL, &senBR, &senML, &senMR, &senL, &senR};                                
      temp = 100;                                                  
    
      for(int i = 0; i <= 6; i++) {                              
        if(*(convert[i]) == 0) {                                   
          *convert[i] = temp;            
        }
      }
      
      int *sensor[7] = {&senB, &senBL, &senBR, &senML, &senMR, &senL, &senR};                            
      temp1 = 100;                                                  
    
      for(int i = 0; i <= 6; i++) {                              
        if(*(sensor[i]) < temp1) {                                   
          temp1 = *(sensor[i]);   
          sensor = i;         
        }
      }
    }
  
    
                     
    Serial.println(sensor);
    delay(1000);

instead of returning back the position of sensor in the array (0 - 6) it randomly returns numbers from '0' to '6'

          sensor = i;

sensor is the array containing the list of sensor values pointers. Try using a different variable, like "closestSensor".

sensor = i;

doesn't make sense. 'sensor' is an array not an int var. Just create another in like closestSensor = i. That will save the index.
You probably don't need the sensor[]... it's identical to convert[]....

@majenko @mistergreen

You are absolutely right..My mistake :astonished:

Thanks for your help guys. :slight_smile: