Go Down

Topic: Attiny85 and servo (Read 2881 times) previous topic - next topic

WireJunky

I am at a breaking point here.
I have the attiny85 and cunning turtle (servo8bit) library.

Code: [Select]
#include <Servo8Bit.h>













Servo8Bit myServo;  //create a servo object.


void setup()
{



  myServo.attach(0); 
  delay(1);


}

void loop()
{
  myServo.writeMicroseconds(1500);
  delay(1000);
}



There's the code. All it's supposed to do is start up and make the continuous rotation servo spin. It doesn't spin. It doesn't do anything. I'm out of patience. i've been waiting a few months now for an updated servo library to surface but nothing has come up. I need help here.

nicoverduin

Have you looked at the example he used in his GITHUB library? He has written his own delay functions to avoid conflicts with timer0.
see: https://github.com/fri000/Servo8Bit/blob/master/example.cpp

I suspect if you add the delay functions at the end of your code and the forward declaration at the top it might actually work.
Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

WireJunky


Have you looked at the example he used in his GITHUB library? He has written his own delay functions to avoid conflicts with timer0.
see: https://github.com/fri000/Servo8Bit/blob/master/example.cpp

I suspect if you add the delay functions at the end of your code and the forward declaration at the top it might actually work.


Code: [Select]
// Servo8Bit Example code
// Ilya Brutman

#include "Servo8Bit.h"
void delay(uint16_t milliseconds);  //forward declaration to the delay function

Servo8Bit myServo;  //create a servo object.
int led = 0;

void setup()
{
 
                       

    myServo.attach(0);  //attach the servo to pin PB3
    delay(1);

   
}
   
   void loop()
    {
        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'
            delay(1);                      // 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'
            delay(1);                      // waits 15ms for the servo to reach the position
        }
    }
void delay(uint16_t milliseconds)
{
    for(uint16_t i = 0; i < milliseconds; i++)
    {
        delayMicroseconds(1000);
    }
}//end delay


Like this? I try this and I get an error saying "call of overloaded 'delay(int)' is ambiguous".

nickgammon

Well, call it myDelay or some different name.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

nicoverduin

And while you aare at it, may as well add his function of delayMicroSeconds also:
Code: [Select]

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

    // for a one-microsecond delay, simply return. the overhead
    // of the function call yields a delay 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
    // delay 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 delay, 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
    // delay 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 delays.
    us--;
#endif

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


Met vriendelijke groet / kindest regards
Nico Verduin
www.verelec.nl
Do not PM me for personal consultancy unless you are willing to pay for it.

nickgammon

And what's with














all the blank lines in your code?









It doesn't make it any more readable.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

WireJunky


Well, call it myDelay or some different name.


Code: [Select]
// Servo8Bit Example code
// Ilya Brutman

#include "Servo8Bit.h"
void mydelay(uint16_t milliseconds);  //forward declaration to the delay function

Servo8Bit myServo;  //create a servo object.
int led = 0;

void setup()
{
  myServo.attach(0);  //attach the servo to pin PB3
    delay(1);
}
   void loop()
    {
        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'
            delay(1);                      // 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'
            delay(1);                      // waits 15ms for the servo to reach the position
        }
    }
void mydelay(uint16_t milliseconds)
{
    for(uint16_t i = 0; i < milliseconds; i++)
    {
        delayMicroseconds(1000);
    }
}//end delay


I put it as mydelay, but still no luck.
By the way, should I be using the attiny on a 1mhz or 8 mhz clock?

ky9129

Does it works with another arduino? Double check connections.
However, I have heard that attiny is not fully compatible with arduino - few libraries/ functions might not work

WireJunky


Does it works with another arduino? Double check connections.
However, I have heard that attiny is not fully compatible with arduino - few libraries/ functions might not work


The connections are simple.
http://farm7.static.flickr.com/6065/6121376853_e8355afe33.jpg
The servo is hooked into the proper pins on the programmer.
For some reason though, the servo spins on pin 1 of the attiny, but that's all it does. Spin.

Code: [Select]
// Servo8Bit Example code
// Ilya Brutman

#include "Servo8Bit.h"
void mydelay(uint16_t milliseconds);  //forward declaration to the delay function

