[solved]Servo program won't compile

hi there folks,

i’m trying to make a device for a Maker Faire, i got it nearly completed but my program has one mistake that has to do with the attaching an writing of the servo. It’s supposed to point at the strongest lightsource through two LDRs, by turning the servo. down below is the code as i got it. the problem is probably quite obvious but I can’t seem to figure it out. Could anyone take a look at it and explain to me what i’m doing wrong?

#include <Servo.h>
#define servo 9
#define LeftSensor 0
#define RightSensor 1
#define Button 2
#define led 13

Servo servo;

int Speed = 50;
int LeftSensorVal = 0;
int RightSensorVal = 0;
int SensorThreshold = 200;
int delayParam = 10;
int ButtonVal = 0;
int old_ButtonVal = 0;
int ButtonState = 0;

void setup()
{
  Serial.begin(9600);
  servoSetup();
}

void loop()
{
  LeftSensorVal = analogRead(LeftSensor);
  RightSensorVal = analogRead(RightSensor);
  ButtonVal = digitalRead(Button);
  
  if((ButtonVal == HIGH) && (old_ButtonVal == LOW))
  {
    Serial.println("Button Pushed");
    ButtonState = 1 - ButtonState;
    delay(10);
  }
  old_ButtonVal = ButtonVal;
  
  if(ButtonState == 1)
  {
    digitalWrite(led, HIGH);
    
    if(LeftSensorVal - RightSensorVal >= SensorThreshold)
    {
      Serial.println("left");
      goLeft();
      delay(delayParam * 100);
      goStop();
    }
    else if(RightSensorVal - LeftSensorVal >= SensorThreshold)
    {
      Serial.println("right");
      goRight();
      delay(delayParam * 100);
      goStop();
    }
  }
  else
  {
    Serial.println("stop");
    digitalWrite(led, LOW);
    goStop();
  }
}

void servoSetup()
{
  setSpeed(Speed);
  pinMode(servo, OUTPUT);
  servo.attach(servo);
  pinMode(Button, INPUT);
  pinMode(led, OUTPUT);
  goStop();
}

void setSpeed(int newSpeed)
{
  setSpeedServo(newSpeed);
}

void setSpeedServo(int newSpeed)
{
  if(newSpeed >= 100) {newSpeed = 100;}
  if(newSpeed <= 0) {newSpeed = 0;}
  Speed = newSpeed * 0.9;
}

void goRight()
{
  servo.write(90 + Speed);
}

void goLeft()
{
  servo.write(90 - Speed);
}

void goStop()
{
  servo.write(90);
}

It constantly gives the following error:

sketch_may25a.ino:2:15: error: expected unqualified-id before numeric constant
sketch_may25a.ino:8:7: note: in expansion of macro ‘servo’
sketch_may25a.ino: In function ‘void servoSetup()’:
sketch_may25a.ino:70:9: error: request for member ‘attach’ in ‘9’, which is of non-class type ‘int’
sketch_may25a.ino: In function ‘void goRight()’:
sketch_may25a.ino:90:9: error: request for member ‘write’ in ‘9’, which is of non-class type ‘int’
sketch_may25a.ino: In function ‘void goLeft()’:
sketch_may25a.ino:95:9: error: request for member ‘write’ in ‘9’, which is of non-class type ‘int’
sketch_may25a.ino: In function ‘void goStop()’:
sketch_may25a.ino:100:9: error: request for member ‘write’ in ‘9’, which is of non-class type ‘int’

thanks to those who have an answer.

Hi Lenny

#define servo 9
...
Servo servo;

You’ve confused the poor program :slight_smile:

The first “servo” represents the pin number. The second servo is the name of your instance of the Servo object. One of the "servo"s needs to change. Probably quicker to change the former, and remember to change it also wherever the pin number appears as a parameter to a function call. For example …

#define servoPin 9

Regards

Ray

Your thread title is complete nonsense. You have a coding issue. There is nothing wrong with the Arduino's ability to write to or attach a servo IF you wrote parsable code. Your title states that there is something wrong with how the Arduino is dealing with the code. There isn't, since there is no code on the Arduino.

To PaulS: could you please not be such a dick, and think how i got to it? I didn't know what to put in the title so i used what i read in the error log. so give me a n example of what i should have used and i'll change it.

Regards Lennydune

I didn't know what to put in the title

So, somehow it's my fault that you are an idiot? I refuse to accept that.

So, somehow it's my fault that you are an idiot? I refuse to accept that.

You're on shaky ground, PaulS, in my opinion.

Thread title "servo not being written and/or attached" was neutral and did not say the Arduino was at fault.

First line of Lenny's original post:

my program has one mistake

Fourth line of original post:

explain to me what i'm doing wrong

Lenny seems to have been pretty open from the start that his code was at fault.

Lenny seems to have been pretty open from the start that his code was at fault.

There is a huge difference between the code not doing what the author thinks it should do (which is what the title implies) and code that won't even compile (the reality).

The post title should reflect the real issue.

well as I said, gimme a better option for a title or STFU.

I'm with PaulS - the title suggests that the servo isn't being written or attached. If the code doesn't compile, it doesn't run, so saying that the servo isn't being written or attached is a nonsense.

Mind your language, and cut out the personal insults.

okay then, does anyone have suggestions for a better title, because I can't come up with one myself. That's the entire problem. I will change it at once.

How about "Servo program won't compile"?

Hackscribble: How about "Servo program won't compile"?

Psst, reply #8

okay, changed it. Are we all good?