/
SantaHat.ino
72 lines (57 loc) · 1.94 KB
/
SantaHat.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Code for Santa's Sorting Hat Project
// By Natasha Dzurny for Brown Dog Gadgets
// Circuit: Connect button to pin D9 and ground; Connect Servo to pin D3
#include <Servo.h> //Include the library that lets you command the servo
int buttonPin = 9; // This is pin D9 on the Crazy Circuits board
int LED = 13; // This is the LED on the board
boolean buttonGate = false;
Servo servo;
int angle = 0;
int startAngle = 95; // The position when the stick is straight up - change angle if necessary
int stageLeft = 30; //The left most position as if you were wearing it - change angle if necessary
int stageRight = 160; //The right most position as if you were wearing it - change angle if necessary
void setup() {
servo.attach(3); //Servo is connected to pin D3
pinMode(buttonPin, INPUT_PULLUP);
pinMode(LED, OUTPUT); // Define pin #13 as output, for the LED
servo.write(startAngle);
delay(10);
Serial.begin(9600);
}
void loop() {
int buttonValue = digitalRead(buttonPin);
if (!buttonGate) {
if (buttonValue == LOW) {
buttonGate = true;
digitalWrite(LED, HIGH);
angleDance(90, 100, 10); // Change if you'd like! The first number is the minimum angle,
angleDance(60, 120, 7); // the second is the maximum angle and the third is
angleDance(40, 150, 4); // how many times it wiggles
chooseYourFate();
delay(5000);
}
}
else {
buttonGate = false;
digitalWrite(LED, LOW);
servo.write(startAngle);
}
}
int angleDance(int angleMin, int angleMax, int numberOfTimes) {
int danceDelay = 200;
for (int i = 0; i <= numberOfTimes; i++) {
int randomAmount = random(angleMin, angleMax);
Serial.println(randomAmount);
servo.write(randomAmount);
delay(danceDelay);
}
}
void chooseYourFate() {
long naughtyOrNice = random(2);
if (naughtyOrNice == 0) {
servo.write(stageLeft);
} if (naughtyOrNice == 1) {
servo.write(stageRight);
}
Serial.println(naughtyOrNice);
}