n00b problem: code uploads but doesn't work

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

The first thing to do is to put some Serial.print()s into you program so that you can see the value of relevant variables such as distance. Is it what you expect ?

Have you checked your wiring ?
Does the servo move when you run the Servo Sweep example ?
How is the servo powered ? "From the Arduino" is the wrong answer by the way.

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom.... :slight_smile:

UKHeliBob:
The first thing to do is to put some Serial.print()s into you program so that you can see the value of relevant variables such as distance. Is it what you expect ?

Have you checked your wiring ?
Does the servo move when you run the Servo Sweep example ?
How is the servo powered ? "From the Arduino" is the wrong answer by the way.

I checked my wiring, and I have gotten the servo to sweep. I was using a protoshield with a tiny breadboard to place the sensor, but upon further inspection, it looks like a wire broke off in one of the holes. I don't know if that messes with the functionality or not, but I moved the sensor to another breadboard to be safe. I powered the sensor (and servo) through the breadboard before and it would run other programs. The servo is powered on 4 1.5v AAA batteries.

TomGeorge:
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom.... :slight_smile:

Sure, here's a picture I drew in a notebook. I hope it's legible.

You can't just have one wire going to the servo. It has to share a ground connection with the rest of the circuitry. Circuit also means "circle" :slight_smile:

aarg:
You can't just have one wire going to the servo. It has to share a ground connection with the rest of the circuitry. Circuit also means "circle" :slight_smile:

....Yes, I suppose that would help
(talk about embarassing)

Thanks for all your help, everybody! I got everything working the way I wanted and I owe it all to you guys. Thanks a bunch!!