diff --git a/examples/randomServoAngle/randomServoAngle.ino b/examples/randomServoAngle/randomServoAngle.ino new file mode 100644 index 0000000..30a0cfa --- /dev/null +++ b/examples/randomServoAngle/randomServoAngle.ino @@ -0,0 +1,30 @@ +// INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono +#include + +// Instanciate a Chrono object. +Chrono myChrono; + +// INCLUDE SERVO LIBRARY. +#include + +// 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); + } +} diff --git a/examples/servo_control_analog_input/servo_control_analog_input.ino b/examples/servo_control_analog_input/servo_control_analog_input.ino new file mode 100644 index 0000000..fa62926 --- /dev/null +++ b/examples/servo_control_analog_input/servo_control_analog_input.ino @@ -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 + +// INSTANTION A SERVO OBJECT. +Servo myServo; + +// INCLUDE CHRONO LIBRARY : http://github.com/SofaPirate/Chrono +#include + +// 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 + } + +}