I am new to Arduino and the DigiSpark board. I was trying to control a servo from a pot and an running into a problem. I can control the LED with the analogRead of A1 but when I enable that line of code, the servo no longer operates even though I am setting the value of "angle" This code will not run the servo.
#include <SimpleServo.h>
SimpleServo ServoTest;
void setup() {
// put your setup code here, to run once
// Serial.begin(9600);
ServoTest.attach(0);
pinMode(1,OUTPUT);
analogReference(INTERNAL2V56);
}
void loop() {
// put your main code here, to run repeatedly:
int angle = map(analogRead(A1), 0, 1023, 0, 180);
//int angle = 127;
analogWrite(1,angle);
angle =0;
ServoTest.write(angle);
delay (500);
angle=45;
ServoTest.write(angle);
delay (500);
angle=90;
ServoTest.write(angle);
delay (500);
angle=135;
ServoTest.write(angle);
delay (500);
angle=180;
ServoTest.write(angle);
delay (500);
}
However, if I comment out the analogRead, as below, the servo does operate.
#include <SimpleServo.h>
SimpleServo ServoTest;
void setup() {
// put your setup code here, to run once
// Serial.begin(9600);
ServoTest.attach(0);
pinMode(1,OUTPUT);
analogReference(INTERNAL2V56);
}
void loop() {
// put your main code here, to run repeatedly:
//int angle = map(analogRead(A1), 0, 1023, 0, 180);
int angle = 127;
analogWrite(1,angle);
angle =0;
ServoTest.write(angle);
delay (500);
angle=45;
ServoTest.write(angle);
delay (500);
angle=90;
ServoTest.write(angle);
delay (500);
angle=135;
ServoTest.write(angle);
delay (500);
angle=180;
ServoTest.write(angle);
delay (500);
}
Any ideas why this might be? I've tried adding some long delays after the analogRead and that doesn't seem to help.
I am using the board from Figure 1. The servo is connected to P0, the onboard LED to P1, and the servo to P2 (A1). When I make the analogRead statement active, I am able to read the A/D value on the pot and control the brightness of the onboard LED with an analogWrite on P1.
Checking that Digispark Board can drive SG90 Servo Motor at DPin-0 (PB0).
You need to install ATTinyCore as per instructions of the attached file..
1. Please, connect a SG90 type Servo Motor with DPin-0 (PB0) of Digispark ATtiny85 Board.
2. Upload the following sketck (tested).
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
If Servo library is installed to libraries, then that version (which doesn't support ATtiny parts)
will be used instead of the one included with the core, which does. To get around this,
include Servo_ATTinyCore.h instead - this will always use the version that came with core.
*/
#include <Servo_ATTinyCore.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(0); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for (pos = 0; pos <= 180; pos += 1)
{ // 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(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1)
{ // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
3. Check that the Servo is turning to-and-fro. 4. Now, connect Pot at Ch-1 of ADC (PB2) to control the position of the shaft of the Servo Motor.
5. Upload the following sketch (tested).
#include <Servo_ATTinyCore.h>
Servo myservo;
void setup()
{
myservo.attach(0);
}
void loop()
{
int y = analogRead(A1);
byte position = map(y, 0, 1023, 0, 180);
myservo.write(position);
delay(1000);
}
6. Check that the position of the shaft changes as you rotate the Pot. ProgrammingATtiny85 (5).pdf (172.6 KB)