Author Topic: Porting a simple project from Arduino to Digispark (LDR + Servo)  (Read 4152 times)

horstk

  • Newbie
  • *
  • Posts: 2
Hi!

I was wondering if any of you could help me out with a problem I'm currently facing. I wrote a little sketch for my arduino that maps values I get from a LDR to a servo. On my Arduino it works like a charm:

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

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

int ldrpin = 0;  // analog pin used to connect the LDR
int val;    // variable to read the value from the analog pin

void setup()
{
  Serial.begin(9600);
  myservo.attach(A2);  // attaches the servo on pin 2 to the servo object
}

void loop()
{
  val = analogRead(ldrpin);            // reads the value of the LDR (value between 0 and 1023)
  Serial.println(val);
  val = map(val, 0, 1023, 40, 130);     // scale it to use it with the servo (value between 0 and 180)
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(15);                           // waits for the servo to get there

  SoftwareServo::refresh();
  Serial.println(val);
}

Since it's really simple I thought I could port it to my digispark. But I just can't get it to work. I tries SimpleServo.h, SoftwareServo.h, Servo8Bit.h and even SoftRcPulseOut. I tries different servos as well. But i just get jitter or nothing at all.

Any suggestions?

I would be really glad if someone had an idea!

Thank you in advance.

defragster

  • Sr. Member
  • ****
  • Posts: 467
Re: Porting a simple project from Arduino to Digispark (LDR + Servo)
« Reply #1 on: January 16, 2014, 09:47:23 pm »
Having scanned topics before I thought to 'search "servo delay"' - I found stuff - I'd suggest you could try it . . .

Servo output/refresh is timing dependent it seems and DigiSpark with non hardware USB and other things needs to have allowances made . . .

The one I found as expected was along this line:
https://digistump.com/board/index.php/topic,1157.msg5030.html#msg5030

horstk

  • Newbie
  • *
  • Posts: 2
Re: Porting a simple project from Arduino to Digispark (LDR + Servo)
« Reply #2 on: January 19, 2014, 04:33:53 am »
Thanks for the Idea. I actually got it to work with the Adafruit SoftServo Library. Now however, it doesn't do anything. I may have accidentally plugged in 5V and GND in reverse, may I have bricked my board?