Author Topic: Help with Simpleservo library  (Read 800 times)

jbalat

  • Newbie
  • *
  • Posts: 5
Help with Simpleservo library
« on: March 14, 2019, 04:29:32 pm »
I am using the simpleservo library to drive a big servo with external power supply.
I noticed there is no refresh command like in the arduino servo library so when I drive the servo to its position sometimes I am able to push it back by hand.
I guess I can keep writing it’s current position, however wondering if there is a better way?

jbalat

  • Newbie
  • *
  • Posts: 5
Re: Help with Simpleservo library
« Reply #1 on: March 14, 2019, 11:39:49 pm »
Please see code below.
I am reading 2 push buttons using no resistors.
One button makes the servo go one way and the other button makes it go the other way.
I was thinking I can use this to electronically change gears on my bike??

Ideally I will want to learn these angles and store and read them from memory, but I need to get the servo to feedback when its in the wrong position and drive back to it.. I tried to use the .read command but it didnt work?
 
Code: [Select]
#include <SimpleServo.h>


SimpleServo servoLeft;          // Define left servo

#define ACTIVATED LOW
const int buttonPin = 2;     // the number of the pushbutton pin
const int buttonPin1 = 3;     // the number of the pushbutton pin 1
const int ledPin =  1;

// variables will change:
int buttonState = 0;          // variable for reading the pushbutton status
int buttonState1 = 0;          // variable for reading the pushbutton status
int a = 90;                       //rotation angle                     //rotation angle
int pos = 0;
int astep = 15;

void setup() {
  pinMode(ledPin, OUTPUT);
  servoLeft.attach(0);  // Set left servo to digital pin 10
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin,HIGH);
  pinMode(buttonPin1, INPUT);
  digitalWrite(buttonPin1,HIGH);
  servoLeft.write(a);
}

void loop() {            // Loop through motion tests
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState1 = digitalRead(buttonPin1);
  //servoLeft.write(a);
 
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == ACTIVATED) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    forward();             // Example: move forward
    delay(100);           // Wait 2000 milliseconds (2 seconds)
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
 
  if (buttonState1 == ACTIVATED) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    reverse();             // Example: move forward
    delay(100);           // Wait 2000 milliseconds (2 seconds)
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  } 
}


// Motion routines for forward, reverse, turns, and stop
void forward() {
   for(pos = a; pos <= a+astep; pos += 1)        // goes from 0 degrees to 180 degrees
  {                                         // in steps of 1 degree
    servoLeft.write(pos);                     // tell servo to go to position in variable 'pos'
    delay(5);                              // waits 15 milliseconds for the servo to reach the position
   // SoftwareServo::refresh();               // required by SoftwareServo Library to sweep correctly
  }           
  a +=astep;
}


void reverse() {
  for(pos = a; pos >= a-astep; pos -= 1)        // goes from 0 degrees to 180 degrees
  {                                         // in steps of 1 degree
    servoLeft.write(pos);                     // tell servo to go to position in variable 'pos'
    delay(5);                                 // waits 15 milliseconds for the servo to reach the position
  }
  a -=astep;
}


Adding this to the top of the loop works but its really noisy
Code: [Select]
void loop() {            // Loop through motion tests
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  buttonState1 = digitalRead(buttonPin1);
  servoLeft.write(a-1);
  servoLeft.write(a);
  delay(15);
« Last Edit: March 14, 2019, 11:54:27 pm by jbalat »

ninja1000

  • Newbie
  • *
  • Posts: 13
Re: Help with Simpleservo library
« Reply #2 on: October 07, 2019, 10:30:11 pm »
     slotxo       
Play to your heart's content. We have brought online slots games here for you to choose from,
so you can enjoy a wide variety of games in many styles, styles, and repetitions. The colorful
graphics within the game add to your excitement, with jackpot prizes and special bonuses that
are most often broken and you will not be lonely anymore because our website guarantees more
than 10,000 users per day.