// Example sketch for generating tone from 3-way switch position // jd 09/14/2015 // based on Arduino.cc Learning Example /* Melody Plays a melody circuit: * 8-ohm speaker on digital pin 8 created 21 Jan 2010 modified 30 Aug 2011 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Tone */ #include #include "pitches.h" byte swPin_D1 = 2; byte swPin_D2 = 3; byte LED_PIN = 13; byte TONE_PIN = 8; int pulseSw1 = 0; boolean pulseSw1New = false; int pulseMin = 750; int pulseMax = 1625; int pulseMid = 1190; int sw1_uS = pulseMin; // Switch 1 combines result from swPin_D1 and D2 int tempo = 300; // use these arrays to add a tune to the startup int notes[] = {NOTE_C6, NOTE_C6, NOTE_E6, NOTE_G6, NOTE_G6, NOTE_E7, NOTE_E7, NOTE_C7, NOTE_C7 }; int beats[] = {1, 1, 1, 1, 2, 1, 2, 1, 2 }; int lenth = 9; void setup() { // put your setup code here, to run once: pinMode(swPin_D1, INPUT_PULLUP); //set swPin_D1 to be an input pinMode(swPin_D2, INPUT_PULLUP); //set swPin_D2 to be an input pinMode(LED_PIN, OUTPUT); //set LED_PIN to be an OUTPUT pinMode(TONE_PIN, OUTPUT); //set TONE_PIN to be an OUTPUT for (int i = 0; i < lenth; i++) { TimerFreeTone(TONE_PIN,notes[i],beats[i]*tempo); // pause between notes delay(tempo / 2); } Serial.begin(9600); // turn on Serial Port delay(tempo * 2); } void loop() { // put your main code here, to run repeatedly: if (digitalRead(swPin_D1) != 1) { // 3-way Switch 1 in low position sw1_uS = pulseMin; if (pulseSw1 != NOTE_C6) { pulseSw1 = NOTE_C6; // set tone for low pitch pulseSw1New = true; } } else { if (digitalRead(swPin_D2) != 1 ) { // 3-way Switch 1 in high position sw1_uS = pulseMax; if (pulseSw1 != NOTE_G6) { pulseSw1 = NOTE_G6; // set tone for high pitch pulseSw1New = true; } } else { // 3-way Switch 1 in mid position sw1_uS = pulseMid; if (pulseSw1 != NOTE_E6) { pulseSw1 = NOTE_E6; // set tone for center pitch pulseSw1New = true; } } } if (pulseSw1New == true) { TimerFreeTone(TONE_PIN, pulseSw1,tempo); //play tone based on switch 1 position pulseSw1New = false; } Serial.print(pulseSw1); Serial.print(" "); Serial.println(pulseSw1New); delay(1000); }