Pages: [1]   Go Down
 Author Topic: 'For' Loop  (Read 432 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 35
 « on: March 18, 2013, 10:49:36 am » Bigger Smaller 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
Karma: 43
Posts: 3279
 « Reply #1 on: March 18, 2013, 10:53:22 am » Bigger Smaller 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
Newbie
Karma: 0
Posts: 35
 « Reply #2 on: March 18, 2013, 11:01:20 am » Bigger Smaller Reset

I am still getting '0' instead of '100'
 Logged

Offline
Newbie
Karma: 0
Posts: 35
 « Reply #3 on: March 18, 2013, 11:05:26 am » Bigger Smaller 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
Karma: 82
Posts: 3123
 « Reply #4 on: March 18, 2013, 11:13:01 am » Bigger Smaller Reset

Quote
I am still getting '0' instead of '100'

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
Edison Member
Karma: 8
Posts: 1021
 « Reply #5 on: March 18, 2013, 11:19:10 am » Bigger Smaller 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
Newbie
Karma: 0
Posts: 35
 « Reply #6 on: March 18, 2013, 11:27:09 am » Bigger Smaller 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
Karma: 90
Posts: 3903
 « Reply #7 on: March 18, 2013, 11:32:41 am » Bigger Smaller 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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline
Newbie
Karma: 0
Posts: 35
 « Reply #8 on: March 18, 2013, 11:37:17 am » Bigger Smaller Reset

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

Offline
Newbie
Karma: 0
Posts: 35
 « Reply #9 on: March 18, 2013, 01:01:03 pm » Bigger Smaller 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
Karma: 90
Posts: 3903
 « Reply #10 on: March 18, 2013, 01:17:34 pm » Bigger Smaller Reset

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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

0
Offline
Full Member
Karma: 3
Posts: 134
Arduino rocks
 « Reply #11 on: March 18, 2013, 01:19:31 pm » Bigger Smaller 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
Newbie
Karma: 0
Posts: 35
 « Reply #12 on: March 18, 2013, 01:32:49 pm » Bigger Smaller Reset

@majenko @mistergreen

You are absolutely right..My mistake