I'm very new to Arduino, and forum posting, so please bear with me.
For the most part, watching and reading various tutorials has taught me a lot, but there is still a lot that I don't know.
I'm trying to make a kinetic sculpture that will sense a person/object nearby and turn itself away. I have an Uno, a standard servo from Radioshack and an ultrasonic range sensor (HC-SR04). I borrowed pieces of code for object-avoiding robots and tried to implement my own parameters. It will compile and upload to the board but nothing happens.
If anybody here could provide any advice, assistance or point me in the right direction I would appreciate it immensely.
Here is my code:
#include <Servo.h> //Include Servo Library
#include <NewPing.h> //Include NewPing library
Servo myServo;
#define SERVO_LEFT (180)
#define SERVO_CENT (90)
#define SERVO_RIGHT (0)
#define TRIG_PIN 12 // Trigger pin connected to
#define ECHO_PIN 11 //Echo pin connected to
#define MAX_DISTANCE 100 //Maximum distance for sensor to look
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
int time; //variable to store how long it takes ultrasonic wave to come back
int distance; //variable to store distance calculated from sensor
int trigDistance = 50; //distance in which servo will activate
int frontDistance; //variable to store distance in front of the sensor
int leftDistance; //varible to store distance after left turn
int rightDistance; //variable to store distance after left turn
void setup() {
// put your setup code here, to run once:
myServo.attach(9);
myServo.write(SERVO_CENT);
}
void loop() {
// put your main code here, to run repeatedly:
scan(); //retrieve distance
frontDistance = distance; //Set distance to front distance
if(frontDistance < trigDistance){
myServo.write (SERVO_LEFT);
delay (2000);
scan(); //retireve distance
leftDistance = distance; //left-facing distance
if(leftDistance < trigDistance){
myServo.write (SERVO_RIGHT);
delay (2000);
scan();
rightDistance = distance;
if(rightDistance < trigDistance){
myServo.write (SERVO_LEFT);
delay (2000);
}
else{
myServo.write (SERVO_RIGHT);
}
}
else{
myServo.write (SERVO_LEFT);
}
}
else{
myServo.write (SERVO_CENT);
}
}
void scan(){
time = sonar.ping();
distance = time / US_ROUNDTRIP_CM;
if(distance == 0){
distance = 100;
}
delay (10);
}
Thanks in advance,
mloney9