openobject.org

Project - LED Display...

From Physical Programming

Image:LED.jpg

So initially I was looking at creating a LED device that reacts to sound, the first step was to have a look at some existing projects. From this research I found some projects with outcomes similar to what I was aiming for.

I considered these as ok starting points for basic electronics.

On makezine.com, the internet, Arduino forums and instructables there were a couple of projects that gave some information however in general a lot of relevent information was missing. For example there were pictures of the projects however the process was unclear and most times no code was given.

There are also kits that you can buy from Jcar that make a VU meter however I was trying to use the code and Arduino to achive a different result.

I also sourced a number of parts for the project: The LED's were from ebay (resistors included) as was the Arduino board (Duemilanove).Everything else was from Jcar.


Image:picleds.jpg


Some of the codes of relevence that I was focusing on using and adapting;

A primary fading code

int value = 0;                            // variable to keep the actual value 
int ledpin = 9;                           // light connected to digital pin 9

void setup() 
{ 
  // nothing for setup 
} 

void loop() 
{ 
  for(value = 0 ; value <= 255; value+=5) // fade in (from min to max) 
  { 
    analogWrite(ledpin, value);           // sets the value (range from 0 to 255) 
    delay(30);                            // waits for 30 milli seconds to see the dimming effect 
  } 
  for(value = 255; value >=0; value-=5)   // fade out (from max to min) 
  { 
    analogWrite(ledpin, value); 
    delay(30); 
  }  
}


A code to cycle LED's in an array

int timer = 100;                   // The higher the number, the slower the timing.
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
int num_pins = 6;                  // the number of pins (i.e. the length of the array)

void setup()
{
  int i;

  for (i = 0; i < num_pins; i++)   // the array elements are numbered from 0 to num_pins - 1
    pinMode(pins[i], OUTPUT);      // set each pin as an output
}

void loop()
{
  int i;

  for (i = 0; i < num_pins; i++) { // loop through each pin...
    digitalWrite(pins[i], HIGH);   // turning it on,
    delay(timer);                  // pausing,
    digitalWrite(pins[i], LOW);    // and turning it off.
  }
  for (i = num_pins - 1; i >= 0; i--) { 
    digitalWrite(pins[i], HIGH);
    delay(timer);
    digitalWrite(pins[i], LOW);
  }
}


So here this is looking at a VU meter. Its creating an array for the LED's and using an input its taking a reading comparing it against thresholds and writing it too the pins.

define NUMBER_OF_LEDS 6
#define NUMBER_OF_SAMPLES 20

#define DEBUG true

#define VU_METER_DISPLAY_DELAY 10

//analog in pins
byte voltageReferencePin = 0;
byte voltagePin = 2;

byte ledPins[NUMBER_OF_LEDS] = { 8 , 9 , 10 , 11 , 12 , 13 };

byte voltageComparisonThresholds[] = { 0 , 10 , 30 , 55 , 80 , 100 };

int voltageReference = 0;

int samples[NUMBER_OF_SAMPLES] = {0};
int sample = 0;
int sampleTotal = 0;
byte sampleIndex  = 0;


void setup() {
 for (byte i=0; i<NUMBER_OF_LEDS; i++){
   pinMode(ledPins[i],OUTPUT);
 }
 pinMode(voltageReferencePin,INPUT);
 pinMode(voltagePin,INPUT);
 
 if(DEBUG){Serial.begin(9600);}
}

void loop(){
 sampleTotal -= samples[sampleIndex]; 
 samples[sampleIndex] = analogRead(voltageReferencePin);
 sampleTotal += samples[sampleIndex++];  
 if (sampleIndex >= NUMBER_OF_SAMPLES) {sampleIndex = 0;}
 sample = sampleTotal / NUMBER_OF_SAMPLES; 

 if(DEBUG){Serial.print("virtual vu: ");}
 
 for (byte i=0; i<NUMBER_OF_LEDS; i++){
   if ( analogRead(voltagePin) >= voltageReference + voltageComparisonThresholds[i] ) {
     digitalWrite(ledPins[i],HIGH); if(DEBUG){Serial.print("|");}
   }else{
     digitalWrite(ledPins[i],LOW);  if(DEBUG){Serial.print(" ");} 
   }
   delay(VU_METER_DISPLAY_DELAY);
 }

 if(DEBUG){ Serial.println(" "); }
 
} 


This code is essentially doing the same thing as the code above in comparing the reading against a value and writing it to a pin.

int val = 0;

void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
}

void loop(){
val = analogRead(0);
if(analogRead(2) > val){
digitalWrite(13, HIGH);
} else{
digitalWrite(13, LOW);
}
delay(10);
if(analogRead(2) > val+10){
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
} else{
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
delay(10);
if(analogRead(2) > val+30){
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
} else{
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
delay(10);
if(analogRead(2) > val+55){
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
} else{
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
delay(10);
if(analogRead(2) > val+80){
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
} else{
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
delay(10);
if(analogRead(2) > val+100){
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
} else{
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
delay(10);

}

I was also looking at this code and was abel to get it to work.

/* Piezo Knock
 * -----------
 * Turn a standard piezo buzzer into a force sensor
 *
 * Created 24 October 2006
 * copyleft 2006 Tod E. Kurt <tod@todbot.com


int ledPin = 13;
int piezoPin = 0;

int THRESHOLD = 100;

int val = 0;       
int t = 0;         

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("ready");      
}


void loop() {
  digitalWrite(ledPin,LOW);     

  val = analogRead(piezoPin);   
  if( val >= THRESHOLD ) {      
    digitalWrite(ledPin, HIGH); 
    t = 0;
    while(analogRead(piezoPin) >= (THRESHOLD/2)) {
      t++;
    } // wait for it to go LOW 
    if(t!=0) 
      Serial.println(t);
  }
}


I was also able to get all the LED's reacting to simple programming commands (ie. to turn on and off in sequence and also to change colour).



So this was the inital aim of what I was trying to do:


Image:diagram_1LED.jpg

Image:diagram_2LED.jpg


I would still like to work on this project to get it working and although the project didn't get to this level I have still gained alot from the process including;

  • A grater introduction / understanding of code and its applications
  • Better understanding of devices and their uses (ie. the Arduino board)
  • Skills in understanding electronics and its applications