-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphoto-buzzer-button-servo.ino
172 lines (125 loc) · 4.12 KB
/
photo-buzzer-button-servo.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Scholars' Lab Arduino Tutorials
Example Sketch 10
PHOTORESISTOR -> BUZZER & BUTTON -> SERVO
Pushing a button causes the servo motor to rotate 180 then back.
Amount of light triggers playing a tune on the buzzer.
This sketch uses code from SparkFun SIK version 3.3,
from experiments 5, 6, 8, and 11
This code is completely free for any use.
Visit https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v33/ for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.
Version 1.0/2022 AES
*/
/**************************************************
SERVO CODE
*/
// Include the Servo library
#include <Servo.h>
// Create constants for setting pins.
const int sensorPin = 0;
const int servoPin = 9;
// Create servo control object
Servo servo;
/**************************************************/
/**************************************************
BUZZER CODE
*/
const int buzzerPin = 12;
const int songLength = 18;
char notes[songLength] =
{'c', 'd', 'f', 'd', 'a', ' ', 'a', 'g', ' ', 'c', 'd', 'f', 'd', 'g', ' ', 'g', 'f', ' '};
int beats[songLength] = {1, 1, 1, 1, 1, 1, 4, 4, 2, 1, 1, 1, 1, 1, 1, 4, 4, 2};
int tempo = 113;
/****************************************************/
/*****************************************************
PHOTORESISTOR CODE
*/
// Set some contstants. Set the low at the highest value, it will auto adjust.
// Set the high at the lowest, it will also auto adjust.
int lightlevel, low = 1023, high = 0;
/* Change tripPoint value to increase or decrease the sensitivity.
* View the values produced by the photoresistor (Serial.println)
* to see what the high and low values will be. Set this about 100
* more than the low value.
*/
int tripPoint = 400;
/******************************************************/
/*****************************************************
BUTTON CODE
*/
int buttonPin = 2;
int buttonState;
/*****************************************************/
void setup() {
// set the servo pin
servo.attach(servoPin);
servo.write(0); // servo should be at 0 to start
// set the buzzer pin
pinMode(buzzerPin, OUTPUT);
// set the button pin
pinMode(buttonPin, INPUT);
// Uncomment line below to allow for serial output - reading the sensor data
Serial.begin(9600);
}
void loop() {
/**************************************
BUTTON AND SERVO CODE
*/
buttonState = digitalRead(buttonPin);
// if the button is pressed, move the servo to angle 180
// if not, angle is 0
if (buttonState == LOW) {
servo.write(180);
} else {
servo.write(0);
}
/*************************************/
/*********************************
LIGHT AND BUZZER CODE
*/
lightlevel = analogRead(sensorPin);
Serial.println(lightlevel);
if (lightlevel < tripPoint) {
playBuzzer(); // this function created below
}
/********************************/
}
void playBuzzer()
{
int i, duration; //
for (i = 0; i < songLength; i++) // for loop is used to index through the arrays
{
duration = beats[i] * tempo; // length of note/rest in ms
if (notes[i] == ' ') // is this a rest?
delay(duration); // then pause for a moment
else // otherwise, play the note
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration); // wait for tone to finish
}
delay(tempo / 10); // brief pause between notes
}
}
int frequency(char note)
{
int i;
const int numNotes = 8; // number of notes we're storing
char names[numNotes] = {
'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'
};
int frequencies[numNotes] = {
262, 294, 330, 349, 392, 440, 494, 523
};
// Now we'll search through the letters in the array, and if
// we find it, we'll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes
{
if (names[i] == note) // Is this the one?
{
return (frequencies[i]); // Yes! Return the frequency and exit function.
}
}
return (0); // We looked through everything and didn't find it,
// but we still need to return a value, so return 0.
}