/*
Murray Code Demonstration
Arduino Uno
Piezo buzzer on pin 8
*/
const int BUZZER = 8;
struct Murray {
char letter;
byte code;
};
Murray table[] = {
{'E',0b00001},
{'A',0b00011},
{'S',0b00101},
{'I',0b00110},
{'U',0b00111},
{'D',0b01001},
{'R',0b01010},
{'J',0b01011},
{'N',0b01100},
{'F',0b01101},
{'C',0b01110},
{'K',0b01111},
{'T',0b10000},
{'Z',0b10001},
{'L',0b10010},
{'W',0b10011},
{'H',0b10100},
{'Y',0b10101},
{'P',0b10110},
{'Q',0b10111},
{'O',0b11000},
{'B',0b11001},
{'G',0b11010},
{'M',0b11100},
{'X',0b11101},
{'V',0b11110}
};
byte lookup(char c) {
c = toupper(c);
for (unsigned int i = 0; i < sizeof(table)/sizeof(table[0]); i++) {
if (table[i].letter == c)
return table[i].code;
}
return 0;
}
void sendBit(bool bitVal) {
if (bitVal)
tone(BUZZER, 1200);
else
tone(BUZZER, 600);
delay(150);
noTone(BUZZER);
delay(50);
}
void sendChar(char c) {
byte code = lookup(c);
Serial.print(c);
Serial.print(" : ");
for (int i = 4; i >= 0; i--) {
bool bitVal = (code >> i) & 1;
Serial.print(bitVal);
sendBit(bitVal);
}
Serial.println();
delay(300);
}
void setup() {
Serial.begin(9600);
Serial.println("Murray Code Demonstration");
Serial.println("-------------------------");
char msg[] = "HELLO";
for (int i = 0; msg[i]; i++) {
sendChar(msg[i]);
}
}
void loop() {
}