// --------------------------------- // Filename : LEDCycler.ino // Author : Sven Maerivoet // Last modified : 26/07/2015 // Target : C++ (Arduino UNO) // // All rights reserved. // --------------------------------- /* Copyright 2015 Sven Maerivoet 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. */ // constants const int kNrOfLEDs = 11; const int kLEDPins[] = {13,12,11,10,9,8,7,6,5,4,3}; const int kPotPin = A5; const int kButtonPin = 2; int fPotValue = 0; boolean fState = true; // ------------- // Setup routine // ------------- void setup() { for (int i = 0; i < kNrOfLEDs; ++i) { pinMode(kLEDPins[i],OUTPUT); } clear(); pinMode(kPotPin,INPUT); pinMode(kButtonPin,INPUT); fState = false; randomSeed(analogRead(1)); Serial.begin(9600); } // ---------------------- // Loop execution routine // ---------------------- void loop() { fPotValue = analogRead(kPotPin); int buttonState = digitalRead(kButtonPin); if (buttonState == HIGH) { fState = !fState; delay(100); } if (fState) { symmetricCycle(random(0,5)); } else { cylonCycle(); } } // ----------------- // Turn off all LEDs // ----------------- void clear() { for (int i = 0; i < kNrOfLEDs; ++i) { digitalWrite(kLEDPins[i],LOW); } } // ----------------------- // Perform one Cylon cycle // ----------------------- void cylonCycle() { const int kCycleDelay = (1024 - fPotValue) / 10; // cycle forwards for (int i = 1; i < kNrOfLEDs; ++i) { clear(); digitalWrite(kLEDPins[i],HIGH); if (i > 0) { digitalWrite(kLEDPins[i - 1],HIGH); } delay(kCycleDelay); } // cycle backwards for (int i = (kNrOfLEDs - 2); i >= 0; --i) { clear(); digitalWrite(kLEDPins[i],HIGH); if (i < (kNrOfLEDs - 1)) { digitalWrite(kLEDPins[i + 1],HIGH); } delay(kCycleDelay); } } // ----------------------------------------------------- // Symmetrically cycle the LEDs from the center outwards // ----------------------------------------------------- void symmetricCycle(int amplitude) { amplitude *= 2; int cycleDelay = (1024 - fPotValue) / 10; if (cycleDelay < 1) { cycleDelay = 1; } if (amplitude > 1) { cycleDelay /= (amplitude / 2); } const int kCenterPos = kNrOfLEDs / 2; clear(); if (amplitude > kNrOfLEDs) { amplitude = kNrOfLEDs; } for (int i = 0; i < (amplitude + 1); ++i) { digitalWrite(kLEDPins[kCenterPos - i],HIGH); digitalWrite(kLEDPins[kCenterPos + i],HIGH); delay(cycleDelay); } for (int i = amplitude; i > 0; --i) { digitalWrite(kLEDPins[kCenterPos - i],LOW); digitalWrite(kLEDPins[kCenterPos + i],LOW); delay(cycleDelay); } clear(); delay(50); }