Servo8Bit myServo;  //create a servo object.
int led = 0;

void setup()
{
  myServo.attach(0);  //attach the servo to pin PB3
    delay(1);
}
   void loop()
    {
        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'
            delay(1);                      // 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'
            delay(1);                      // waits 15ms for the servo to reach the position
        }
    }
void mydelay(uint16_t milliseconds)
{
    for(uint16_t i = 0; i < milliseconds; i++)
    {
        delayMicroseconds(1000);
    }
}//end delay




How hard is it to write a library? I think it may be quicker for me to write one instead of waiting for cunningturtle to update his.

nickgammon



Well, call it myDelay or some different name.


Code: [Select]
// Servo8Bit Example code
// Ilya Brutman

#include "Servo8Bit.h"
void mydelay(uint16_t milliseconds);  //forward declaration to the delay function

Servo8Bit myServo;  //create a servo object.
int led = 0;

void setup()
{
  myServo.attach(0);  //attach the servo to pin PB3
    delay(1);   // <---------- mydelay (1);
}


I put it as mydelay, but still no luck.
By the way, should I be using the attiny on a 1mhz or 8 mhz clock?


Yes but you have to call mydelay, not delay.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

WireJunky




Well, call it myDelay or some different name.


Code: [Select]
// Servo8Bit Example code
// Ilya Brutman

#include "Servo8Bit.h"
void mydelay(uint16_t milliseconds);  //forward declaration to the delay function

Servo8Bit myServo;  //create a servo object.
int led = 0;

void setup()
{
  myServo.attach(0);  //attach the servo to pin PB3
    delay(1);   // <---------- mydelay (1);
}


I put it as mydelay, but still no luck.
By the way, should I be using the attiny on a 1mhz or 8 mhz clock?


Yes but you have to call mydelay, not delay.


What do you mean by call mydelay?
Forgive my ignorance, I'm a beginner in this stuff.

nickgammon

Code: [Select]

    delay(1);   


This line calls (invokes) the function. Thus if you are renaming delay as mydelay you have to do it everywhere in the sketch. You haven't done that.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

WireJunky


Code: [Select]

    delay(1);   


This line calls (invokes) the function. Thus if you are renaming delay as mydelay you have to do it everywhere in the sketch. You haven't done that.


// Servo8Bit Example code
// Ilya Brutman

#include "Servo8Bit.h"
void mydelay(uint16_t milliseconds);  //forward declaration to the delay function

Servo8Bit myServo;  //create a servo object.
int led = 0;

void setup()
{
  myServo.attach(1);  //attach the servo to pin PB3
    mydelay(1);
}
   void loop()
    {
        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(1);                      // 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(1);                      // waits 15ms for the servo to reach the position
        }
    }
void mydelay(uint16_t milliseconds)
{
    for(uint16_t i = 0; i < milliseconds; i++)
    {
        delayMicroseconds(1000);
    }
}//end delay

Like this? I must be missing something, this doesn't work either.

nickgammon

Can you put your code in code tags please, and post your error message?

Read this before posting a programming question

Quote

this doesn't work


... doesn't tell us much. It doesn't compile? It doesn't run?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

WireJunky


Can you put your code in code tags please, and post your error message?

Read this before posting a programming question

Quote

this doesn't work


... doesn't tell us much. It doesn't compile? It doesn't run?



Code: [Select]
// Servo8Bit Example code
// Ilya Brutman

#include "Servo8Bit.h"
void mydelay(uint16_t milliseconds);  //forward declaration to the delay function

Servo8Bit myServo;  //create a servo object.
int led = 0;

void setup()
{
  myServo.attach(1);  //attach the servo to pin PB3
    mydelay(1);
}
   void loop()
    {
        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(1);                      // 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(1);                      // waits 15ms for the servo to reach the position
        }
    }
void mydelay(uint16_t milliseconds)
{
    for(uint16_t i = 0; i < milliseconds; i++)
    {
        delayMicroseconds(1000);
    }
}//end delay


I don't get an error message. By 'it doesn't work' I mean that when I power it up, all the servo does is spin in a circle. Even with standard servo it does nothing. And now when I try to upload it doesn't even upload.

Go Up