Is it possible...

…to run two loops at the same time? I have 6 el wires that I am running with the EL Escudo Dos from Sparkfun. They are set up to look like guitar strings “strumming” to the beat. I have a sketch that does great for that. I also have an RGB LED strip that I am using as edge lighting. I would like it to turn on the music and do random color changes, but turn off when the music goes off. The el wire is running on pins 2 - 7, the RGB pins 8-10. I have not had any luck combining code that does not simply loop through all 9 pins. I have spent hours looking at different sketches to no avail. Have I overreached with my first arduino project?

Have you posted your code using the 'suggestions' in the "Sticky" note at the top of this forum group? and did you post a diagram of what you currently have done?
If not then consider that having left no useful information you have received no useful answer... Are you surprised?

Not quite sure what you mean, but...

You can't have two loops as in void loop(), but have a look at functions, which is basically what loop() is, and structure your code in setup() and / or loop() to call your own functions when required.

Bare bones:

long lastRunEL = 0;
long lastRunLED = 0;
int pause = 50;

loop() {
  do_EL();
  if (music playing) {
    do_LED();
  }
}

function do_EL() {
  if (millis() - lastRunEL > pause) {
    // do whatever you want to do with the EL lighting
    lastRunEL = millis();
  }
}

function do_LED() {
  if (millis() - lastRunLED > pause) {
    // do whatever you want to do with the LED string
    lastRunLED = millis();
  }
}

This causes the loop to run constantly, but the actual functions to only be called every 50 ms (based on pause). You can change the pause obviously. This allows you to constantly run the EL strings to do whatever, but only when there's music playing will it also call the LED function, otherwise it gets skipped.

Nick Gannon, that is pretty much what I am trying to do! I looked at that link, and while I logically understand it, it still pretty much looks like greek to me and far as being able to figure out how to transfer that concept to my project.

Here is the sketch I have working for my el wire, this is working how I want:

/*

  • TODO: needs to be documented
  • This fella takes the output of a Electret Mic and an OpAmp and tries to detect rhythm.
  • It works ok, but I need to throw in a low-pass filter to try and focus the sensor on the bass vs. loud events
    */
    int sensorValue = 0;
    float variance = .87;

int newHigh = 0;
int counter = 0;

void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

Serial.begin(9600);

}

void loop()
{
sensorValue = analogRead(A4);

if (sensorValue > newHigh)
{
newHigh = sensorValue;// * variance;
counter = 0;

Serial.println(“beat”);
digitalWrite(2, HIGH);
delay(25);
digitalWrite(3, HIGH);
delay(25);
digitalWrite(4, HIGH);
delay(25);
digitalWrite(5, HIGH);
delay(25);
digitalWrite(6, HIGH);
delay(25);
digitalWrite(7, HIGH);
delay(25);
} else
{
Serial.println(" ");
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
}

counter = counter + 1;
if (counter > 50)
{
counter = 0;
newHigh = newHigh * variance;
}

delay(1
);

}

Here is the sketch for my RGB strip. I love how the colors change, but have not figured out how to make them come on with music and turn off when the music is off, nor have I figured out how to combine the two sketches. Any thoughts?

// color swirl! connect an RGB LED to the PWM pins as indicated
// in the #defines
// public domain, enjoy!

#define REDPIN 11
#define GREENPIN 10
#define BLUEPIN 9

#define FADESPEED 5 // make this higher to slow down

void setup() {
pinMode(A4,INPUT); //microphone
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
}

void loop() {
int r, g, b;

// fade from blue to violet
for (r = 0; r < 256; r++) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from violet to red
for (b = 255; b > 0; b–) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from red to yellow
for (g = 0; g < 256; g++) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
// fade from yellow to green
for (r = 255; r > 0; r–) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from green to teal
for (b = 0; b < 256; b++) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from teal to blue
for (g = 255; g > 0; g–) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
}

  delay(1
  );

Why? Why would anyone put a carriage return between the value and the close paren?

because without it the sketch will not compile, with it, it does and I do not know enough about coding to figure out why. If you have anything helpful to add, please feel free.

because without it the sketch will not compile

Bull. That carriage return does not allow the sketch to compile. Get rid of it.

I tried, and when I did it would not compile, when I put it back it compiled. Appreciate it if you would not call me a liar.

Appreciate it if you would not call me a liar.

Prove it. Post the error message(s). I stand by my statement that the CR is NOT needed. The compiler will back me up.

There is something extraordinarily strange going on if you need additional whitespace to make delay calls compile. It compiles for me on IDE 1.04 with and without the whitespace - can you reproduce the issue?

a picture with and without. The only thing I changed was the semi-colon.
I would appreciate and apology.

WildBill, what do you mean by whitespace? This is all for a class assignment, and I do not understand all the terminology...

The only thing I changed was the semi-colon.

That is NOT the same as removing or adding a carriage return.

I would appreciate and apology.

I'm sorry you are wrong.

Then why don't you explain it to me Paul and be helpful so I can learn, instead of being so condescending. I have already said I was a newb and had not clue. Your original statement was this:

PaulS:

  delay(1

);



Why? Why would anyone put a carriage return between the value and the close paren?

Since I am a newb and do not understand the lingo I thought you were talking about the semi colon.....I now understand what WildBill was referring to by white space....thank you Bill.

jew29:
Then why don't you explain it to me Paul and be helpful so I can learn, instead of being so condescending. I have already said I was a newb and had not clue. Your original statement was this:

PaulS:

  delay(1

);



Why? Why would anyone put a carriage return between the value and the close paren?

Which is correct! The carriage return is between the value and the close parenthesis. The semi-colon was never part of his question and therefore not part of the issue here. Removing the carriage return does NOT automatically remove the semi-colon as well.

I don't know where you figure that by removing the carriage return, you also need to remove the semi-colon. He's not being condescending, he asked a valid question. As he said, I'm sorry, you're wrong.

so why is an extra space an issue? Does it affect the coding in any way?

While it doesn’t affect the compiler (if done properly), it does affect the ability of others to be able to read, understand, and ultimately help you with your coding issues. Learn to write code properly should always be a priority. If you’re so lazy to do that, then do yourself a favor and let the IDE do it for you by highlighting all of your code and clicking on Tools → Auto format.

While you’re at it, go ask any of your past or present language teachers/professors if it’s okay to add random carriage returns in the middle of sentences. They can still read it.

And there is the problem. I am taking a class that is requiring me to use arduino for a project, but does not require any coding experience as a prereq. for the class. Most of the class has been about reading schematics and learning to lay out bread boards and put together pcb's and such. I have never taken ANY coding classes and am completely clueless when it comes to that. It is not lazy, it is lack of knowledge, which is why I am on here to hopefully learn a little, so I understand my project. This summer I am taking Java, and next fall an internet programming class, however that will not help me with this particular project.

Thanks for the tip on the auto format, I did not know about that.