Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library problem #5

Open
DaveCalaway opened this issue Jan 10, 2016 · 2 comments
Open

Library problem #5

DaveCalaway opened this issue Jan 10, 2016 · 2 comments

Comments

@DaveCalaway
Copy link

Hi guys, i have tried to load this library with this support: https://goo.gl/Viu6aQ
I edited the example in this way:

#include "Servo8Bit.h"

void mymydelay(uint16_t milliseconds);  //forward declaration to the mydelay function
Servo8Bit myServo;
void setup() {
  myServo.attach(1);

}

int test()
{
    myServo.write(0);   //rotate to the 0 degree position
    mydelay(2000);        //wait 2 seconds

    myServo.write(180); //rotate to the 180 degree position
    mydelay(2000);        //wait 2 seconds

    myServo.write(90);  //rotate to the center (90 degree) position
    mydelay(2000);        //wait 2 seconds


    //sweep the servo
    while(1)
    {
        for(int pos = 0; pos < 180; pos++)  // goes from 0 degrees to 180 degrees
        {                                   // in steps of 1 degree
            myServo.write(pos);             // tell servo to go to position in variable 'pos'
            mydelay(15);                      // waits 15ms for the servo to reach the position
        }

        for(int pos = 180; pos > 1; pos--)  // goes from 180 degrees to 0 degrees
        {
            myServo.write(pos);             // tell servo to go to position in variable 'pos'
            mydelay(15);                      // waits 15ms for the servo to reach the position
        }
    }
}

void mydelayMicroseconds(uint16_t us)
{
#if F_CPU >= 16000000L
    // for the 16 MHz clock on most Arduino boards

    // for a one-microsecond mydelay, simply return.  the overhead
    // of the function call yields a mydelay of approximately 1 1/8 us.
    if (--us == 0)
        return;

    // the following loop takes a quarter of a microsecond (4 cycles)
    // per iteration, so execute it four times for each microsecond of
    // mydelay requested.
    us <<= 2;

    // account for the time taken in the preceeding commands.
    us -= 2;
#else
    // for the 8 MHz internal clock on the ATmega168

    // for a one- or two-microsecond mydelay, simply return.  the overhead of
    // the function calls takes more than two microseconds.  can't just
    // subtract two, since us is unsigned; we'd overflow.
    if (--us == 0)
        return;
    if (--us == 0)
        return;

    // the following loop takes half of a microsecond (4 cycles)
    // per iteration, so execute it twice for each microsecond of
    // mydelay requested.
    us <<= 1;

    // partially compensate for the time taken by the preceeding commands.
    // we can't subtract any more than this or we'd overflow w/ small mydelays.
    us--;
#endif

    // busy wait
    __asm__ __volatile__ (
        "1: sbiw %0,1" "\n\t" // 2 cycles
        "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
    );
}//end mydelayMicroseconds


void mydelay(uint16_t milliseconds)
{
    for(uint16_t i = 0; i < milliseconds; i++)
    {
        mydelayMicroseconds(1000);
    }
}//end mydelay

void loop() {
  test();
  }

When i try it, the servo motor still not work.

What did I do wrong?

Thx Dave.

@maltevonehren
Copy link

I found the Issue was a simple c++ problem in the Servo8Bit constructor. For some reason after the constructor myServoNumber is 0 instead of invalidServoNumber. This results in the register function not being called and nothing happening. Changing the default for myServoNumber to ServoSequencer::kInvalidServoIndex instead of invalidServoNumber fixes the problem.

@FedericoBusero
Copy link
Contributor

I created a pull request: #9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants