Tuesday, 17 March 2015

LED Project

LED Project - Arron Dick

My proposal is to have ~12 LEDs connected up and have a back and forth pattern running between then, similar to the lights on Knightrider.

Further expansions past the back and forth pattern include controlling the speed with a variable resistor and controlling the pattern is some way with a button(either reversing the direction or changing the pattern type)

Fritzing diagram:

Schematic diagram.

 Video of it working, shows the pushing of the button(which stops progression of which LED is going) and the adjustment of the flashing timer using the variable resister.




The connection to the switch came out a little different from the board and I couldn't rearrange it to make it similar to the physical layout.

Code:
/*
  Blinker, modified from the Blink example but made to have various patterns and mulitple LEDS
  Author: Arron Dick
  Date: 25/02/2015
*/

//Sensor pin
int sensorPin = A0;  //Which pin has the sensor
int sensorValue = 0; //varible to store the value coming from the sensor

const int buttonPin = 13;     // the number of the pushbutton pin

//Where we started putting in LEDS
int startLED = 0;
//Number of LEDS etc
int countLED = 13;
int currentLED = startLED; //Currently lit LED number
int direction = 1; //Direction moving in, 1=forwards, -1 = backwards

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status




// the setup function runs once when you press reset or power the board
void setup() {

  //Initialize digital pins 1-8 as outputs.
  for (int i = startLED; i <= startLED + countLED; i++)
  {
    pinMode(i, OUTPUT);
  }
  //set up 13 as a button input
  pinMode(buttonPin, INPUT);


}

// the loop function runs over and over again forever
void loop() {
  //read the buttonState
  buttonState = digitalRead(buttonPin);

  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  sensorValue /= 6; //divide it so that its not too slow at max setting



  digitalWrite(currentLED + startLED, HIGH); // turn the LED on (HIGH is the voltage level)
  delay(sensorValue);              // wait for a second
  digitalWrite(currentLED + startLED, LOW);  // turn the LED off by making the voltage LOW
  delay(sensorValue);              // wait for a second

  currentLED += direction;
  //-1 due to fencepost problem...damnit
  if ((currentLED >= (countLED - 1)) || (currentLED <= 0)) {
    direction = -direction;
  }

  if (buttonState == HIGH) {

    currentLED -= direction;
  }
}

Original idea changed from having just a back and forth pattern to adding in variable resistor and a push button. I would have liked the push button to change the type of pattern that was being displayed. ie, alternativly flashing odd lights, or counting in binary etc. I haven't done anymore on the code as I feel it is sufficient as it is and I don't want to go over board and make it too complex.

Idea has come from an audrino project I soldered up for makerspace a few years ago, but never got to programming the chip.

This code could be extended with different patterns but I wasn't sure how to implement this with using the switch. Interrupts would be useful but probably not necessary.

No comments:

Post a Comment