Go Down

Topic: i dont get it (Read 4426 times) previous topic - next topic

jada

bill2009
well you made me a bit confused your code uses only two states and not in the way cr0sh had told
(or i didnt understand states correctly ?  :-? )

jada


Quote
Code:unsigned long readPing (int pingPin)
{
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);
 pinMode(pingPin, INPUT);
 return pulseIn(pingPin, HIGH);
}

so the ping code i used is not correct with eregards to the data types ?
if possible could you eloborate so tha  i get a clear picture  ::)



AWOL

Quote
so the ping code i used is not correct with eregards to the data types

The Ping code you have is not the problem, it is just that the example code is horrible.
The data type issue is fairly obvious from the posted code and the documentation.
"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.

jada

Helllo i am back  ;)
well here i wrote the code as cr0sh told
Code: [Select]
///setting up pin no's ////


#include <Servo.h>            
#define ledpin_right 13                  
#define ledpin_left 12
#define motor_ONE_pin_ONE=3;          
#define motor_ONE_pin_TWO=4;          
#define motor_TWO_pin_ONE=5;          
#define motor_TWO_pin_TWO=6;
const int pingPin = 10;
int pos = 0 ;
Servo myservo;
long dist_right =0;
long dist_left = 0;
int state = 1;
long duration,cm;


/////setting pinmode//////


void setup()
{
 myservo.attach(9);
 Serial.begin(9600);                            
 pinMode(motor_ONE_pin_ONE,OUTPUT);    
 pinMode(motor_ONE_pin_TWO,OUTPUT);    
 pinMode(motor_TWO_pin_ONE,OUTPUT);    
 pinMode(motor_TWO_pin_TWO,OUTPUT);    
}


//////looping funtoinn//////


void loop()
{
 switch(state)
 {
   case 1:
   ping_read();
   drive_forward();
   if(cm < 10 )
   {
     state = 2;
   }
   break;
   case 2:
   ping_read_right();
   ping_read_left();
   state =3;
   break;
   case 3:
   if(distance_right > distance_left )
   {
     state =4;
   }
   else if (distance_right < distance_left )
   {
     state =5;
   }
   else if ((distance_right && distance_left ) < 10 )
   {
     state =6;
   }
   case 4:
   drive_right();
   break;
   case 5:
   drive_left();
   break;
   case 6:
   drive_360();
   break;
   default:
 }
}
   



//////ping functions///////

void ping_read()
{
 pinMode(pingPin, OUTPUT);            
 digitalWrite(pingPin, LOW);          
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);          
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);          
 pinMode(pingPin, INPUT);                    
 duration = pulseIn(pingPin, HIGH);          
 cm = microsecondsToCentimeters(duration);
 Serial.print(cm);        
 Serial.print("cm");      
 Serial.println();
}
void ping_read_left()
{
 myservo.write(10);
 dealy(150);
 pinMode(pingPin, OUTPUT);            
 digitalWrite(pingPin, LOW);          
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);          
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);          
 pinMode(pingPin, INPUT);                    
 duration = pulseIn(pingPin, HIGH);          
 distance_left = microsecondsToCentimeters(duration);
 Serial.print(distance_left);      
 Serial.print("cm");    
 Serial.println();
}
void ping_read_right()
{
 myservo.write(180);
 delay(150);
 pinMode(pingPin, OUTPUT);            
 digitalWrite(pingPin, LOW);          
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);          
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);          
 pinMode(pingPin, INPUT);                    
 duration = pulseIn(pingPin, HIGH);          
 distance_right = microsecondsToCentimeters(duration);
 Serial.print(distance_right);      
 Serial.print("cm");    
 Serial.println();
}
long microsecondsToCentimeters(long microseconds)
{
 return microseconds / 29 / 2;
}

///////drive functions//////


void drive_forward()
{
 digitalWrite(motor_ONE_pin_ONE,HIGH);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,HIGH);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}


void drive_stop()
{
 digitalWrite(motor_ONE_pin_ONE,LOW);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,LOW);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

