Created
May 16, 2016 22:58
-
-
Save davidjb/483af5d606433f37207f5dbcaa6e43d7 to your computer and use it in GitHub Desktop.
Arduino random tone player
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Arduino.h" | |
#include "tones.h" | |
const int LED_PIN = 13; | |
const int BUTTON_PIN = 2; | |
const int PIEZO_PIN = 8; | |
int current_tone; | |
boolean playing = false; | |
void setup() { | |
randomSeed(analogRead(0)); | |
// put your setup code here, to run once: | |
pinMode(LED_PIN, OUTPUT); | |
// turn off the inbuilt LED | |
digitalWrite(LED_PIN, LOW); | |
pinMode(BUTTON_PIN, INPUT_PULLUP); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
if (digitalRead(BUTTON_PIN) == LOW) { | |
playing = true; | |
digitalWrite(LED_PIN, HIGH); | |
tone(PIEZO_PIN, NOTES[current_tone], 100); | |
} | |
if (playing && digitalRead(BUTTON_PIN) == HIGH) { | |
// If we were playing, and button pressed, turn off | |
digitalWrite(LED_PIN, LOW); | |
noTone(PIEZO_PIN); | |
// pick a new tone for next time | |
current_tone = random(0, NOTES_LEN - 1); | |
playing = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment