Offline
Newbie
Karma: 0
Posts: 4
|
 |
« on: June 06, 2012, 10:56:58 pm » |
I all am working on a sumo car robot. it uses 4 analog sensors. 1 will detect the other robot to push it out of the ring, 2 (1 back 1 front) will sense the white strip to stop and 1 on the back if pushed from the back tried to use an array in order to scan all sensors one at the time but its not working. will use servo motors but on the program I used led instead off=reverse on=forward this is my code. the sensors will read 5v (1023) when not sensing and less than 1V then sensing (200) Any suggestions will be very appreciated int myPins[] = {A0,A1,A2,A3}; //array de 4 campos A0=0, A1=1.... int motorA=2; //representado como led en proto its motorA on board int motorB=4; //representado como led en proto its motorB on board
int contador=0; //contador para uso con array int to use in array int timer=50; int sensorAttack=A0; //sensor scaneo ataque dont think its needed int sensorFront=A1; //sensor borde delantero dont think its needed int sensorBack=A2; //sensor borde trasero dont think its needed int sensorHit=A3; //sensor de empuje trasero dont think its needed
void setup(){ pinMode(motorA, OUTPUT); pinMode(motorB, OUTPUT); } void loop(){ for(contador=0; contador<4; contador++) delay(1000); { switch(contador){ case 0: if(myPins[contador]<400){ digitalWrite(motorA, HIGH); digitalWrite(motorB, HIGH); //delay(1000); } break; case 1: if(myPins[contador]<400){ digitalWrite(motorA, LOW); digitalWrite(motorB, LOW); //delay(2000); } break; case 2: if(myPins[contador]<400){ digitalWrite(motorA,HIGH); digitalWrite(motorB, HIGH); //delay(2000); } break; case 3: if(myPins[contador]<400){ digitalWrite(motorA, LOW); digitalWrite(motorB, LOW); delay(50); } break; } digitalWrite(motorB, HIGH); digitalWrite(motorA, LOW); } }
Moderator edit: [code] [/code] tags added.
|
|
|
|
« Last Edit: June 12, 2012, 02:36:55 am by AWOL »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 332
Posts: 36396
Seattle, WA USA
|
 |
« Reply #1 on: June 10, 2012, 07:31:08 pm » |
for(contador=0; contador<4; contador++) delay(1000); A delay(4000) would be simpler. Clearly, your curly braces are in the wrong place. A 1 second delay between readings is equivalent to putting a sign on the robot that says "I'm a loser!". Is that what you really want? if(myPins[contador]<400){ This is in the case 0 section. myPins[0] is A0, which is either 14 or 53, depend on your Arduino. Both values are less than 400, so this test will always be true. If it is the value to be read from the pin that is to be compared to 400, you have two problems. The pin number should be 0, not A0. The A0 alias is for use when you are using the analog pin as a digital pin, which is NOT what you seem to be doing here. Second, you never actually read the value of the pin. You appear to be missing a call to analogRead(). Finally, that for loop and switch statement is plain stupid. Get rid of them, and all the cases.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #2 on: June 12, 2012, 02:30:50 am » |
thanks for the replay, not very helpful. 1- Im using arduino one, so the A0 pin stands for the A0 pin, the analog pin, if you read what i wrote, you would notice that I state that the IR sensor reads a value of 1023 (analog) and when is sensing reads about 200, so the "if" statement is correct it will do the function only when the sensor is been sensing some thing.
2- A 4 second delay is ridiculous the idea of the array is to go trough all the sensors as fast as possible, with a 4 s delay it will be a foot away from the ring by the time is stooping the idea is not to leave the ring
3- you are right about the "analogRead" Thats probably the reason why is not working, if I run the same program but only with the CASE SWITCH (no array) and using all the other read values and all that it will work, but there will be sensor conflicts on the performance
4- if I get rid of the switch case, what do you suggest on the program to execute a different action for each one of the sensors?
thanks
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 142
Posts: 19339
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: June 12, 2012, 02:32:14 am » |
A 4 second delay is ridiculous But that's precisely what this for(contador=0; contador<4; contador++) delay(1000); does. Also, switch(contador){ here, "contador" will always have the value 4, but you don't have a case 4. It would also screw up your array indices. Please remember to use [code] [/code] tags when posting code.
|
|
|
|
« Last Edit: June 12, 2012, 02:38:37 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
New Hampshire
Offline
God Member
Karma: 13
Posts: 776
There are 10 kinds of people, those who know binary, and those who don't.
|
 |
« Reply #4 on: June 12, 2012, 07:38:41 am » |
so the "if" statement is correct it will do the function only when the sensor is been sensing some thing.
The "if" statement is, in fact, incorrect. You are comparing the pin value to 400, which as Pauls said will always be less than 400. Nowhere in your code are you performing any sort of sensor reading.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #5 on: June 19, 2012, 01:45:04 am » |
so the "if" statement is correct it will do the function only when the sensor is been sensing some thing.
The "if" statement is, in fact, incorrect. You are comparing the pin value to 400, which as Pauls said will always be less than 400. Nowhere in your code are you performing any sort of sensor reading. The "if" statement is indeed comparing the sensor value which it will always be 1023 unless the sensor senses some thing which will read less than 200. In other words, if it's not reading = 1023 it should not execute what ever the "if" has, if it senses it should execute the "if" instruction. Thanks for the reply
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 142
Posts: 19339
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: June 19, 2012, 01:46:38 am » |
The "if" statement is indeed comparing the sensor value Not in the code you posted it isn't.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 225
Posts: 14081
Lua rocks!
|
 |
« Reply #7 on: June 19, 2012, 01:52:49 am » |
thanks for the replay, not very helpful. 1- Im using arduino one, so the A0 pin stands for the A0 pin, the analog pin, if you read what i wrote, ...
The reply was helpful. Unfortunately you didn't read or understand it. So slow down and look at what PaulS told you.
|
|
|
|
|
Logged
|
|
|
|
|
New Hampshire
Offline
God Member
Karma: 13
Posts: 776
There are 10 kinds of people, those who know binary, and those who don't.
|
 |
« Reply #8 on: June 19, 2012, 07:39:27 am » |
Let me reiterate: Nowhere in your code are you performing any sort of sensor reading.A sensor reading will looking something like this: sensorVal = analogRead(A0);
Where in your code are you performing such an analogRead? (hint: it's a rhetorical question)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #9 on: June 20, 2012, 03:14:30 am » |
Thank you all for all the replies, i did some changes in fact to add the line for anlalogRead cause that was missing but still not going ARDUINO ONE by the way...
int myPins[] = {A0,A1,A2,A3}; //array analog pins A0, A1... int motorA=2; //servo motor, will use just a LED. LED HIGH will go forward, led LOW backwards int motorB=4; //same
int contador=0; //counter to move trough the switch case int i=0; // constant to read sensors one @ the time in array int timer=50; int valor=0;
void setup(){ pinMode(motorA, OUTPUT); pinMode(motorB, OUTPUT); } void loop(){
digitalWrite(motorB, HIGH); digitalWrite(motorA, LOW); for(i=0; i<4; i++){ //will make analogRead the Pin in the array and assigned to constant "valor" valor=analogRead(myPins[i]); for(contador=0; contador<4; contador++){ delay(1000); //delay shouldnt be that big, sensor reads in uS switch(contador){ case 0: if(valor<400){ digitalWrite(motorA, HIGH); digitalWrite(motorB, HIGH); //delay(1000); } break; case 1: if(valor<400){ digitalWrite(motorA, LOW); digitalWrite(motorB, LOW); //delay(2000); } break; case 2: if(valor<400){ digitalWrite(motorA,HIGH); digitalWrite(motorB, HIGH); //delay(2000); } break; case 3: if(valor<400){ digitalWrite(motorA, LOW); digitalWrite(motorB, LOW); //delay(50); } break; } } } }
I tried to make the array using constants, but not sure if that's possible, is it? Any suggestions are appreciated
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 142
Posts: 19339
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: June 20, 2012, 03:49:49 am » |
Any suggestions are appreciated You could tell us what is happening that should not, or what is not happening that should
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 332
Posts: 36396
Seattle, WA USA
|
 |
« Reply #11 on: June 22, 2012, 07:26:33 pm » |
for(i=0; i<4; i++){ //will make analogRead the Pin in the array and assigned to constant "valor" valor=analogRead(myPins[i]); for(contador=0; contador<4; contador++){ delay(1000); //delay shouldnt be that big, sensor reads in uS GET RID OF THE STUPID DELAY!
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 98
Posts: 6769
-
|
 |
« Reply #12 on: June 23, 2012, 10:05:56 am » |
Thank you all for all the replies, i did some changes in fact to add the line for anlalogRead cause that was missing but still not going It seems pointless to arrange your sensors in an array and read them in a loop only to walk through the list of sensors in a switch statement. The loop/switch construct is not eliminating any code duplication or providing any abstraction. You might just as well delete the loop and the switch statement and simply have a chunk of code to deal with each sensor in turn.
|
|
|
|
|
Logged
|
|
|
|
|
|