Skip to content

Commit

Permalink
Added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas O Fredericks committed Nov 7, 2017
1 parent c1b01c1 commit 5a202b2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/randomServoAngle/randomServoAngle.ino
Original file line number Diff line number Diff line change
@@ -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);
}
}
36 changes: 36 additions & 0 deletions examples/servo_control_analog_input/servo_control_analog_input.ino
Original file line number Diff line number Diff line change
@@ -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.