0% found this document useful (0 votes)
241 views

Ipoduino Code

This document defines constants and functions for playing tones and music on an Arduino. It includes: 1) Constants for pins connected to a speaker, buttons, LEDs, and a potentiometer. 2) Functions for playing tones, rests, and predefined musical pieces ("giantsteps", "thelicc", "yee"). 3) A setup() function to initialize pins. 4) A loop() function that checks buttons, plays tones in piano or song mode based on button presses.

Uploaded by

api-340868886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
241 views

Ipoduino Code

This document defines constants and functions for playing tones and music on an Arduino. It includes: 1) Constants for pins connected to a speaker, buttons, LEDs, and a potentiometer. 2) Functions for playing tones, rests, and predefined musical pieces ("giantsteps", "thelicc", "yee"). 3) A setup() function to initialize pins. 4) A loop() function that checks buttons, plays tones in piano or song mode based on button presses.

Uploaded by

api-340868886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

#include "pitches.

h"
#define SPEAKER 5
#define POWER 2
#define BUTTON1 A0
#define BUTTON2 A1
#define BUTTON3 A2
#define R 8
#define G 9
#define B 10
#define POT A3
int NOTE_DUR = 50; // Was 137

void setup() {
pinMode(POWER, OUTPUT);
pinMode(SPEAKER, OUTPUT);
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(BUTTON3, INPUT);
pinMode(R, OUTPUT);
pinMode(G, OUTPUT);
pinMode(B, OUTPUT);
pinMode(POT, INPUT);
Serial.begin(9600);
}

void play(int note, int dur) {


tone(SPEAKER, note);
delay(dur * NOTE_DUR);
noTone(SPEAKER);
delay(NOTE_DUR / 3); //delay(dur * NOTE_DUR / 3);
}

void play2(int note, int dur) {


int OGnote = note;
for (int i = 0; i < dur; ++i)
{
if (analogRead(POT) > 700)
note += 350;
else if (analogRead(POT) > 600)
note += 300;
else if (analogRead(POT) > 500)
note += 250;
else if (analogRead(POT) > 400)
note += 200;
else if (analogRead(POT) > 300)
note += 150;
else if (analogRead(POT) > 200)
note += 100;
else if (analogRead(POT) > 100)
note += 50;
tone(SPEAKER, note);
if (analogRead(BUTTON3) > 200)
{
digitalWrite(R, 255);
digitalWrite(G, 0);
digitalWrite(B, 0);
}
else if (analogRead(BUTTON2) > 200)
{
digitalWrite(R, 0);
digitalWrite(G, 255);
digitalWrite(B, 0);
}
if (analogRead(BUTTON1) > 200)
{
digitalWrite(R, 0);
digitalWrite(G, 0);
digitalWrite(B, 255);
}
note = OGnote;
delay(NOTE_DUR);
digitalWrite(R, 0);
digitalWrite(G, 0);
digitalWrite(B, 0);
}
}

void rest(int dur) {


noTone(SPEAKER);
delay(dur * NOTE_DUR);
}

void giantsteps() {
int giantsteps[200][2] = { {NOTE_CS4, 2}, {NOTE_E4, 2}, {NOTE_GS4, 2},
{NOTE_B4, 2}, {NOTE_AS4, 6}, {NOTE_GS4, 2}, {NOTE_FS4, 2}, {NOTE_DS4, 2},
{NOTE_B3, 4}, // 9
{NOTE_D4, 2},{NOTE_E4, 2}, {NOTE_FS4, 2}, {NOTE_A4, 2}, {NOTE_G4, 2},
{NOTE_D4, 2}, {NOTE_B3, 2}, {NOTE_G3, 2}, {NOTE_C4, 2}, {NOTE_GS3, 2},
{NOTE_G3, 2},
{NOTE_F3, 2},{NOTE_DS3, 2},{NOTE_F3, 2}, {NOTE_G3, 2}, {NOTE_GS3, 2},
{NOTE_AS3, 2},{NOTE_C4, 2}, {NOTE_D4, 2}, {NOTE_F4, 2}, {NOTE_E4, 2},
{NOTE_C4, 2},
{NOTE_A3, 2},{NOTE_G3, 2}, {NOTE_FS3, 2}, {NOTE_DS4, 2},{NOTE_D4, 2},
{NOTE_C4, 2}, {NOTE_B3, 2}, {NOTE_D4, 2}, {NOTE_G4, 2}, {NOTE_B4, 2},
{NOTE_D4, 2},
{NOTE_F4, 2},{NOTE_GS4, 2},{NOTE_C5, 2}, {NOTE_DS4, 2},{NOTE_F4, 2},
{NOTE_G4, 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OTE_B4, 2}, {NOTE_AS4, 2},
{NOTE_A4, 2},
{NOTE_GS4,2},{NOTE_C5, 2}, {NOTE_AS4, 2}, {NOTE_G4, 2}, {NOTE_F4, 2},
{NOTE_DS4, 2},{NOTE_G4, 2}, {NOTE_DS4, 2}, {0, 4}, {NOTE_D5, 6},
{NOTE_AS4, 4},
{NOTE_FS4,2},{NOTE_D4, 4}, {0, 2}, {NOTE_D4, 2}, {NOTE_G4, 2},
{NOTE_B4, 2}, {NOTE_D4, 2}, {NOTE_F4, 2}, {NOTE_GS4, 2}, {NOTE_C5, 2},
{NOTE_DS4, 2},
{NOTE_F4, 2},{NOTE_G4, 2}, {NOTE_AS4, 2}, {NOTE_E4, 2}, {NOTE_FS4, 2},
{NOTE_GS4, 2},{NOTE_AS4, 2}, {NOTE_FS4, 6}, {NOTE_DS4, 2}, {NOTE_CS4, 2},
{NOTE_C4, 2},
{NOTE_B3, 2},{NOTE_GS3, 2},{NOTE_C4, 2}, {NOTE_D4, 2}, {NOTE_DS4, 2},
{NOTE_C4, 2}, {NOTE_G4, 2}, {NOTE_F4, 2}, {NOTE_D4, 2}, {NOTE_C4, 2},
{NOTE_AS3, 4}, // 165 + 9 = 174
};

for (int i = 0; i < 174; ++i)


{
play(giantsteps[i][0], giantsteps[i][1]);
}
}

void thelicc() {
int licc[10][2] = { {NOTE_D4, 3}, {NOTE_E4, 2}, {NOTE_F4, 3}, {NOTE_G4, 2},
{NOTE_D4, 1}, {NOTE_E4, 5}, {NOTE_C4, 3}, {NOTE_D4, 8} };

for (int i = 0; i < 8; ++i)


{
play(licc[i][0], licc[i][1]);
}
}

void yee() {
int yee[20][2] = { {NOTE_C4, 3}, {NOTE_D4, 5}, {NOTE_C4, 3}, {NOTE_E4, 4},
{NOTE_E4, 4}, {NOTE_D4, 5}, {NOTE_C4, 4}, {NOTE_D4, 8}, {NOTE_G4, 5}, {NOTE_G4,
3},
{NOTE_D4, 3}, {NOTE_E4, 5}, {NOTE_D4, 3}, {NOTE_F4, 4}, {NOTE_F4, 4},
{NOTE_G4, 5}, {NOTE_A4, 3}, {NOTE_G4, 12}
};

for (int i = 0; i < 4; ++i)


{
play(yee[i][0], yee[i][1]);
}
rest(4);
play(yee[4][0], yee[4][1]);
rest(4);
play(yee[5][0], yee[5][1]);
play(yee[6][0], yee[6][1]);
rest(4);
play(yee[7][0], yee[7][1]);
rest(3);
play(yee[8][0], yee[8][1]);
play(yee[9][0], yee[9][1]);
rest(6);
play(yee[10][0], yee[10][1]);
play(yee[11][0], yee[11][1]);
play(yee[12][0], yee[12][1]);
play(yee[13][0], yee[13][1]);
rest(4);
play(yee[14][0], yee[14][1]);
rest(4);
play(yee[15][0], yee[5][1]);
play(yee[16][0], yee[6][1]);
rest(4);
play(yee[17][0], yee[7][1]);
}

void longnote(int note) {


play2(note, 2);
}

void loop() {
digitalWrite(POWER, HIGH);
bool button1 = false;
bool pianoMode = false;
int current = 0;
bool record = true;
int previous = 0;
while (analogRead(BUTTON1) > 200)
{
current = millis();
if (record)
previous = current;
if (current - previous > 3000)
{
button1 = false;
pianoMode = true;
play(NOTE_C3, 8);
break;
}
else
{
button1 = true;
record = false;
}
}

while (pianoMode)
{
if (analogRead(BUTTON3) > 200)
{
longnote(NOTE_C4);
}
else if (analogRead(BUTTON2) > 200)
{
longnote(NOTE_D4);
}
else if (analogRead(BUTTON1) > 200)
{
longnote(NOTE_E4);
}
else // No sound when no buttons are being pressed
noTone(SPEAKER);
}

digitalWrite(R, 0);
digitalWrite(G, 0);
digitalWrite(B, 0);

if (button1)//analogRead(BUTTON1) > 200)


{
digitalWrite(R, 20);
digitalWrite(G, 255);
digitalWrite(B, 0);
giantsteps();
}
else if (analogRead(BUTTON2) > 200)
{
digitalWrite(R, 0);
digitalWrite(G, 100);
digitalWrite(B, 255);
thelicc();
}
else if (analogRead(BUTTON3) > 200)
{
digitalWrite(R, 255);
digitalWrite(G, 0);
digitalWrite(B, 100);
yee();
}
delay(10);
}

You might also like