// // Copyright 2014, Aaron Ardiri // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // simple arduino sketch using six LED's and the piezo element // // - design is to have a sequence 0,1,2,3,4,5,5,4,3,2,1,0 repeat // - NOTE: double use of LEDs 0 and 5 - this is for nice visual effect #define ledPINBase 2 #define ledPINCount 6 // pins 2,3,4,5,6 and 7 #define piezoPIN 8 // we want to move across the LED pins in approximately a second #define delayPeriod (1000 / ledPINCount) // we want to play tones an octive up and down from middle C #define toneBase 262 #define toneDiff ((toneBase * 2) / ledPINCount) #define toneDuration (delayPeriod / 2) // some global variables int g_direction = 1; int g_led = ledPINBase; // configuration of the arduino void setup() { // configure the LED pins - turn them all off for (int i=ledPINBase; i<(ledPINBase+ledPINCount); i++) { pinMode(i, OUTPUT); digitalWrite(i, LOW); } } // called periodically by the arduino core - this is where we do stuff void loop() { int state; // for visual effect; toggle the state of the LED state = digitalRead(g_led); if (state == HIGH) digitalWrite(g_led, LOW); else digitalWrite(g_led, HIGH); // iterate forwards through LEDs if (g_direction == 1) { g_led++; // have we had a led counter overflow? if (g_led == (ledPINBase+ledPINCount)) { g_led == (ledPINBase+ledPINCount)-1; g_direction = -1; } } // iterate backwards through LEDs else { g_led--; // have we had a led counter underflow if (g_led == (ledPINBase-1)) { g_led = ledPINBase; g_direction = 1; } } // play a ticking sound tone(piezoPIN, toneBase + ((g_led-ledPINBase)*toneDiff), toneDuration); // wait a specific period of time before the next iteration delay(delayPeriod); }