Help troubleshooting/developing a patch

Hey ArduinoHeads,

I'm a noob and am not really fluent enough in the Arduino code's language to know if what I'm writing will do what I want it to.

Ultimately, I'm trying to take 2 analog inputs (1 mic, 1 pot) and 12 outputs (UV LEDs), where the amplitude of the mic input, after being scaled by a "coefficient" from the pot, controls the 12 LEDs like those in a decibel meter. This is to say:

  1. the range of analog values from 0-1023 is divided into 12 stages, one for each LED: each stage is 85 wide (85 x 12 = 1020, close enough for me)
  2. the pot acts like a gain/trim control on the mic input, so that it can be scaled to fit the amplitude of the sound around it; if not enough LEDs light when the sound is loud, a clockwise knob turn will rescale (multiply) the values coming from the mic so they fill out the 1024 possible values; or, if all the LEDs light too much, a counterclockwise knob turn will keep the "meter" in check; I'm thinking that the range of this coefficient (from the pot) should be about x1 to x20, so the analog value from the pot's pin should be divided by 50 (1024/50 = 20.48); I'm pretty sure I can rescale this value (different multipliers and additive offsets) by myself, but I need to know how to represent the idea of an analog coefficient in the code

I'm using 12v-rated ultraviolet LEDs, so I have an NPN transistor (2N2222) running from each of the Arduino's digi outs to a single LED to switch the 12v circuits on and off. (However as far as the code goes, this doesn't matter, of course.)

I think I'm going to need a series of "if" statements to do this properly. Namely, I need an "if" statement for each of the 12 stages, dividing the mic's 1024 into these bands of 85 such that:
STAGE 1: if micValuepotValue is 0-85: the 1st LED lights
STAGE 2: if micValue
potValue is 85-170: the 1st and 2nd LEDs light
STAGE 3: if micValuepotValue is 170-255: the 1st, 2nd, and 3rd, LEDs light
STAGE 4: if micValue
potValue is 255-340: the 1st, 2nd, 3rd, and 4th LEDs light
... and so on until...
STAGE 12: if micValue*potValue is 935-1020: all LEDs light

Keeping the code as clean and small as possible is preferable since I need this thing to run AFAP (as fast as possible), particularly if it's going to follow and "meter" the sound around it in any meaningful way.

What I have as a skeleton of a proper patch will follow as a reply to this post. ANY and ALL help getting my patch to do what I want is GREATLY appreciated. I learn best thru dialectic, and seeing how others solve the problems I create is the most helpful thing in the world to me when it comes to learning how to solve my own problems.

Thanks,
RealJake

int pin1 = 1; // LED output pins
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int pin8 = 8;
int pin9 = 9;
int pin10 = 10;
int pin11 = 11;
int pin12 = 12;

int micpin = 0; // analog input pins
int potpin = 1;
int micval = 0; // analog variable values
int potval = 0;

void setup()
{
pinMode(pin1, OUTPUT); // declare LED output pins
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
pinMode(pin8, OUTPUT);
pinMode(pin9, OUTPUT);
pinMode(pin10, OUTPUT);
pinMode(pin11, OUTPUT);
pinMode(pin12, OUTPUT);
}

void loop()
{
micval = analogRead(micpin); // read mic pin value
potval = (analogRead(potpin) / 50); // read pot pin value and divide by 50 to scale range to 0-20.48

if ((micval * potval) < 85) // 1st stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 170) // 2nd stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 255) // 3rd stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 340) // 4th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 425) // 5th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 510) // 6th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 595) // 7th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 680) // 8th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 765) // 9th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
digitalWrite(pin9, HIGH);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 850) // 10th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
digitalWrite(pin9, HIGH);
digitalWrite(pin10, HIGH);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 935) // 11th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
digitalWrite(pin9, HIGH);
digitalWrite(pin10, HIGH);
digitalWrite(pin11, HIGH);
digitalWrite(pin12, LOW);
}

else // 12th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
digitalWrite(pin9, HIGH);
digitalWrite(pin10, HIGH);
digitalWrite(pin11, HIGH);
digitalWrite(pin12, HIGH);
}
}

You have a good plan and should just keep going-- everything you say seems right.

The code size and execution speed are not going to be an issue. human visual response is in the milliseconds, while Arduino execution is in the microseconds. As long as you don't use delay() anywhere, it will be happy times.

One thing though, if you keep calling us "Arduinoheads", your code probably won't work. :slight_smile:

D

ps: I posted just as you were putting your code up... so this doesn't relate to your code. Why don't you just prototype it one LED at a time?

pps:

  • there is a command somewhere (not in the main code reference) to write to the entire port (8 pins) in one line;
  • the 20.48 won't happen, as you're not using floats, which coincidentally slow execution and take up lots of code space.
  • you can simplify the setup by doing something like
for (int x = 0; x<7; X++){
pinMode(x,OUTPUT); }

Thanks for your quick replies, Daniel. A few new questions have come from your answers, tho:

(1) Why would I want to write to 8 pins at once? I assume it's so I can turn blocks like 2-9 or 5-12 HIGH or LOW all ot once, but why would this help (practically)? Is the main benefit that the code is more compact? I'm just trying to figure out where your suggestion is coming from.

(2) I should mention that I'm using the 168-equipped NGs, which I think CAN do floats (as I was just reading elsewhere in the forums), but as I don't need the accuracy of floats at all and they slow things down, maybe I'm better off using an evenly-dividing integer like 32 (rather than 50). Thoughts?

(3) What is this "++" command? Most of my coding experience is either graphical (Max/MSP/PD, Isadora) or restricted to SuperCollider, and even in SC I'm not that good. One of the things I rely on in SC, tho, is its handy help features which allow you to hilight any piece of text and look up both its definitions and its instantiations in examples. Is there any way like this that I can quickly find out what "++" does WITHIN the program, without having to resort to an online reference or something similar?

Sorry for the simple questions, but I really am a noob's noob. I only recently got into the Arduino thru a Wesleyan University course on interactive sculpture and installation, and am now trying to integrate it into an involved and complex senior thesis project (in music, actually), so my learning curve has to be QUICK. I've only been using the thing for a few weeks, but I NEED to nail this and a couple other patches by April, so that explains why I might ask questions that seem readily answerable (and which under most circumstances I would take pleasure in answering myself). Ultimately, in these few cases, my learning must follow from my knowing, and not the other way 'round. Once this project is over and done, you'll see me here hacking thru stuff bit by bit and loving it all the way, but until then, I'm gonna be pretty stupid.

Thanks again,
RealJake

hi

(1) Why would I want to write to 8 pins at once?

To make your code read more simply!

(3) What is this "++" command? Most of my coding experience is either graphical (Max/MSP/PD, Isadora) or restricted to SuperCollider, and even in SC I'm not that good. One of the things I rely on in SC, tho, is its handy help features which allow you to hilight any piece of text and look up both its definitions and its instantiations in examples. Is there any way like this that I can quickly find out what "++" does WITHIN the program, without having to resort to an online reference or something similar?

I believe it's C++ based... it means increment the loop, as explained in the reference. A local copy of the same reference is included in the Arduino software.

As for the need to finish quickly, you are probably less than 8 hours from being finished. Start building it...!

I realized that I had to move from top-down rather than bottom-up with regards to my "if" statements. Otherwise, either only the 1st stage would ever activate, or multiple stages would activate together. (For example, if the incoming value is 1000, ALL the stages set under 1000 would activate, rather than JUST the one around 1000.) So now, things read from the 12th stage (highest (micval * potval) value) downwards. I also added a "0th stage" where none of the LEDs light. This means I'm dividing the potential 1024 into 13 parts, of width 79 each. This makes the 0th stage a little narrower than the others (76), but that's okay since everything's off then and that's kinda boring.

I changed the scaling of the potpin to be by 64 (rather than 50 which produces a float), with an additive offset of 1, giving me a multiplicative (with regard to the micval) range of 1-16. This comes from (1024 / 64), which gives 0-15, and adding 1 gives 1-16.

I've also added serial capabilities (9600 bpm): the patch will print out in three columns the values coming from the micpin, the scaled potpin, and their product.

Updated Code as of Thursday 3/22/07 at 1:15AM is as follows (split across this and the next post since it's kinda long):

int pin1 = 1; // LED output pins
int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int pin6 = 6;
int pin7 = 7;
int pin8 = 8;
int pin9 = 9;
int pin10 = 10;
int pin11 = 11;
int pin12 = 12;

int micpin = 0; // analog input pins
int potpin = 1;
int micval = 0; // analog variable starting values
int potval = 0;

void setup()
{
Serial.begin(9600); // start serial port at 9600 bpm
pinMode(pin1, OUTPUT); // declare LED output pins
pinMode(pin2, OUTPUT);
pinMode(pin3, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin7, OUTPUT);
pinMode(pin8, OUTPUT);
pinMode(pin9, OUTPUT);
pinMode(pin10, OUTPUT);
pinMode(pin11, OUTPUT);
pinMode(pin12, OUTPUT);
}

void loop()
{
micval = analogRead(micpin); // read mic pin value
potval = ((analogRead(potpin) / 64) +1); // read pot pin value, divide by 64, and add 1 to scale range to 1-16
Serial.print(micval); // print micval
Serial.print('\t'); // print a tab
Serial.print(potval); // print potval (after scaling to 1-16)
Serial.print('\t'); // print a tab
Serial.println(micval * potval); // print final decibel value (micval * potval) and a line return

if ((micval * potval) > 945) // 12th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
digitalWrite(pin9, HIGH);
digitalWrite(pin10, HIGH);
digitalWrite(pin11, HIGH);
digitalWrite(pin12, HIGH);
}

else if ((micval * potval) > 866) // 11th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
digitalWrite(pin9, HIGH);
digitalWrite(pin10, HIGH);
digitalWrite(pin11, HIGH);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) > 787) // 10TH stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
digitalWrite(pin9, HIGH);
digitalWrite(pin10, HIGH);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) > 708) // 9th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
digitalWrite(pin9, HIGH);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) > 629) // 8th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) > 550) // 7th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) < 471) // 6th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, HIGH);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) > 392) // 5th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, HIGH);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) > 313) // 4th stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, HIGH);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) > 234) // 3rd stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, HIGH);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) > 155) // 2nd stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else if ((micval * potval) > 76) // 1st stage
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}

else // 0th stage
{
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
digitalWrite(pin3, LOW);
digitalWrite(pin4, LOW);
digitalWrite(pin5, LOW);
digitalWrite(pin6, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
digitalWrite(pin9, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin11, LOW);
digitalWrite(pin12, LOW);
}
}