Go Down

Topic: bonehead about servo sweep  running ard 0017 (Read 517 times) previous topic - next topic

CDM

Am trying to learn programming.  Have seen what others are doing and came up with this adaptation to move a bot and avoid objects.  I would like to sweep the head or "scan", and have come close but no cigar.  This code sweeps the head but very fast one way and slow the other; so only steps by 1, in one direction.  It only reads the sensor at the end of the sweep.  If I remove the scan code the bot works fine with the sensor looking ahead only and sees walls, etc.  What am I not seeing?  Thanks much, this is a great site.

Code: [Select]
   #include <Servo.h>

Servo left;
Servo right;
Servo head;


int pos = 0;
int headPos = 0;
int value = 0;
int treshold = 150;
int sensor = 0;

void setup() {
 Serial.begin  (9600);
 right.attach(10);
 left.attach(9);  
 head.attach(6);
}
void ReadSensor () {
 for(headPos = 0; headPos <180; headPos += 1)          
   head.write(pos);                        
   delay(10);                          
 for(headPos = 180; headPos >= 1; headPos-=1)  
   {                                    
   head.write(headPos);          
   delay(10);          
 value = analogRead(0);
 Serial.println(value);
 if (value > treshold) {
   turn();
 }
 else
 {
   move();
 }
}    
}
void turn () {
 left.write(145);
 right.write(145);
}
void move () {
 left.write(135);
 right.write(45);
}
       
void loop() {
 ReadSensor();
}  
 

AWOL

#1
Oct 17, 2009, 11:44 pm Last Edit: Oct 17, 2009, 11:50 pm by AWOL Reason: 1
Code: [Select]
for(headPos = 0; headPos <180; headPos += 1)          
   head.write(pos);                        
   delay(10);                          
 for(headPos = 180; headPos >= 1; headPos-=1)  
   {                                    
   head.write(headPos);          
   delay(10);          



I'd say that you're probably missing some "}" braces in there.
Or they're misplaced, which is more likely if this code is compiling

Code: [Select]
void ReadSensor () {
 for(headPos = 0; headPos <180; headPos += 1)  {
       head.write(pos);
       delay(10);
      // You may want to do stuff here
 }

 for(headPos = 180; headPos >= 1; headPos-=1) {
     head.write(headPos);          
     delay(10);          
     value = analogRead(0);
     Serial.println(value);
     if (value > treshold) {
        turn();
     } else {
        move();
     }
 }    
}

"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.

CDM

Thanks AWOL, think you are right.  I have played with moving the braces around and every iteration that will compile (quite a few) also acts differently.  Guess I have to find the right combo.  Thanks again.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy