Help using VarSpeedServo

Hi,

First off, HI! pretty new to this but eager to make some stuff. Maybe you can help me with my first project.

I'll be using the Arduino to move about 6 servos when a button is pushed/switch flicked to move components on a floor-standing lamp. So the lamp will turn on, things move to one position. Lamp is turned off, things move back to original position. Simple, right?

I'm butchering the "edge detection" code to sense when the button is pushed, and to move a single servo appropriately, see below. But I'm getting a large error when it is compiled, its as if the library isnt there, but it is. I'm assuming it's a simple mistake on my part.

/*
  /*
  State change detection (edge detection)
    
 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.
 
 This example shows how to detect when a button or button changes from off to on
 and on to off.
    
 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in LED on
   most Arduino boards)
 
 created  27 Sep 2005
 modified 30 Aug 2011
 by Tom Igoe

This example code is in the public domain.
    
 http://arduino.cc/en/Tutorial/ButtonStateChange
 
 */
#include <Servo.h>
// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
Servo myservo;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
  myservo.attach(9); //attach servo to pin 9
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

  
  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 2 == 0) {
 myservo.write(180);
}
else {
  myservo.write(0);
}
}

The error when compiling: C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:56:23: error: WProgram.h: No such file or directory
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In function 'void handle_interrupts(timer16_Sequence_t, volatile uint16_t*, volatile uint16_t*)':
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:91: error: 'LOW' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:91: error: 'digitalWrite' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:122: error: 'HIGH' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:122: error: 'digitalWrite' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:126: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: At global scope:
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:266: error: 'boolean' does not name a type
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In constructor 'VarSpeedServo::VarSpeedServo()':
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:283: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'uint8_t VarSpeedServo::attach(int, int, int)':
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:297: error: 'OUTPUT' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:297: error: 'pinMode' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:304: error: 'isTimerActive' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::detach()':
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:315: error: 'isTimerActive' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::write(int)':
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:326: error: 'map' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::writeMicroseconds(int)':
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:334: error: 'byte' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:334: error: expected ;' before 'channel' C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:335: error: 'channel' was not declared in this scope C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:343: error: 'clockCyclesPerMicrosecond' was not declared in this scope C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'void VarSpeedServo::slowmove(int, uint8_t)': C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:377: error: 'map' was not declared in this scope C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:380: error: 'byte' was not declared in this scope C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:380: error: expected ;' before 'channel'
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:381: error: 'channel' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:388: error: 'clockCyclesPerMicrosecond' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'int VarSpeedServo::read()':
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:407: error: 'map' was not declared in this scope
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp: In member function 'int VarSpeedServo::readMicroseconds()':
C:\Users\Chris\Programmes\arduino-1.0\libraries\VarSpeedServo\VarSpeedServo.cpp:414: error: 'clockCyclesPerMicrosecond' was not declared in this scope

Again, I'm hoping it's a simple fix as I'm very eager to get this finished, it's been in the making for literally months. Any help is much appreciated and I'll post pics, video etc here when I complete it.

Thanks :slight_smile:

Ok i've modified the code so that it now works as it should, but at full speed. how can i slow it down??

cderm87:
Ok i've modified the code so that it now works as it should, but at full speed. how can i slow it down??

The errors show you were trying to load the "VarSpeedServo" library but the sketch you provided loads the "Servo" library. Perhaps you should provide the sketch you are using.

If you want to use the VarSpeedServo library you should change "Wprogram.h" to "Arduino.h" to make it more compatible with Arduino 1.0.

Interesting, will try that.

The code I'm now using is much simpler, i got rid of stuff i dont need, and just need to slow the servos down now.

/*
  State change detection (edge detection)
    
 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.
 
 This example shows how to detect when a button or button changes from off to on
 and on to off.
    
 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in LED on
   most Arduino boards)
 
 created  27 Sep 2005
 modified 30 Aug 2011
 by Tom Igoe

This example code is in the public domain.
    
 http://arduino.cc/en/Tutorial/ButtonStateChange
 
 */
#include <Servo.h>
// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
Servo myservo;
int pos = 0;                //

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
  myservo.attach(9); //attach servo to pin 9
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH)
    for(pos = 0; pos < 179; 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(30);                       // waits 15ms for the servo to reach the position 
  } 
  if (buttonState == LOW) {
    myservo.write (0);
  }
}

Thanks a mill for the reply, am out at the mo and will try the change when i'm home. :slight_smile:

In VarSpeedServo.cpp change 'Wprogram.h' to 'Arduino.h'.

In your sketch change Servo.h and 'Servo myServo;' to VarSpeedServo.h and 'VarSpeedServo myServo;' respectively.

JohnWasser, you absolute Gent.

Have it working fully as I need it now, I really Appreciate it! That was annoying me for so long!

Below is the code, simplified, and annotated (apologies for not doing this earlier, was butchering a lot of code) Thought I'd put this up in case anyone else needs to do what I did.

/*

 Programme to move servo to one angle when switch is engaged,
 and back again when switch is disengaged, all at reduced speed

 This code samples code from: State change detection (edge detection)
 - Uses updated VarSpeedServo.h library
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * Servo attached to pin 9
 http://arduino.cc/en/Tutorial/ButtonStateChange
 
 */
#include <VarSpeedServo.h>
// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to

// Variables will change:
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
VarSpeedServo myservo;
int pos = 0;                //

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize serial communication:
  Serial.begin(9600);
  myservo.attach(9); //attach servo to pin 9
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH){      //if switch is off
      myservo.slowmove(180, 20);   //move servo to 180 at speed of 20
  } 
    if (buttonState == LOW) {      //if switch is on
      myservo.slowmove (0, 20);    //move servo to 0 at speed of 20
  }
}

John, I don't suppose you know why my servo ticks a bit, every few seconds. And it also can be a tad erratic when going through the movements when button is pushed, and released (mainly released). Do I need a debounce??

Thanks again though, your help is much appreciated, I am not worthy! :stuck_out_tongue: