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.

 

for(x=0; x<=12; ++x)

{

   if( outputValue >= x) digitalwrite(x,HIGH);

   else digitalWrite(x,LOW);

}

for(x=0; x<=12; ++x)

   digitalWrite( x, outputValue >= x ? HIGH : LOW );

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=1; x<=12; ++x){if(output >= x) digitalWrite(x,HIGH); else digitalWrite(x,LOW);}
}

 

Here is what i now have, works nicely

Would be nice if i could set all the output pins like bits

DPS = 0000000000000;

as inputs in one strike without all those lines in the setup