#include <TM1637Display.h> | |
#if defined(__AVR_ATtiny85__) | |
#include <Adafruit_SoftServo.h> | |
#define Servo Adafruit_SoftServo | |
#define SERVO_PIN PB0 | |
#define POT_PIN PB1 | |
#define DISPLAY_CLK PB3 | |
#define DISPLAY_DIO PB4 | |
#else | |
#include <Servo.h> | |
#define LED_PIN 3 | |
#define SERVO_PIN 9 | |
#define POT_PIN 0 | |
#define DISPLAY_CLK 4 | |
#define DISPLAY_DIO 3 | |
#endif | |
// | |
Servo myservo; // create servo object to control a servo | |
int val; // variable to read the value from the analog pin | |
int lastVal; | |
TM1637Display display(DISPLAY_CLK, DISPLAY_DIO); //set up the 4-Digit Display. | |
#define ADMUX_VREF 0x21 | |
void setup() { | |
// Set up the interrupt that will refresh the servo for us automagically | |
OCR0A = 0xAF; // any number is OK | |
TIMSK |= _BV(OCIE0A); // Turn on the compare interrupt (below!) | |
display.setBrightness(0x0a); //set the diplay to maximum brightness | |
myservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object | |
pinMode(POT_PIN, INPUT); | |
} | |
void loop() { | |
{ | |
long avg = 0; | |
for (int i = 0; i < 100; i++) { | |
avg += analogRead(POT_PIN); | |
} | |
val = avg / 100; | |
} | |
val = map(val, 0, 1023, 400, 2400); // scale it to use it with the servo (value between 0 and 180) | |
if (val != lastVal) { | |
lastVal = val; | |
} | |
display.showNumberDec(val);// , false, 4, 3); | |
myservo.writeMicroseconds(val); // sets the servo position according to the scaled value | |
delay(5); | |
} | |
// We'll take advantage of the built in millis() timer that goes off | |
// to keep track of time, and refresh the servo every 20 milliseconds | |
volatile uint8_t counter = 0; | |
SIGNAL(TIMER0_COMPA_vect) { | |
// this gets called every 2 milliseconds | |
counter += 2; | |
// every 20 milliseconds, refresh the servos! | |
if (counter >= 20) { | |
counter = 0; | |
myservo.refresh(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment