Pages: [1]   Go Down
Author Topic: 'For' Loop  (Read 556 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

      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
« Last Edit: March 18, 2013, 10:52:59 am by panosip » Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks wildbill for your reply.

I am still getting '0' instead of '100' smiley-sad
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the part of the code that I use



Code:

    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
}


Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3357
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I am still getting '0' instead of '100' smiley-sad

Not surprising, considering all this code:

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:

Code:
convert[i] = temp; 
Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1024
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So..I change the code to something like that

Code:

   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:

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


Still nothing:(
« Last Edit: March 18, 2013, 11:29:51 am by panosip » Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not surprised.  This is not doing what you think:
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;           
        }
      }
     
     
      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:
      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).
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys for your help

@majenko that works!!! Thanks so much. smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

    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'
« Last Edit: March 18, 2013, 01:02:52 pm by panosip » Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
          sensor = i;         
sensor is the array containing the list of sensor values pointers.  Try using a different variable, like "closestSensor".
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

0
Offline Offline
Sr. Member
****
Karma: 8
Posts: 290
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@majenko @mistergreen

You are absolutely right..My mistake smiley-eek

Thanks for your help guys. smiley
Logged

Pages: [1]   Go Up
Jump to: