Created
October 4, 2017 15:52
-
-
Save Muffinoota/4d35532b2a84883686ea1d40d429f46a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Pin Definitions | |
//Pin Definitions | |
//The 74HC595 uses a serial communication | |
//link which has three pins | |
const int data = 8; | |
const int latch = 9; | |
const int clock = 10; | |
const int button = 11; | |
const int speakerPin = 12; | |
int bits[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000}; | |
int masks[] = {B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111}; | |
//Used for single LED manipulation | |
int ledState = 0; | |
const int ON = HIGH; | |
const int OFF = LOW; | |
int ledIterator = 0; | |
bool shouldPlay = false; | |
enum noteLED { | |
ledDo, | |
ledDi, | |
ledRe, | |
ledRi, | |
ledMi, | |
ledFa, | |
ledFi, | |
ledSol, | |
ledSi, | |
ledLa, | |
ledLi, | |
ledTi, | |
none | |
}; | |
//These are used in the bitwise math that we use to change individual LEDs | |
//For more details http://en.wikipedia.org/wiki/Bitwise_operation | |
void setup() { | |
pinMode(2, OUTPUT); //LED pin | |
pinMode(3, OUTPUT); //LED pin | |
pinMode(4, OUTPUT); //LED pin | |
pinMode(5, OUTPUT); //LED pin | |
pinMode(data, OUTPUT); //Bit shift register thing data | |
pinMode(clock, OUTPUT); //Bit shift register thing clock | |
pinMode(latch, OUTPUT); //Bit shift register thing latch | |
pinMode(button, INPUT); //Button | |
pinMode(speakerPin, OUTPUT); //Buzzer out | |
for (int i = 0; i <= 7; i++) { | |
changeLED(i, OFF); | |
} | |
} | |
void loop() { | |
if (digitalRead(button) == LOW) shouldPlay = true; //If button pressed then play the note selected | |
else shouldPlay = false; | |
double knob = analogRead(0); //Read the potentiometer | |
if (knob < 86) lightNoteLED(ledDo); | |
else if (knob < 172) lightNoteLED(ledDi); | |
else if (knob < 256) lightNoteLED(ledRe); | |
else if (knob < 342) lightNoteLED(ledRi); | |
else if (knob < 430) lightNoteLED(ledMi); | |
else if (knob < 512) lightNoteLED(ledFa); | |
else if (knob < 598) lightNoteLED(ledFi); | |
else if (knob < 684) lightNoteLED(ledSol); | |
else if (knob < 768) lightNoteLED(ledSi); | |
else if (knob < 854) lightNoteLED(ledLa); | |
else if (knob < 940) lightNoteLED(ledLi); | |
else if (knob < 1024) lightNoteLED(ledTi); | |
} | |
void playTone(int tone, int duration) { | |
for (long i = 0; i < duration * 1000L; i += tone * 2) { | |
digitalWrite(speakerPin, HIGH); | |
delayMicroseconds(tone); | |
digitalWrite(speakerPin, LOW); | |
delayMicroseconds(tone); | |
} | |
} | |
void playNote(char note, int duration) { | |
if (shouldPlay) { | |
char names[] = { 'c', 'C', 'd', 'D', 'e', 'f', 'F', 'g', 'G', 'a', 'A', 'b'}; | |
int tones[] = { 1915, 1805, 1700, 1607, 1519, 1432, 1362, 1275, 1204, 1136, 1072, 1014}; | |
// play the tone corresponding to the note name | |
for (int i = 0; i < 12; i++) { | |
if (names[i] == note) { | |
playTone(tones[i], duration); | |
} | |
} | |
} | |
} | |
void lightNoteLED(noteLED note) { | |
// int delay = brightness * 3.921568627; | |
int toneTime = 380; | |
for (int i = 2; i <= 5; i++) digitalWrite(i, LOW); | |
for (int i = 0; i <= 7; i++) changeLED(i, OFF); | |
switch (note) { | |
case ledDo: { | |
changeLED(1, ON); | |
playNote('c', toneTime); | |
break; | |
} | |
case ledDi: { | |
changeLED(2, ON); | |
playNote('C', toneTime); | |
break; | |
} | |
case ledRe: { | |
changeLED(3, ON); | |
playNote('d', toneTime); | |
break; | |
} | |
case ledRi: { | |
changeLED(4, ON); | |
playNote('D', toneTime); | |
break; | |
} | |
case ledMi: { | |
changeLED(5, ON); | |
playNote('e', toneTime); | |
break; | |
} | |
case ledFa: { | |
changeLED(6, ON); | |
playNote('f', toneTime); | |
break; | |
} | |
case ledFi: { | |
changeLED(7, ON); | |
playNote('F', toneTime); | |
break; | |
} | |
case ledSol: { | |
digitalWrite(2, HIGH); | |
playNote('g', toneTime); | |
break; | |
} | |
case ledSi: { | |
digitalWrite(3, HIGH); | |
playNote('G', toneTime); | |
break; | |
} | |
case ledLa: { | |
digitalWrite(4, HIGH); | |
playNote('a', toneTime); | |
break; | |
} | |
case ledLi: { | |
digitalWrite(5, HIGH); | |
playNote('A', toneTime); | |
break; | |
} | |
case ledTi: { | |
changeLED(0, ON); | |
playNote('b', toneTime); | |
break; | |
} | |
default: { | |
break; | |
} | |
} | |
} | |
/* | |
* updateLEDs() - sends the LED states set in ledStates to the 74HC595 | |
* sequence | |
*/ | |
void updateLEDs(int value){ | |
digitalWrite(latch, LOW); //Pulls the chips latch low | |
shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register | |
digitalWrite(latch, HIGH); //Pulls the latch high displaying the data | |
} | |
/* | |
* updateLEDsLong() - sends the LED states set in ledStates to the 74HC595 | |
* sequence. Same as updateLEDs except the shifting out is done in software | |
* so you can see what is happening. | |
*/ | |
void updateLEDsLong(int value){ | |
digitalWrite(latch, LOW); //Pulls the chips latch low | |
for(int i = 0; i < 8; i++){ //Will repeat 8 times (once for each bit) | |
int bit = value & B10000000; //We use a "bitmask" to select only the eighth | |
//bit in our number (the one we are addressing this time through | |
value = value << 1; //we move our number up one bit value so next time bit 7 will be | |
//bit 8 and we will do our math on it | |
if(bit == 128){digitalWrite(data, HIGH);} //if bit 8 is set then set our data pin high | |
else{digitalWrite(data, LOW);} //if bit 8 is unset then set the data pin low | |
digitalWrite(clock, HIGH); //the next three lines pulse the clock pin | |
delay(1); | |
digitalWrite(clock, LOW); | |
} | |
digitalWrite(latch, HIGH); //pulls the latch high shifting our data into being displayed | |
} | |
/* | |
* changeLED(int led, int state) - changes an individual LED | |
* LEDs are 0 to 7 and state is either 0 - OFF or 1 - ON | |
*/ | |
void changeLED(int led, int state){ | |
ledState = ledState & masks[led]; //clears ledState of the bit we are addressing | |
if(state == ON){ledState = ledState | bits[led];} //if the bit is on we will add it to ledState | |
updateLEDs(ledState); //send the new LED state to the shift register | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment