Not just in this code, but for other things like it in the future.
Thanks!
servocontrol.ino (1.62 KB)
Not just in this code, but for other things like it in the future.
Thanks!
servocontrol.ino (1.62 KB)
void setup() {
Servo myservo; // create servo object to control a servo
void Servo(byte second, // 0-59
byte minute, // 0-59
byte hour, // 0-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
const int trigPin = 2;
const int echoPin = 4;
void setup() {
// initialize serial communication:
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
// and the distance result in centimeters:
long duration, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(20);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
// the condition for the distance
if ( cm > 7 && cm < 14)
{
myservo.write(140); // sets the servo position according to the scaled value
delay(4000);
}
else if ( cm < 8)
{
myservo.write(40); // sets the servo position according to the scaled value
delay(100);
}
else
{
myservo.write(40); // sets the servo position according to the scaled value
delay(100);
}
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
It means the Servo object in setup isn't visible in loop.
Or in any other function.
Also, one setup function only.
I have no idea what the void Servo function is supposed to be, but the compiler won't like it.
Time for a complete rewrite.
"scope" is a concept that you need to be familiar with. It refers to the "area" of the program in which a variable is accessible.
Global variables (defined outside any function, and usually defined before setup() ) are accessible everywhere in a program.
A variable defined inside a function (such as your myservo which you have defined inside the setup() function) is only accessible within the function.
You should be aware that C/C++ is case-sensitve. Servo and servo are two different things.
You should also be aware that every variable and function must have a unique name. You seem to be trying to create a function called Servo which is the name already used by the Servo.h library. However that error has not appeared because you have not included the library.
If you want to use an external library (such as the Servo library) you must include it with a line like this before the setup() function.
#include <Servo.h>
...R