I'm new to the Arduino programming world. I'm just trying to get my ultrasonic sensor to activate the servo. So I pieced together 2 sketches, but when I uncomment these 2 lines I get errors that make no sense. "Initializer expected before void" Not very helpful.
int cm;
cm = microsecondsToCentimeters(duration);
// Program for using continuous rotation servos
#include <Servo.h> //this adds code to our program that defines what a Servo object is and can do
Servo amotor; // create a servo object to control a servo
int mspeed = 1500; // variable to store the motor speed
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0;
const int pingPin = 13;
const int greenLed = 7;
//pin which delivers time to receive echo using pulseIn()
int inPin = 12;
//range in cm which is considered safe to enter, anything
//coming within less than 5 cm triggers red LED
int safeZone = 5;
//int cm;
void setup() {
amotor.attach(9); // attaches the servo on pin 10 to the servo object amotor
pinMode(buttonPin, INPUT);
pinMode(greenLed, OUTPUT);
Serial.begin(9600);
}
void loop(){
//raw duration in milliseconds, cm is the
//converted amount into a distance
long duration, cm;
pinMode(pingPin, OUTPUT);
//sending the signal, starting with LOW for a clean signal
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//setting up the input pin, and receiving the duration in
//microseconds for the sound to bounce off the object infront
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
// convert the time into a distance
// cm = microsecondsToCentimeters(duration);
//printing the current readings to ther serial display
Serial.print(cm);
Serial.print("cm");
Serial.println();
//checking if anything is within the safezone, if not, keep
//green LED on if safezone violated, activate red LED instead
if (cm > safeZone)
{
digitalWrite(greenLed, HIGH);
mspeed = 1300; //set position variable
amotor.writeMicroseconds(mspeed); // tell servo to move as indicated by variable 'mspeed'
delay(1000); //time for the servo to move
} else {
mspeed = 1500; //set position variable
amotor.writeMicroseconds(mspeed); // tell servo to move as indicated by variable 'mspeed'
delay(1000); //time for the servo to move
}
}/code]