-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas O Fredericks
committed
Nov 7, 2017
1 parent
c1b01c1
commit 5a202b2
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
examples/servo_control_analog_input/servo_control_analog_input.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |