Here I just made a simple little piano. It uses four push buttons connected to digital pins 1-4 and a piezo speaker connected to pin 8. I used notes that are easily distinguishable when played.
the code:
#include "pitches.h" // include this library
int switchPin1 = 1; // set up variables
int switchPin2 = 2; int switchPin3 = 3; int switchPin4 = 4; void setup() {
pinMode(switchPin1, INPUT); // set pinModes to input pinMode(switchPin2, INPUT); pinMode(switchPin3, INPUT); pinMode(switchPin4, INPUT); } void loop() {
if (digitalRead(switchPin1) == HIGH) { // if button is pressed, tone (8, NOTE_D3, 50); // play this tone } if (digitalRead(switchPin2) == HIGH) { tone (8, NOTE_G3, 50); } if (digitalRead(switchPin3) == HIGH) { tone (8, NOTE_C4, 50); } if (digitalRead(switchPin4) == HIGH) { tone (8, NOTE_F4, 50); } } A Fritzing picture of my circuit is attached.
|