Arduino button music
- Dec 2, 2016
- 1 min read
const int buttonPin2 = 2; // the number of the pushbutton pin const int buttonPin3 = 3; const int buttonPin4 = 4; const int buttonPin5 = 5; const int buttonPin6 = 6; //const int buttonPin7 = 7; //const int buttonPin8 = 8;
int speakerPin = 9; int tone1 = 1915; int tone2 = 1700; int tone3 = 1519; int tone4 = 1432; int tone5 = 1275; //int tone6 = 1136; //int tone7 = 1014;
// variables will change: //int buttonState = 0; // variable for reading the pushbutton status
void setup() { // initialize the LED pin as an output: //pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(speakerPin, OUTPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT); }
void loop() { // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (digitalRead(buttonPin2) == HIGH) {//DO digitalWrite(speakerPin, HIGH); delayMicroseconds(tone1); } else { digitalWrite(speakerPin, LOW); delayMicroseconds(tone1); }
if (digitalRead(buttonPin3) == HIGH) {//RE digitalWrite(speakerPin, HIGH); delayMicroseconds(tone2); } else { digitalWrite(speakerPin, LOW); delayMicroseconds(tone2); }
if (digitalRead(buttonPin4) == HIGH) {//ME digitalWrite(speakerPin, HIGH); delayMicroseconds(tone3); } else { digitalWrite(speakerPin, LOW); delayMicroseconds(tone3); }
if (digitalRead(buttonPin5) == HIGH) {//FA digitalWrite(speakerPin, HIGH); delayMicroseconds(tone4); } else { digitalWrite(speakerPin, LOW); delayMicroseconds(tone4); }
if (digitalRead(buttonPin6) == HIGH) {//SO digitalWrite(speakerPin, HIGH); delayMicroseconds(tone5); } else { digitalWrite(speakerPin, LOW); delayMicroseconds(tone5); }
}









Comments