|
| 1 | +/** |
| 2 | + * ServoRamp |
| 3 | + * |
| 4 | + * Use ramps to move the position of a RC servo motor with your Arduino and a potentiometer. |
| 5 | + * |
| 6 | + * Circuit: |
| 7 | + * |
| 8 | + * Servo motors have three wires: power, ground, and signal. The power wire is |
| 9 | + * typically red, and should be connected to the 5V pin on the Arduino or Genuino |
| 10 | + * board. The ground wire is typically black or brown and should be connected to |
| 11 | + * a ground pin on the board. The signal pin is typically yellow, orange or white |
| 12 | + * and should be connected to pin 9 on the board. |
| 13 | + * |
| 14 | + * Note that servos draw considerable power, so if you need to drive more than |
| 15 | + * one or two, you'll probably need to power them from a separate supply (i.e. not |
| 16 | + * the +5V pin on your Arduino). Be sure to connect the grounds of the Arduino and |
| 17 | + * external power supply together. |
| 18 | + * |
| 19 | + * Created in 2025 by Sofian Audry |
| 20 | + * |
| 21 | + * Inspired from the following code: |
| 22 | + * https://www.arduino.cc/en/Tutorial/Knob |
| 23 | + * https://www.arduino.cc/en/Tutorial/Sweep |
| 24 | + */ |
| 25 | +#include <Plaquette.h> |
| 26 | + |
| 27 | +// The metronome will launch a new ramp every 5 seconds. |
| 28 | +Metronome metro(5.0); |
| 29 | + |
| 30 | +// Ramp that controls the angle of the servo. |
| 31 | +Ramp ramp; |
| 32 | + |
| 33 | +// The servo-motor output on pin 9. |
| 34 | +ServoOut servo(9); |
| 35 | + |
| 36 | +void begin() { |
| 37 | + // Initialize ramp. |
| 38 | + ramp.speed(30); // 30 degrees per second |
| 39 | + ramp.easing(easeOutQuint); |
| 40 | +} |
| 41 | + |
| 42 | +void step() { |
| 43 | + // When metronome ticks, launch a new ramp. |
| 44 | + if (metro) |
| 45 | + // Ramp to random position. |
| 46 | + ramp.go(randomFloat(180)); |
| 47 | + |
| 48 | + // Send value of ramp to servo (as an angle between 0 and 180 degrees). |
| 49 | + servo.putAngle(ramp); |
| 50 | +} |
0 commit comments