| 1 |
/* |
| 2 |
Arduino Starter Kit example |
| 3 |
Project 5 - Servo Mood Indicator |
| 4 |
|
| 5 |
This sketch is written to accompany Project 5 in the Arduino Starter Kit |
| 6 |
|
| 7 |
Parts required: |
| 8 |
- servo motor |
| 9 |
- 10 kilohm potentiometer |
| 10 |
- two 100 uF electrolytic capacitors |
| 11 |
|
| 12 |
created 13 Sep 2012 |
| 13 |
by Scott Fitzgerald |
| 14 |
|
| 15 |
https://store.arduino.cc/genuino-starter-kit |
| 16 |
|
| 17 |
This example code is part of the public domain. |
| 18 |
*/ |
| 19 |
|
| 20 |
// include the Servo library |
| 21 |
#include <Servo.h> |
| 22 |
|
| 23 |
Servo myServo; // create a servo object |
| 24 |
|
| 25 |
int const potPin = A0; // analog pin used to connect the potentiometer |
| 26 |
int potVal; // variable to read the value from the analog pin |
| 27 |
int angle; // variable to hold the angle for the servo motor |
| 28 |
|
| 29 |
void setup() { |
| 30 |
myServo.attach(9); // attaches the servo on pin 9 to the servo object |
| 31 |
Serial.begin(9600); // open a serial connection to your computer |
| 32 |
} |
| 33 |
|
| 34 |
void loop() { |
| 35 |
potVal = analogRead(potPin); // read the value of the potentiometer |
| 36 |
// print out the value to the Serial Monitor |
| 37 |
Serial.print("potVal: "); |
| 38 |
Serial.print(potVal); |
| 39 |
|
| 40 |
// scale the numbers from the pot |
| 41 |
angle = map(potVal, 0, 1023, 0, 179); |
| 42 |
|
| 43 |
// print out the angle for the servo motor |
| 44 |
Serial.print(", angle: "); |
| 45 |
Serial.println(angle); |
| 46 |
|
| 47 |
// set the servo position |
| 48 |
myServo.write(angle); |
| 49 |
|
| 50 |
// wait for the servo to get there |
| 51 |
delay(15); |
| 52 |
} |