Arduino Uno. Wiring diagram attached. I am working through a tutorial on using a keypad to make musical notes with a buzzer. The first step ties keys to a specific character (1, 2, 3, ... A, B, C, etc) and then prints the character to the serial terminal. This works just fine, so I know it isn't a wiring or hardware issue.
In the next step, instead of sending characters to the serial terminal it is supposed to send an integer frequency to a buzzer to play. When I implement that code, the serial terminal shows completely wrong values being generated.
#include <Key.h>
#include <Keypad.h>int buzzer = 10;
const byte ROWS = 4;
const byte COLS = 4;//Original Code, from the first step, edited out
//char buttons[ROWS][COLS] = {
// {'1', '2', '3', 'A'},
//{'4', '5', '6', 'B'},
//{'7', '8', '9', 'C'},
//{'*', '0', '#', 'D'}
//};int tones[ROWS][COLS] = {
{31, 93, 147, 208},
{247, 311, 370, 440},
{523, 587, 698, 880},
{1397, 2637, 3729, 4978}
};byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};Keypad customKeypad = Keypad(makeKeymap(tones),rowPins, colPins, ROWS, COLS);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}void loop() {
// put your main code here, to run repeatedly:
int customKey = customKeypad.getKey();if(customKey) {
Serial.println(customKey);
tone(buzzer, customKey, 1000);
delay(1000);
noTone(buzzer);
}
}
The output I should be getting in the serial terminal (organized by button layout):
31, 93, 147, 208
247, 311, 370, 440
523, 587, 698, 880
1397, 2637, 3729, 4978
The actual output I am getting is:
31, (Nothing), 93, (Nothing)
-109, (Nothing), -48, (Nothing)
-9, (Nothing), 55, 1
114, 1, -72, 1