void drive_right()
{
 digitalWrite(motor_ONE_pin_ONE,HIGH);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,LOW);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

void drive_left()
{
 digitalWrite(motor_ONE_pin_ONE,LOW);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,HIGH);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

void drive_360()
{
 digitalWrite(motor_ONE_pin_ONE,HIGH);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,LOW);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

and i get the following whole lot of errors
Quote
FINAL.cpp: In function 'void setup()':
FINAL:26: error: expected primary-expression before '=' token
FINAL:26: error: expected primary-expression before ',' token
FINAL:26: error: expected `;' before ')' token
FINAL:27: error: expected primary-expression before '=' token
FINAL:27: error: expected primary-expression before ',' token
FINAL:27: error: expected `;' before ')' token
FINAL:28: error: expected primary-expression before '=' token
FINAL:28: error: expected primary-expression before ',' token
FINAL:28: error: expected `;' before ')' token
FINAL:29: error: expected primary-expression before '=' token
FINAL:29: error: expected primary-expression before ',' token
FINAL:29: error: expected `;' before ')' token
FINAL.cpp: In function 'void loop()':
FINAL:54: error: 'distance_right' was not declared in this scope
FINAL:54: error: 'distance_left' was not declared in this scope
FINAL:76: error: expected primary-expression before '}' token
FINAL:76: error: expected `;' before '}' token
FINAL.cpp: In function 'void ping_read_left()':
FINAL:102: error: 'dealy' was not declared in this scope
FINAL:111: error: 'distance_left' was not declared in this scope
FINAL.cpp: In function 'void ping_read_right()':
FINAL:128: error: 'distance_right' was not declared in this scope
FINAL.cpp: In function 'void drive_forward()':
FINAL:143: error: expected primary-expression before '=' token
FINAL:143: error: expected primary-expression before ',' token
FINAL:143: error: expected `;' before ')' token
FINAL:144: error: expected primary-expression before '=' token
FINAL:144: error: expected primary-expression before ',' token
FINAL:144: error: expected `;' before ')' token
FINAL:145: error: expected primary-expression before '=' token
FINAL:145: error: expected primary-expression before ',' token
FINAL:145: error: expected `;' before ')' token
FINAL:146: error: expected primary-expression before '=' token
FINAL:146: error: expected primary-expression before ',' token
FINAL:146: error: expected `;' before ')' token
FINAL.cpp: In function 'void drive_stop()':
FINAL:153: error: expected primary-expression before '=' token
FINAL:153: error: expected primary-expression before ',' token
FINAL:153: error: expected `;' before ')' token
FINAL:154: error: expected primary-expression before '=' token
FINAL:154: error: expected primary-expression before ',' token
FINAL:154: error: expected `;' before ')' token
FINAL:155: error: expected primary-expression before '=' token
FINAL:155: error: expected primary-expression before ',' token
FINAL:155: error: expected `;' before ')' token
FINAL:156: error: expected primary-expression before '=' token
FINAL:156: error: expected primary-expression before ',' token
FINAL:156: error: expected `;' before ')' token
FINAL.cpp: In function 'void drive_right()':
FINAL:162: error: expected primary-expression before '=' token
FINAL:162: error: expected primary-expression before ',' token
FINAL:162: error: expected `;' before ')' token
FINAL:163: error: expected primary-expression before '=' token
FINAL:163: error: expected primary-expression before ',' token
FINAL:163: error: expected `;' before ')' token
FINAL:164: error: expected primary-expression before '=' token
FINAL:164: error: expected primary-expression before ',' token
FINAL:164: error: expected `;' before ')' token
FINAL:165: error: expected primary-expression before '=' token
FINAL:165: error: expected primary-expression before ',' token
FINAL:165: error: expected `;' before ')' token
FINAL.cpp: In function 'void drive_left()':
FINAL:171: error: expected primary-expression before '=' token
FINAL:171: error: expected primary-expression before ',' token
FINAL:171: error: expected `;' before ')' token
FINAL:172: error: expected primary-expression before '=' token
FINAL:172: error: expected primary-expression before ',' token
FINAL:172: error: expected `;' before ')' token
FINAL:173: error: expected primary-expression before '=' token
FINAL:173: error: expected primary-expression before ',' token
FINAL:173: error: expected `;' before ')' token
FINAL:174: error: expected primary-expression before '=' token
FINAL:174: error: expected primary-expression before ',' token
FINAL:174: error: expected `;' before ')' token
FINAL.cpp: In function 'void drive_360()':
FINAL:180: error: expected primary-expression before '=' token
FINAL:180: error: expected primary-expression before ',' token
FINAL:180: error: expected `;' before ')' token
FINAL:181: error: expected primary-expression before '=' token
FINAL:181: error: expected primary-expression before ',' token
FINAL:181: error: expected `;' before ')' token
FINAL:182: error: expected primary-expression before '=' token
FINAL:182: error: expected primary-expression before ',' token
FINAL:182: error: expected `;' before ')' token
FINAL:183: error: expected primary-expression before '=' token
FINAL:183: error: expected primary-expression before ',' token
FINAL:183: error: expected `;' before ')' token

:( could anyone help me out please  :)

bill2009

#19
Dec 02, 2010, 03:55 pm Last Edit: Dec 02, 2010, 04:06 pm by bill2009 Reason: 1
Code: [Select]
#define motor_ONE_pin_ONE=3;
#define motor_ONE_pin_TWO=4;
#define motor_TWO_pin_ONE=5;
#define motor_TWO_pin_TWO=6;

the format for # define is "#define symbol value" with no punctuation.
take the = and ;'s out of these and some of the flakier errors will disappear.  then you've got some simple errors (distance_right is declared as dist_right, delay is misspelled in one place).  

fix those, compile again and havnd try to figure out what the messages mean.  

I combined states in your code because they were just sequential steps.  

The test for left and right <10 still needs to be fixed.

Groove

Don't put semicolons on #defines unless you really know what you're doing
Per Arduino ad Astra

bill2009

Quote
Don't put semicolons on #defines unless you really know what you're doing


or equal signs?

Korman

#22
Dec 02, 2010, 04:17 pm Last Edit: Dec 02, 2010, 04:17 pm by Korman Reason: 1
Use const int or whatever instead of #define.

Korman

jada

i changed the code to..
Code: [Select]
///setting up pin no's ////


#include <Servo.h>            
#define ledpin_right 13                  
#define ledpin_left 12
#define motor_ONE_pin_ONE 3          
#define motor_ONE_pin_TWO 4    
#define motor_TWO_pin_ONE 5      
#define motor_TWO_pin_TWO 6
const int pingPin = 10;
int pos = 0 ;
Servo myservo;
int state = 1;
long duration,cm;
long distance_right = 0 ;
long distance_left = 0 ;


/////setting pinmode//////


void setup()
{
 myservo.attach(9);
 Serial.begin(9600);                            
 pinMode(motor_ONE_pin_ONE,OUTPUT);    
 pinMode(motor_ONE_pin_TWO,OUTPUT);    
 pinMode(motor_TWO_pin_ONE,OUTPUT);    
 pinMode(motor_TWO_pin_TWO,OUTPUT);    
}


//////looping funtoinn//////


void loop()
{
 switch(state)
 {
   case 1:
   ping_read();
   drive_forward();
   if(cm < 10 )
   {
     state = 2;
   }
   break;
   case 2:
   ping_read_right();
   ping_read_left();
   state =3;
   break;
   case 3:
   if(distance_right > distance_left )
   {
     state =4;
   }
   else if (distance_right < distance_left )
   {
     state =5;
   }
   else if ((distance_right && distance_left ) < 10 )
   {
     state =6;
   }
   
   else
   {
     state = 1 ;
   }
   case 4:
   drive_right();
   break;
   case 5:
   drive_left();
   break;
   case 6:
   drive_360();
   break;
   default:  ;
 }
}
   



//////ping functions///////

void ping_read()
{
 pinMode(pingPin, OUTPUT);            
 digitalWrite(pingPin, LOW);          
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);          
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);          
 pinMode(pingPin, INPUT);                    
 duration = pulseIn(pingPin, HIGH);          
 cm = microsecondsToCentimeters(duration);
 Serial.print(cm);        
 Serial.print("cm");      
 Serial.println();
}
void ping_read_left()
{
 myservo.write(10);
 delay(150);
 pinMode(pingPin, OUTPUT);            
 digitalWrite(pingPin, LOW);          
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);          
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);          
 pinMode(pingPin, INPUT);                    
 duration = pulseIn(pingPin, HIGH);          
 distance_left = microsecondsToCentimeters(duration);
 Serial.print(distance_left);      
 Serial.print("cm");    
 Serial.println();
}
void ping_read_right()
{
 myservo.write(180);
 delay(150);
 pinMode(pingPin, OUTPUT);            
 digitalWrite(pingPin, LOW);          
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);          
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);          
 pinMode(pingPin, INPUT);                    
 duration = pulseIn(pingPin, HIGH);          
 distance_right = microsecondsToCentimeters(duration);
 Serial.print(distance_right);      
 Serial.print("cm");    
 Serial.println();
}
long microsecondsToCentimeters(long microseconds)
{
 return microseconds / 29 / 2;
}

///////drive functions//////


void drive_forward()
{
 digitalWrite(motor_ONE_pin_ONE,HIGH);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,HIGH);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}


void drive_stop()
{
 digitalWrite(motor_ONE_pin_ONE,LOW);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,LOW);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

void drive_right()
{
 digitalWrite(motor_ONE_pin_ONE,HIGH);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,LOW);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

void drive_left()
{
 digitalWrite(motor_ONE_pin_ONE,LOW);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,HIGH);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

void drive_360()
{
 digitalWrite(motor_ONE_pin_ONE,HIGH);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,LOW);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

now i ahve noo errors BUT
even noiw the drive forward condition is not occuring when an obstacle is detedted the servo is set to 0 degrees and then the left motor is rotaing continiously  :(

jada

Quote
the format for # define is "#define symbol value" with no punctuation.

&&
Quote
Don't put semicolons on #defines unless you really know what you're doing

well i am really sorry ii didnt notice any equal to signs or colons any where i am really sorry for wasting your time  :-[
well now i have seen what i posted and i could see that it was clearly indicated as to wheere the error was but i didnt notice any equalto or colon anywhere  >:(

bill2009

Code: [Select]
((distance_right && distance_left ) < 10 )


you're going to drive me nuts til you fix that.

put serial prints at the top and bottom of the loop routine with the states and distances please.

jada

i changed it
Quote
put serial prints at the top and bottom of the loop routine with the states and distances please.
:-?

jada

Quote
put serial prints at the top and bottom of the loop routine with the states and distances please.

I guess this is what you meant
Code: [Select]
///setting up pin no's ////


#include <Servo.h>            
#define ledpin_right 13                  
#define ledpin_left 12
#define motor_ONE_pin_ONE 3          
#define motor_ONE_pin_TWO 4    
#define motor_TWO_pin_ONE 5      
#define motor_TWO_pin_TWO 6
const int pingPin = 10;
int pos = 0 ;
Servo myservo;
int state = 1;
long duration,cm;
long distance_right = 0 ;
long distance_left = 0 ;


/////setting pinmode//////


void setup()
{
 myservo.attach(9);
 Serial.begin(9600);                            
 pinMode(motor_ONE_pin_ONE,OUTPUT);    
 pinMode(motor_ONE_pin_TWO,OUTPUT);    
 pinMode(motor_TWO_pin_ONE,OUTPUT);    
 pinMode(motor_TWO_pin_TWO,OUTPUT);    
}


//////looping funtoinn//////


void loop()
{
 switch(state)
 {
   state = 1 ;
   Serial.print("in state 1 ");
    Serial.println();
   case 1:
   ping_read();
   drive_forward();
   if(cm < 10 )
   {
     state = 2;
     Serial.print("switching to state two");
     Serial.println();
   }
   break;
   case 2:
   ping_read_right();
   ping_read_left();
   state =3;
   Serial.print("in state three");
   Serial.println();
   break;
   case 3:
   if(distance_right > distance_left )
   {
     state =4;
     Serial.print("switching to state 4 ");
     Serial.println();
   }
   else if (distance_right < distance_left )
   {
     state =5;
     Serial.print("switching to state 5 ");
     Serial.println();
   }
   else if ( (distance_right<10) && (distance_left<10) )
   {
     state =6;
     Serial.print("switching to state 1 ");
     Serial.println();
   }
   
   else
   {
     state = 1 ;
     Serial.print("switching to state 1 ");
     Serial.println();
   }
   case 4:
   drive_right();
   break;
   case 5:
   drive_left();
   break;
   case 6:
   drive_360();
   break;
   default:  ;
 }
}
   



//////ping functions///////

void ping_read()
{
 pinMode(pingPin, OUTPUT);            
 digitalWrite(pingPin, LOW);          
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);          
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);          
 pinMode(pingPin, INPUT);                    
 duration = pulseIn(pingPin, HIGH);          
 cm = microsecondsToCentimeters(duration);
 Serial.print(cm);        
 Serial.print("cm");      
 Serial.println();
 return;
}
void ping_read_left()
{
 myservo.write(10);
 delay(150);
 pinMode(pingPin, OUTPUT);            
 digitalWrite(pingPin, LOW);          
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);          
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);          
 pinMode(pingPin, INPUT);                    
 duration = pulseIn(pingPin, HIGH);          
 distance_left = microsecondsToCentimeters(duration);
 Serial.print(distance_left);      
 Serial.print("cm");    
 Serial.println();
 return;
}
void ping_read_right()
{
 myservo.write(180);
 delay(150);
 pinMode(pingPin, OUTPUT);            
 digitalWrite(pingPin, LOW);          
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);          
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);          
 pinMode(pingPin, INPUT);                    
 duration = pulseIn(pingPin, HIGH);          
 distance_right = microsecondsToCentimeters(duration);
 Serial.print(distance_right);      
 Serial.print("cm");    
 Serial.println();
 return;
}
long microsecondsToCentimeters(long microseconds)
{
 return microseconds / 29 / 2;
}

///////drive functions//////


void drive_forward()
{
 digitalWrite(motor_ONE_pin_ONE,HIGH);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,HIGH);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}


void drive_stop()
{
 digitalWrite(motor_ONE_pin_ONE,LOW);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,LOW);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

void drive_right()
{
 digitalWrite(motor_ONE_pin_ONE,HIGH);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,LOW);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

void drive_left()
{
 digitalWrite(motor_ONE_pin_ONE,LOW);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,HIGH);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

void drive_360()
{
 digitalWrite(motor_ONE_pin_ONE,HIGH);
 digitalWrite(motor_TWO_pin_ONE,LOW);
 digitalWrite(motor_ONE_pin_TWO,LOW);
 digitalWrite(motor_TWO_pin_TWO,LOW);
 return;
}

even now the same thing occurs and this is what i get on the serial monitor after it put my hnad infront the ping
Code: [Select]
9cm
switching to state two
13cm
14cm
in state three
switching to state 5

bill2009

and is it now driving left?

jada

Quote
and is it now driving left?

Yes !
but the problem is the servo stays to the left position and i sillt cant see the drive forward occuring  :-?
i modified and
Quote
case 4:
   drive_right();
   state = 1 ;
   break;
   case 5:
   drive_left();
   state = 1;
   break;
   case 6:
   drive_360();
   state = 1;
   break;
   default:  ;

Bur still i cant see it drive forward

Go Up