Permalink
Browse files

Added examples

  • Loading branch information...
thomasfredericks committed Nov 7, 2017
1 parent c1b01c1 commit 5a202b2d716ebbce6ed93c0566e3f690a95a2fa8
@@ -0,0 +1,30 @@
// INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono
#include <Chrono.h>
// Instanciate a Chrono object.
Chrono myChrono;
// INCLUDE SERVO LIBRARY.
#include <Servo.h>
// INSTANTION A SERVO OBJECT.
Servo myServo;
void setup() {
// ATTACH THE SERVO TO PIN 9
myServo.attach(9);
}
void loop() {
// Use Chrono as a metronome with an interval of 1000 ms :
if (myChrono.hasPassed(1000) ) { // elapsed(1000) returns 1 (true) if 1000 ms have passed.
myChrono.restart(); // restart the Chrono
int randomAngle = random(0,180);
myServo.write(randomAngle);
// ALTERNATIVE: USE MICROSECONDS FOR MORE PRECISION.
// int randomUs = random(1500,2500);
// myServo.writeMicroseconds(randomUs);
}
}
@@ -0,0 +1,36 @@
/*
Controlling a servo position using a potentiometer (variable resistor).
The Chrono library is used to wait till the servo gets to its position.
*/
// INCLUDE SERVO.
#include <Servo.h>
// INSTANTION A SERVO OBJECT.
Servo myServo;
// INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono
#include <Chrono.h>
// Instanciate a Chrono object.
Chrono myChrono;
// Analog pin used to connect the potentiometer.
int potpin = A0;
void setup() {
myServo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
// THIS BLOCK OF CODE WILL RUN EVERY 15 MS.
if (myChrono.hasPassed(15) ) { // elapsed(15) returns 1 (true) if 15 ms have passed.
myChrono.restart(); // restart the Chrono
int val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // 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
}
}

0 comments on commit 5a202b2

Please sign in to comment.