Combination Experiment

For makers who have purchased an Adafruit Starter Pack, get help with the tutorials here!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
scsosna
 
Posts: 6
Joined: Wed Oct 27, 2010 8:33 pm

Combination Experiment

Post by scsosna »

The following uses CIRC-05, CIRC-07, and CIRC-08 together - you'll need to adjust what goes where to make it all fit, but it does. You need to be very BANNED in what goes where, I put the buttons on top, the IC towards the bottom, and the LEDs were immediately next to each other. There's probably better ways than what I did (the pot was hidden beneath a multitude of wires) but it does work!

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);
}

User avatar
len17
 
Posts: 394
Joined: Sat Mar 14, 2009 7:20 pm

Re: Combination Experiment

Post by len17 »

It's time to quit the tutorials and start assembling your robot army. :)

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Starter Pack”