Button One stops the LEDs from running through their sequence or restarts them if stopped. Button Two changes the directions of the LEDs (instead of going from 0-255, go from 255-0). The pot is used to change the delay between LED change.
Code: Select all
// Flag to determine whether the LEDs move forward or backwards.
boolean forward = true;
// Flag to determine whether or not the LEDs are still moving.
boolean running = true;
// What's the delay to use
int delayTime = 512;
// What's the current value of the LEDs being fed into the IC.
int ledValue = 0;
// The pins for the IC
static int data = 11;
static int latch = 12;
static int clock = 13;
// The pins for the two buttons
static int runningButton = 2;
static int directionButton = 3;
// The analog pin for reading data from the pot.
static int sensor = 0;
/*
Setup up digital pins for the integrated circuit and the buttons.
*/
void setup()
{
// Initialize for the IC
pinMode(data, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(clock, OUTPUT);
// Initialize the buttons
pinMode(runningButton, INPUT);
pinMode(directionButton, INPUT);
}
/*
Method for doing the work.
*/
void loop()
{
// Has anyone pressed the running button? This needs to be done in the main
// loop so we can restart once the button has been pressed to stop.
checkRunningButton();
// Has anyone pressed the direction button? By checking the button in the
// main loop, we can cause a change in direction when not currently running
checkDirectionButton();
// We only try and run the LEDs if the flag tells us so.
if (running)
{
if (forward)
{
// Before running the LEDs, see if we need to reset the value back
// to the beginning. By doing this, we can maintain the LEDs when
// stopped, and pick up where we stopped.
if (ledValue > 255) ledValue = 0;
goForward();
}
else
{
// Before running the LEDs, see if the LED value needs to be reset.
if (ledValue <1) ledValue = 255;
goBackwards();
}
}
}
/*
Push the current value into the IC to update the LEDs
*/
void updateLEDs(int value)
{
digitalWrite(latch, LOW);
shiftOut(data, clock, MSBFIRST, value);
digitalWrite(latch, HIGH);
}
/*
Change the LEDs by moving the value up, which looks like the LEDs are moving backwards.
*/
void goForward()
{
// Running through the values until a) we received the max value,
// b) the button is pressed to stop or c) the button is pressed
// to change direction.
for (; running && forward && ledValue < 256; ledValue++)
{
// Update the LEDs being displayed
updateLEDs(ledValue);
// First, check the current delay so the delay occurs with the
// most current value as returned by the pot
checkDelay();
delay(delayTime);
// Check the buttons, which might cause us to drop out of the loop
checkRunningButton();
checkDirectionButton();
}
}
/*
Change the LEDs by moving the value down, which looks like the LEDs are moving backwards
*/
void goBackwards()
{
// Running through the values until a) we received the min value,
// b) the button is pressed to stop or c) the button is pressed
// to change direction.
for (; running && !forward && ledValue >= 0; ledValue--)
{
// Update the LEDs being displayed
updateLEDs(ledValue);
// First, check the current delay so the delay occurs with the
// most current value as returned by the pot
checkDelay();
delay(delayTime);
// Check the buttons, which might cause us to drop out of the loop
checkRunningButton();
checkDirectionButton();
}
}
/*
Determine whether the button specified has been pressed. A pressed button
is signalled by a LOW when reading the pin.
*/
boolean checkButton(int inputPin)
{
return (LOW == digitalRead(inputPin));
}
/*
Determine if the direction of the LEDs is changed by a button being pressed.
*/
boolean checkDirectionButton()
{
if (checkButton(directionButton))
{
// Flip the direction, either from forward --> backwards or backwards --> forward
forward = !forward;
// Wait until the button is released
while (checkButton(directionButton));
}
}
/*
Determine the current running status, based on the flag and on whether the button is pressed.
*/
boolean checkRunningButton()
{
if (checkButton(runningButton))
{
// Flip the flag for running, either move from running --> non-running or
// non-running --> running.
running = !running;
// Wait until the button is released;
while (checkButton(runningButton));
}
}
/*
Use the pot reading to determine the delay time between LED changes.
*/
void checkDelay()
{
delayTime = analogRead(sensor);
}