Go Down

Topic: 'For' Loop (Read 684 times) previous topic - next topic

Jek90

Mar 18, 2013, 04:49 pm Last Edit: Mar 18, 2013, 04:52 pm by panosip Reason: 1
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.

Code: [Select]


     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

wildbill

This:
Code: [Select]
  for(int i = 0; i == 6; i++)

was likely intended to be:
Code: [Select]
  for(int i = 0; i <= 6; i++)

Jek90

Thanks wildbill for your reply.

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

Jek90

This is the part of the code that I use



Code: [Select]


    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
}



Arrch

Quote

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


Not surprising, considering all this code:

Code: [Select]
      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:

Code: [Select]
convert[i] = temp; 

kf2qd

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.

Jek90

#6
Mar 18, 2013, 05:27 pm Last Edit: Mar 18, 2013, 05:29 pm by panosip Reason: 1
So..I change the code to something like that

Code: [Select]


   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 :
Code: [Select]


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



Still nothing:(

majenko

I'm not surprised.  This is not doing what you think:
Code: [Select]

      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:
Code: [Select]

      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).
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Jek90

Thanks guys for your help

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

Jek90

#9
Mar 18, 2013, 07:01 pm Last Edit: Mar 18, 2013, 07:02 pm by panosip Reason: 1
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:

Code: [Select]


    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'

majenko

Code: [Select]

          sensor = i;         

sensor is the array containing the list of sensor values pointers.  Try using a different variable, like "closestSensor".
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

mistergreen

Code: [Select]

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[]....

Jek90

@majenko @mistergreen

You are absolutely right..My mistake :smiley-eek:

Thanks for your help guys. :)

Go Up