Having a programing error

Hi, Lately I am trying to write code for a servo (w/i) the servo library. But kept getting an error.

My code:
#include <Servo.h>

void setup() {
// put your setup code here, to run once:
Servo servo.attach(10);
}

void loop() {
// put your main code here, to run repeatedly:
Servo digitalWrite(HIGH);
delayMicroseconds (1500);
Servo digitalWrite (Low);
}

Error messages:

Arduino: 1.8.15 (Windows 10), Board: "Arduino Uno"

expected initializer before '.' token

Did you define and instantiate a servo object like

Servo myServo1:?

What does this mean?

Have you looked at a servo example to see how your code differs?

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

You have not written code.

You have typed a bunch of random stuff and expect the computer to understand it.

a Servo object (variable) needs to be defined before it is used. it needs to be configured (attached) for a specific pin (i.e. 10)

Servo servo;
void setup() {
    servo.attach(10);
}

an RC servo is controlled by a pulse from 1-2 msec which corresponds to an angle. it's not a binary value on/off, HIGH/LOW. the write() function will generate the pulse for a specific angle
consdier

void loop()
{
    servo.write (10);
    delay (1000);     // 1 sec

    servo.write (100);
    delay (1000);
}

Find an example code, study it line by line so when you write your own code it doesn’t look like C++ but is C++

It looks like you intended to write either:

#include <Servo.h>

const byte ServoPin = 10;

Servo servo;

void setup()
{
  // put your setup code here, to run once:
  servo.attach(ServoPin);
}

void loop()
{
  // put your main code here, to run repeatedly:
  servo.writeMicroseconds(1500); 
}

or

const byte ServoPin = 10;

void setup()
{
  // put your setup code here, to run once:
  pinMode(ServoPin, OUTPUT);
}

void loop()
{
  // put your main code here, to run repeatedly:
  digitalWrite(ServoPin, HIGH)
  delayMicroseconds(1500);
  digitalWrite(ServoPin, LOW);
  delay(20);  // Keep the repeat rate near 50 Hz
}

Either will move the servo to a middle position and keep it there forever. Once you assign a pin to the Servo library you should not write to it directly.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.