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