Hyderabad , India
Offline
God Member
Karma: 5
Posts: 621
can't help not to think arduinaizing something !
|
 |
« Reply #15 on: December 01, 2010, 09:26:06 pm » |
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 ? :-? )
|
|
|
|
|
Logged
|
|
|
|
|
Hyderabad , India
Offline
God Member
Karma: 5
Posts: 621
can't help not to think arduinaizing something !
|
 |
« Reply #16 on: December 01, 2010, 09:28:44 pm » |
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 : 
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 143
Posts: 19380
I don't think you connected the grounds, Dave.
|
 |
« Reply #17 on: December 02, 2010, 05:31:34 am » |
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.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Hyderabad , India
Offline
God Member
Karma: 5
Posts: 621
can't help not to think arduinaizing something !
|
 |
« Reply #18 on: December 02, 2010, 09:34:21 am » |
Helllo i am back  well here i wrote the code as cr0sh told ///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 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 
|
|
|
|
|
Logged
|
|
|
|
|
ottawa, canada
Offline
God Member
Karma: 4
Posts: 973
Arduino rocks
|
 |
« Reply #19 on: December 02, 2010, 09:55:41 am » |
#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.
|
|
|
|
« Last Edit: December 02, 2010, 10:06:43 am by bill2009 »
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #20 on: December 02, 2010, 09:56:07 am » |
Don't put semicolons on #defines unless you really know what you're doing
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
ottawa, canada
Offline
God Member
Karma: 4
Posts: 973
Arduino rocks
|
 |
« Reply #21 on: December 02, 2010, 10:12:39 am » |
Don't put semicolons on #defines unless you really know what you're doing or equal signs?
|
|
|
|
|
Logged
|
|
|
|
|
Central Europe
Offline
Edison Member
Karma: 5
Posts: 1220
Use the Source, Luke.
|
 |
« Reply #22 on: December 02, 2010, 10:17:18 am » |
Use const int or whatever instead of #define.
Korman
|
|
|
|
« Last Edit: December 02, 2010, 10:17:36 am by Korman »
|
Logged
|
|
|
|
|
Hyderabad , India
Offline
God Member
Karma: 5
Posts: 621
can't help not to think arduinaizing something !
|
 |
« Reply #23 on: December 02, 2010, 10:47:09 am » |
i changed the code to.. ///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 
|
|
|
|
|
Logged
|
|
|
|
|
Hyderabad , India
Offline
God Member
Karma: 5
Posts: 621
can't help not to think arduinaizing something !
|
 |
« Reply #24 on: December 02, 2010, 10:51:05 am » |
the format for # define is "#define symbol value" with no punctuation.
&& 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 > 
|
|
|
|
|
Logged
|
|
|
|
|
ottawa, canada
Offline
God Member
Karma: 4
Posts: 973
Arduino rocks
|
 |
« Reply #25 on: December 02, 2010, 10:53:38 am » |
((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.
|
|
|
|
|
Logged
|
|
|
|
|
Hyderabad , India
Offline
God Member
Karma: 5
Posts: 621
can't help not to think arduinaizing something !
|
 |
« Reply #26 on: December 02, 2010, 10:57:08 am » |
i changed it put serial prints at the top and bottom of the loop routine with the states and distances please. :-?
|
|
|
|
|
Logged
|
|
|
|
|
Hyderabad , India
Offline
God Member
Karma: 5
Posts: 621
can't help not to think arduinaizing something !
|
 |
« Reply #27 on: December 02, 2010, 11:46:36 am » |
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 ///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 9cm switching to state two 13cm 14cm in state three switching to state 5
|
|
|
|
|
Logged
|
|
|
|
|
ottawa, canada
Offline
God Member
Karma: 4
Posts: 973
Arduino rocks
|
 |
« Reply #28 on: December 02, 2010, 12:02:53 pm » |
and is it now driving left?
|
|
|
|
|
Logged
|
|
|
|
|
Hyderabad , India
Offline
God Member
Karma: 5
Posts: 621
can't help not to think arduinaizing something !
|
 |
« Reply #29 on: December 02, 2010, 12:08:08 pm » |
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 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
|
|
|
|
|
Logged
|
|
|
|
|
|