Jared - LED Control

/*Purpose:      Test SIS-2 chip with LEDs
 *Date Created: 2/22/11
 *Authors:      Boby Cole & Harrison Bourne
 *Modified By:  Jared Morell
 *Date Modified:04/11/2011
 */

#include <stdio.h>

const int LEARN=11;  // learn pin on SIS-2
const int OUT1=13;   // output 1 from IR decoder
const int OUT2=12;   // output 2 from IR decoder
const int BUTTON=10; // pushbutton used to control learn state
const int LED3=2;    // bottom LED
const int LED4=3;    // middle LED
const int LED5=4;    // top LED


void setup(){
  pinMode(LEARN, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(OUT1, INPUT);
  pinMode(OUT2, INPUT);
  pinMode(BUTTON, INPUT);
}

int last_out1val=0;
int last_out2val=0;

int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

int indexOfLed = 2;  //index that indicates which LED should be on
int last_index=4;  //pin number of the LED that is on at the
int flag = 0;

void loop(){
 
  //initializes all of the LED outputs to zero
  if(flag == 0){
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    flag = 1;
  }
 
  // read input info
  int out1val=digitalRead(OUT1);
  int out2val=digitalRead(OUT2);
  int reading = digitalRead(BUTTON);
 
  // IR code received corresponding to output 1 (UP)
  if(out1val == HIGH){
    
     digitalWrite(indexOfLed,LOW);
     if(indexOfLed == 2){
        indexOfLed = 4;  //moves the LED to the bottom of the bar when the
                         //index goes above the LED bar's confines
     }else{
        indexOfLed = indexOfLed -1;  //moves up the LED in the LED bar
     }
     digitalWrite(indexOfLed, HIGH);
     delay(500);  //Keeps the LED from moving twice when you press the button slightly to long
   }
   
   // IR code received corresponding to output 2 (DOWN)
   if(out2val == HIGH){
      digitalWrite(indexOfLed,LOW);
      if(indexOfLed == 4){
         indexOfLed = 2;  //moves the LED to the top of the bar when the
                          //index goes below the LED bar's confines
      } else{
         indexOfLed = indexOfLed + 1; //moves down the LED in the LED bar
      }
      digitalWrite(indexOfLed, HIGH);
      delay(500);  //Keeps the LED from moving twice when you press the button slightly to long
   }


/////////////////Debounce Button///////////////////

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    
    lastDebounceTime = millis();  // reset the debouncing timer
  }
 
  if ((millis() - lastDebounceTime) > debounceDelay) {  //if the state of the button has remained the same.
    buttonState = reading;
  }
 
  lastButtonState = reading;

  digitalWrite(LEARN, !buttonState);  //Tells the chip to start programing.
}