-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sketch.ino
102 lines (88 loc) · 2.22 KB
/
Sketch.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
#include "pitches.h" //file with the frequencies of notes in the fourth octave
#define SPEAKER_PIN 16 // defining the speaker pin
#define POTENTIOMETER_PIN A0 // defining the potentiometer pin
const int buttonPins[] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 14, 15}; // declares an array representing the pin numbers for buttons
// notes mapped to respective indices of the buttonPins array
const int Notes[] = {NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4,
NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4};
const int NumNotes = sizeof(buttonPins) / sizeof(buttonPins[0]);
int potValue;
void setup()
{
// Initialize pins and components
initializePins();
}
void loop()
{
// Read buttons, adjust pitch, and play notes
int selectedNote = readButtons();
if (selectedNote)
{
playNote(selectedNote);
}
else
{
stopSound();
}
potValue = analogRead(POTENTIOMETER_PIN);
adjustPitchAndPlay(selectedNote);
}
void initializePins()
{
// Set up button pins as inputs with pull-up resistors
for (int i = 0; i < NumNotes; i++)
{
pinMode(buttonPins[i], INPUT_PULLUP);
}
pinMode(SPEAKER_PIN, OUTPUT); // Set the speaker pin as an output
}
int readButtons()
{
// Check if any button is pressed and return the corresponding note
for (int i = 0; i < NumNotes; i++)
{
if (digitalRead(buttonPins[i]) == LOW)
{
return Notes[i];
}
}
return 0; // No button pressed
}
void playNote(int note)
{
// Play a note on the speaker
tone(SPEAKER_PIN, note);
}
void stopSound()
{
// Stop the sound on the speaker
noTone(SPEAKER_PIN);
}
void adjustPitchAndPlay(int note)
{
if (note)
{
for (int i = 0; i < NumNotes; i++)
{
if (digitalRead(buttonPins[i]) == LOW)
{
int selectedNote = Notes[i];
int adjustedNote = adjustPitch(selectedNote);
playNoteForDuration(adjustedNote, 200);
stopSound();
}
}
}
}
int adjustPitch(int note)
{
// Modify the pitch of a note based on the potentiometer reading
int pitchRange = map(potValue, 0, 1023, -12, 12);
return note + pitchRange;
}
void playNoteForDuration(int note, int duration)
{
// Play a note for a specified duration
playNote(note);
delay(duration);
}