Author Topic: Digispark Servo & Button Combination  (Read 2681 times)

joshtubb

  • Newbie
  • *
  • Posts: 1
Digispark Servo & Button Combination
« on: March 08, 2016, 02:40:27 pm »
I created a string of code for my arduino uno which allows me to rotate my servo by pushing one of two buttons. I can push either of the buttons to rotate it either CW or CCW. I wanted to be able to use this same code on the digispark, but I cannot work out how to get it to work. I believe this is because it does not recognise the servo.h library. What can I do to make this work?

The code is as follows:
Code: [Select]
#include <Servo.h>
 
const int servoPin = 12;
const int buttonA = 9;
const int buttonB = 8;

int buttonState = 0;
int directionState = 0;
 
Servo servo; 
 
int angle = 0;   // servo position in degrees
 
void setup()
{
  servo.attach(servoPin);
  pinMode(servoPin, OUTPUT);
  pinMode(buttonA, INPUT_PULLUP);
  pinMode(buttonB, INPUT_PULLUP);
}
 

void loop()
{
  buttonState = digitalRead(buttonA);
 
  if (directionState == 0)
  {
    if (buttonState == LOW)
    {
      directionState = 1;
      for(angle = 0; angle < 180; angle++)
      { 
        servo.write(angle);               
        delay(1);                   
      }
    }
  }
  else if (directionState == 1)
  {
    if (buttonState == LOW)
    {
      directionState = 0;
      for(angle = 180; angle > 0; angle--)
      { 
        servo.write(angle);               
        delay(1);                   
      }
    }
  }
  buttonState = digitalRead(buttonB);
 
  if (directionState == 0)
  {
    if (buttonState == LOW)
    {
      directionState = 1;
      for(angle = 0; angle < 180; angle++)
      { 
        servo.write(angle);               
        delay(1);                   
      }
    }
  }
  else if (directionState == 1)
  {
    if (buttonState == LOW)
    {
      directionState = 0;
      for(angle = 180; angle > 0; angle--)
      { 
        servo.write(angle);               
        delay(1);                   
      }
    }
  }   
}

What I have tried is to replace
Code: [Select]
#include <Servo.h>
 
const int servoPin = 12;
const int buttonA = 9;
const int buttonB = 8;

int buttonState = 0;
int directionState = 0;
 
Servo servo;
with
Code: [Select]
#include <SoftwareServo.h>

SoftwareServo servo;


const int servoPin = 1;
const int buttonA = 2;
const int buttonB = 3;

int buttonState = 0;
int directionState = 0;

Please help, I am quite new at arduino.