diff options
| author | Vineet K <git@vineetk.net> | 2024-04-19 16:48:55 -0400 |
|---|---|---|
| committer | Shokara Kou <kou@13f0.net> | 2024-04-19 16:48:55 -0400 |
| commit | 7eb9db0d821a0b13441ad7d586a159a8ed4c378f (patch) | |
| tree | dd68fe931695010cea72045fe50d3326e5f631fb | |
| parent | e16ce4456373d02beab8a507c13f0d91598dc887 (diff) | |
connect all 15 buttons to notes arduino
| -rw-r--r-- | notes.c | 20 |
1 files changed, 10 insertions, 10 deletions
| @@ -1,7 +1,7 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * EEL4746C - AVR Piano - Speaker Section | 2 | * EEL4746C - AVR Piano - Speaker Section |
| 3 | * speaker is connected to PD2 and ground | 3 | * speaker is connected to PD2 and ground |
| 4 | * buttons are connceted to PB0-5, PC0-5, PD3-7 | 4 | * 15 buttons are connected to PD3-7, PC0-4, PB0-4 |
| 5 | * the LCD Arduino UNO is connected to TX/RX (PD0/1) | 5 | * the LCD Arduino UNO is connected to TX/RX (PD0/1) |
| 6 | */ | 6 | */ |
| 7 | 7 | ||
| @@ -37,6 +37,7 @@ unsigned short freqs[] = { | |||
| 37 | FREQ(6000), | 37 | FREQ(6000), |
| 38 | FREQ(7000), | 38 | FREQ(7000), |
| 39 | FREQ(8000), | 39 | FREQ(8000), |
| 40 | [255] = 0 // used as a 'silent' note | ||
| 40 | }; | 41 | }; |
| 41 | unsigned char note = 0; | 42 | unsigned char note = 0; |
| 42 | 43 | ||
| @@ -45,7 +46,7 @@ ISR (PCINT0_vect) | |||
| 45 | // check if any button in the whole port is pressed | 46 | // check if any button in the whole port is pressed |
| 46 | for (unsigned char i = 0; i <= 5; i++) { | 47 | for (unsigned char i = 0; i <= 5; i++) { |
| 47 | if (PINB & (1 << i)) { | 48 | if (PINB & (1 << i)) { |
| 48 | note = i + 5 + 6; | 49 | note = i + 5; |
| 49 | break; | 50 | break; |
| 50 | } | 51 | } |
| 51 | } | 52 | } |
| @@ -56,7 +57,7 @@ ISR (PCINT1_vect) | |||
| 56 | // check if any button in the whole port is pressed | 57 | // check if any button in the whole port is pressed |
| 57 | for (unsigned char i = 0; i <= 5; i++) { | 58 | for (unsigned char i = 0; i <= 5; i++) { |
| 58 | if (PINC & (1 << i)) { | 59 | if (PINC & (1 << i)) { |
| 59 | note = i + 5; | 60 | note = i + 5 + 6; |
| 60 | break; | 61 | break; |
| 61 | } | 62 | } |
| 62 | } | 63 | } |
| @@ -67,7 +68,7 @@ ISR (PCINT2_vect) | |||
| 67 | // check if any button in the whole port is pressed | 68 | // check if any button in the whole port is pressed |
| 68 | for (unsigned char i = 3; i <= 7; i++) { | 69 | for (unsigned char i = 3; i <= 7; i++) { |
| 69 | if (PIND & (1 << i)) { | 70 | if (PIND & (1 << i)) { |
| 70 | note = i; | 71 | note = i - 3; |
| 71 | break; | 72 | break; |
| 72 | } | 73 | } |
| 73 | } | 74 | } |
| @@ -82,11 +83,10 @@ main(void) | |||
| 82 | DDRD = (1 << P_TX) | (1 << P_SPKR); | 83 | DDRD = (1 << P_TX) | (1 << P_SPKR); |
| 83 | 84 | ||
| 84 | // Enabling Pin Change Interrupt for All Buttons | 85 | // Enabling Pin Change Interrupt for All Buttons |
| 85 | PCMSK0 = 0b00111111; | 86 | PCMSK0 = 0b00011111; |
| 86 | PCMSK1 = 0b00111111; | 87 | PCMSK1 = 0b00011111; |
| 87 | PCMSK2 = 0b11111000; | 88 | PCMSK2 = 0b11111000; |
| 88 | // PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2); | 89 | PCICR = (1 << PCIE0) | (1 << PCIE1) | (1 << PCIE2); |
| 89 | PCICR = 1 << PCIE2; | ||
| 90 | 90 | ||
| 91 | TCCR1A = 0x00; // use timer1 CTC mode | 91 | TCCR1A = 0x00; // use timer1 CTC mode |
| 92 | TCCR1B = 1 << WGM12; // already prescaled by 8 in table | 92 | TCCR1B = 1 << WGM12; // already prescaled by 8 in table |
| @@ -95,8 +95,8 @@ main(void) | |||
| 95 | 95 | ||
| 96 | // Sends Square wave to the Speaker When a Button is Pressed | 96 | // Sends Square wave to the Speaker When a Button is Pressed |
| 97 | for (unsigned char n = 0;; n = (n + 1) % 2) { | 97 | for (unsigned char n = 0;; n = (n + 1) % 2) { |
| 98 | //if (((PIND & 0b11111000) | (PINC & 0b00111111) | (PINB & 0b00111111)) == 0) | 98 | // check if any buttons are pressed, otherwise don't play a note |
| 99 | if ((PIND & 0b11111000) == 0) | 99 | if (((PINB & PCMSK0) | (PINC & PCMSK1) | (PIND & PCMSK2)) == 0) |
| 100 | continue; | 100 | continue; |
| 101 | 101 | ||
| 102 | // Prepare Timer/Counter | 102 | // Prepare Timer/Counter |
