Let's Make Robots!

Audio to LEDs Graph

Can someone simplify this code? all it does it take in an analog signal from 90 and convert them to leds to pins 2-13

const int analogInPin = A0;
int outputValue = 0;
int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int ledPin7 = 8;
int ledPin8 = 9;
int ledPin9 = 10;
int ledPin10 = 11;
int ledPin11 = 12;
int ledPin12 = 13;

void setup()
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);
  pinMode(ledPin9, OUTPUT);
  pinMode(ledPin10, OUTPUT);
  pinMode(ledPin11, OUTPUT);
  pinMode(ledPin12, OUTPUT);
}

void loop()
{
  outputValue = map(analogRead(analogInPin), 0, 90, 0, 12); 
  digitalWrite(ledPin1, LOW);  
  digitalWrite(ledPin2, LOW);  
  digitalWrite(ledPin3, LOW);  
  digitalWrite(ledPin4, LOW);  
  digitalWrite(ledPin5, LOW);  
  digitalWrite(ledPin6, LOW);  
  digitalWrite(ledPin7, LOW);  
  digitalWrite(ledPin8, LOW);  
  digitalWrite(ledPin9, LOW);  
  digitalWrite(ledPin10, LOW);  
  digitalWrite(ledPin11, LOW);  
  digitalWrite(ledPin12, LOW); 
  for(x=0;x<outputValue;x++)
  {
    digitalWrite(x,HIGH);
  }                 
}

 

I'm using a tip121 transistor to convert audio into a bar graph and it works but i don't like how the lights flash when going up or down

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Here is what i have

 

void setup()
{

  //I don't want all these bellow
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);

//I want a simple one line of code to set them all as output
}

The following use of an array may look a little more complicated but will work in a manner that allows you to easily move an LED, something you will like when you modify your pinout.

const int input = A0;
const int leds[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };

#define array_len(x)    ( sizeof(x) / sizeof(x[0]) )
#define NUM_LEDS        array_len( leds )

int x;
int output = 0;

void setup()
{
    for( x = 0; x < NUM_LEDS; x++ )
        pinMode( leds[ x ], OUTPUT );
}

void loop()
{
    output = map( analogRead( input ), 0, 90, 0, NUM_LEDS );   

    for( x = 0; x < NUM_LEDS; x++ )
       digitalWrite( leds[ x ], output >= leds[ x ] ? HIGH : LOW );
}

This is one of the best pieces of code I've seen on LMR in a while :)

There's just a small issue with the condition that determines if a led is on or off:

digitalWrite( leds[ x ], output >= leds[ x ] ? HIGH : LOW );

I think this should be:

digitalWrite( leds[ x ], output >= x ? HIGH : LOW );

 

Good catch, I almost stated a disclaimer it may not be 100% correct.  Got to love those 2AM posts

I got it now, we are lighting up LED in sucession, not one by one. That last for-loop is to count up to the last one on. Ok, got it now.

 

Ok, I think the main issue here is the !12 thing. Yeah, go with BDK's snippit. 

Wait, I need to ammend the above comment --I can see what you are trying to do with that last loop...

First off, good catch, bdk6 --yes, in addition to not using !12, it does need to be >= instead of > to catch that last pin, huh?

Ok, we are mixing up what for-loops do and the code needed to deal with the readings coming in from your ADC. Let's look at what you have now --its the last for loop we want to look at:

This is your for-loop now:

for (x starts at 0 ;  as long as X does not equal the ADC reading, continue ; increase X each time through)

This is what your current for-loop is doing. These loops are used to do something over-and-over a given number of times, you will never have "code" stuck in your for-loop ()'s. 

Really, you probabaly don't need that last for loop there at all. Instead, you may want to look at a switch case command.

 

I've done this

void setup()
{
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
}

int x;
int output = 0;
int input = A0;

void loop()
{
  output = map(analogRead(input), 0, 90, 0, 12);
  for(x=output;x!=12;x++)
  {
    digitalWrite(x, LOW);
  }
  for(x=0;x!=output;x++)
  {
    digitalWrite(x,HIGH);
  }
}

Basically we decrease the lights to the output mapped value then light up the leds what is at the output value. Works nicely

Ok, let me see if I can fill in a few more gaps.

The code I wrote above will not do what you are trying to do here. I just was using it as an example of using a variable for your pins instead of "hard-coding" them. There is a little more needed to do what you want to do. Let's look at the bigger pieces here.

Basically, your pin numbers are sequential, so we can use for-loops to quickly scan through them to read a bunch of pins, set the outputs of a bunch of pins, etc. Within these for-loops, we will need to add some code to well, do what we want the code to do!

If we look at your code above, we can take care of some of the variables and a big chunk of your setup routine. We just need one variable for your pins. "pinVariable"

Within your setup routine, we can use a for-loop like we discussed. 

for(i=lowest pin number;i<higest pin number;i++)
{
   pinMode(i,OUTPUT);

Next, all your "make the outputs low" command. This can be a for-loop as well.

for(i=lowest pin number;i<higest pin number;i++)
{
   digitalWrite(i,0);

Beyond this, as I suggested before, I would then move on using the switch-case command. 

switch (outputValue)
{
    case whatever:
set a pin high
   break;
   etc.
   etc. 

The above is probably the easiest way to do this, but there are a million ways to approach this project. You can convert to binary and allow each bit to control a pin, you can use different code in your for-loops to turn the lights on and off in differerent ways, etc. but the above is probably the most basic version of what you want to do.

int pinNumberVariable;

for(i=lowest pin #;i<higest pin number;i++)
{
   pinMode(i,OUTPUT);
   read your pin(i);
   write your pin(i);
   etc.

The switch case thing I mentioned in the shoutbox is probably best explained by the arduino site. It has a couple extra squigly brackets and some colons that need to go in the right places. Just follow their example with your values.