const int analogInPin = A0; // Analog input pin ADC0 (PB5)
// Servo8Bit Example code
// Ilya Brutman
#include "TinyDebug.h"
#define LED_PIN PB2
#define SERVO_PIN PB1
#include "Servo8Bit.hpp"
Servo8Bit myServo; //create a servo object.
//a maximum of five servo objects can be created
void setup()
{
pinMode(LED_PIN, OUTPUT);
Debug.begin();
Debug.println(F("Hello Tiny!"));
Debug.print(F("SERVO_PIN:"));
Debug.println(SERVO_PIN);
// Debug.print(F("myServo.attached: "));
// Debug.println(myServo.attached());
Debug.print(F("myServo.attach: "));
Debug.println(myServo.attach(SERVO_PIN));
// Debug.print(F("myServo.attached: "));
// Debug.println(myServo.attached());
// myServo.detach();
// Debug.print(F("myServo.attached: "));
// Debug.println(myServo.attached());
Debug.print(F("myServo.write(0): "));
Debug.println(myServo.write(0)); //rotate to the 0 degree position
Debug.print(F("myServo.readMicroseconds(): "));
Debug.println(myServo.readMicroseconds());
delay(500); //wait 2 seconds
Debug.print(F("myServo.write(180): "));
Debug.println(myServo.write(180)); //rotate to the 180 degree position
Debug.print(F("myServo.readMicroseconds(): "));
Debug.println(myServo.readMicroseconds());
delay(500); //wait 2 seconds
Debug.print(F("myServo.write(90): "));
Debug.println(myServo.write(90)); //rotate to the 90 degree position
Debug.print(F("myServo.readMicroseconds(): "));
Debug.println(myServo.readMicroseconds());
delay(500); //wait 2 seconds
}
//sweep the servo
void loop()
{
/* digitalWrite(LED_PIN, HIGH);
// for (int pos = 0; pos < 180; pos++) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
// Read the analog input value
int sensorValue = analogRead(analogInPin);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)
int pos = sensorValue * 180.0 / 1023.0;
// Print the results to the Debug monitor
Debug.print("Sensor Value: ");
Debug.print(sensorValue);
Debug.print(" | pos: ");
Debug.print(pos);
Debug.println(" V");
myServo.write(pos); // tell servo to go to position in variable 'pos'
for (volatile uint16_t i = 0; i < 0xffff; i++);
}
digitalWrite(LED_PIN, LOW);
// 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'
for (volatile uint16_t i = 0; i < 0xffff; i++);
// }*/
}