1
$\begingroup$

I am using the clickbutton library from Arduous and am having some problems implementing it. As it stand now the code just runs the servo clockwise and I'm not sure what I did wrong. Basically I want the servo if pressed for a short period of time to move according to an exponential function, and if pressed according to a long period of time to move at a regular pace.

#include <Servo.h>
#include "ClickButton.h"

Servo myservo; // create servo object to control a servo

// CONSTANTS

// PINS
const int crServo = 12; // sets pin 12 as servo
const int buttonPinCW = 2; // sets pin 2 as button; CW => clockwise => FOCUS FAR
const int buttonPinCC = 3; // sets pin 3 as button; CC => counterclockwise => FOCUS NEAR
const int ledPinB = 4; // sets pin 10 as LED
const int ledPinG = 5; // sets pin 10 as LED
const int ledPinR = 6; // sets pin 10 as LED

const int t = 1;  // slow down

// SERVO PROPERTIES
const int crSpeedDefault = 100; // is the stay still position, motor should not turn
const int crSpeedCW = 107; // turns the motor full speed clockwise
const int crSpeedCC = 87; // turns the motor full speed counter-clockwise

// SET BUTTON STATES
ClickButton buttonCW(buttonPinCW, LOW);
ClickButton buttonCC(buttonPinCC, LOW);

void setup()
{
  myservo.attach(crServo); // attaches the servo on pin 12 to the servo object
  //pinMode (buttonPinCW, INPUT); // sets button as input
  //pinMode (buttonPinCC, INPUT); // sets button as input
  pinMode (ledPinB, OUTPUT); // sets led as output
  pinMode (ledPinG, OUTPUT); // sets led as output
  pinMode (ledPinR, OUTPUT); // sets led as output
  myservo.write(crSpeedDefault); // default servo to crSpeedDefault
  startup();
}

int startup() {
  //blinker(2, ledPinB);
  //blinker(1, ledPinG);
  //blinker(1, ledPinR);
}

void blinker(int count, int pin) {
  for (int x = 0; x < count; x++)
  {
    digitalWrite(pin, HIGH);
    delay(1000);
    digitalWrite(pin, LOW);
    delay(1000);
  }
}

void loop()
{
  buttonCW.Update();
  buttonCC.Update();
  int buttonCWClicks = 0;
  int buttonCCClicks = 0;
  if (buttonCW.clicks != 0) buttonCWClicks = buttonCW.clicks;
  if (buttonCC.clicks != 0) buttonCCClicks = buttonCC.clicks;
  //buttonStateCW = digitalRead(buttonPinCW);
  //buttonStateCC = digitalRead(buttonPinCC);
  // clockwise rotation
  if (buttonCW.depressed == true) {
    if (buttonCWClicks == 1) {
      digitalWrite(ledPinR, HIGH);
      float speed = crSpeedCW;
      Serial.print("CLOCKWISE-ROTATION \n");
      for (int i = 0; i < t * 5; i++) {
        speed += ((float)crSpeedDefault - speed)/ 10;
        Serial.print(speed);
        Serial.print("\n");
        myservo.write((int)speed);
        delay(100);
      }
      myservo.write(crSpeedCW);
    } 
    else if (buttonCWClicks == -1) {
      digitalWrite(ledPinR, HIGH);
      myservo.write(crSpeedCW);
    }
  } 
  else if (buttonCC.depressed == true) {
    if (buttonCCClicks == 1) {
      digitalWrite(ledPinG, HIGH);
      float speed = crSpeedCC;
      Serial.print("COUNTER-CLOCKWISE-ROTATION \n");
      for (int i = 0; i < t * 5; i++) {
        speed += ((float)crSpeedDefault - speed) / 10;
        Serial.print(speed);
        Serial.print("\n");
        myservo.write((int)speed);
        delay(100);
      }
      myservo.write(crSpeedCC);
    } 
    else if (buttonCCClicks == -1) {
      digitalWrite(ledPinR, HIGH);
      myservo.write(crSpeedCW);
    }
  } 
  else {
    myservo.write(crSpeedDefault);
    digitalWrite(ledPinR, LOW); 
    digitalWrite(ledPinG, LOW);     // turn the LED off by making the voltage LOW
  }
}
$\endgroup$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.