Pages: [1]   Go Down
Author Topic: SUMO ROBOT  (Read 1829 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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
Online Online
Brattain Member
*****
Karma: 644
Posts: 50506
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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?

Code:
    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 Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 309
Posts: 26536
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
A 4 second delay is ridiculous
But that's precisely what this
Code:
for(contador=0; contador<4; contador++)
  delay(1000);
does.

Also,
Code:
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Brattain Member
*****
Karma: 309
Posts: 26536
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 506
Posts: 19120
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Let me reiterate: Nowhere in your code are you performing any sort of sensor reading.

A sensor reading will looking something like this:

Code:
sensorVal = analogRead(A0);

Where in your code are you performing such an analogRead? (hint:  it's a rhetorical question)
Logged


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

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  smiley-confuse

ARDUINO ONE  by the way...

Code:


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 Offline
Brattain Member
*****
Karma: 309
Posts: 26536
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 644
Posts: 50506
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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 Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-confuse

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

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: