Arduino, why didn’t we meet before? My first basic light show.

December 16th, 2011

So lately I’ve been taking some introductory classes that are part of my Electrical Engineering minor and I have to say that they turned out to be quite boring and never deviated from the typical “looking at graphs on the oscilloscope and calculating values on circuit diagrams”. Yet the subject is extremely interesting (and fun to play with), so I started reading on my own. I was particularly interested to read about programmable micro controllers, so I couldn’t wait much longer to buy my first Arduino and try something!

I wanted to see if I could make a simple LED turn on and off at different intensities based on a beat coming out of a 3.5mm audio jack. So I stripped off an old pair of audio earphones, soldered the stranded wire with some solid wire (for ease of prototyping on a breadboard), plugged one of the two wires into one of the analog pins of the Arduino, wrote a simple sketch, uploaded it, turned on the volume and… voila’!

Here’s the code of the sketch. It’s the first time that I see my code brought out of a computer. It feels magical.

#define CHANNEL1 5
#define THRESHOLD 90

#define LED1 3

int val;

void setup(){
  Serial.begin(9600);
}

void loop(){
  val = analogRead(CHANNEL1);

  if (val > THRESHOLD){

    // Slow!
    // analogWrite(LED1, map(val, 0, 1023, 0, 255));

    // Binary shift is faster
    analogWrite(LED1, val >> 2);
  }else{
    analogWrite(LED1, 0);
  }
}

This is just a simple example. The threshold value is fixed and needs a constant volume for consistent results. A better solution would include some sort of calibration built in.

When I first used the map function of the Arduino to scale the values from analogRead (which range from 0 to 1023) to analogWrite (0 – 255) I had around a second lag between the beats and the LED catching up, so I used a simple shift to divide the input by 4. This increased the performance a lot and the lag is now barely noticeable.

I love it.


Follow me @pierotofy and don't forget to
  • MrPaky

    have used C#?

    • Anonymous

      it’s not c#, it’s the language of Arduino. It’s a subset of C that includes some functions written only for Arduino.