Pages: [1]   Go Down
Author Topic: bonehead about servo sweep  running ard 0017  (Read 434 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   #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();
}  
  
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25802
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
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();
      }
  }    
}
« Last Edit: October 17, 2009, 04:50:23 pm 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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: