Can you run 2 different loops in 1 sketch?? need more help....

Hallo everyone,

I am using an Arduino Duemila to trigger clips in Ableton through piezo knock sensors and so far that’s been sorted. So every time I hit the sensor the clip starts.
Now, I am trying to implement another bit of code which would turn LEDs on at a set time.

Here is the problem:

If I run the 2 sketches separately everything works fine. BUT I want everything to work from 1 sketch and if I put the 2 together in the same
loop things start going a bit crazy.

this means that the piezo won’t trigger anything in Ableton anymore, instead will turn on the LEDs. And of course LEDs won’t work with the
delay I’ve set up.

What exactly am I doing wrong?
Is it possible to run 2 different loops in 1 sketch?
or I should use 2 Arduinos running independently?

Could someone help with this please?

here below it’s the code…

#define midichannal 9 // Equates to MIDI Channel 10
#define midichannal 10 // Equates to MIDI Channel 11
#define midichannal 11 // Equates to MIDI Channel 12
#define midichannal 12
#define midichannal 13

// general midi drum notes
#define note_F# 
#define note_D
#define note_A
#define note_G
#define note_F
// define the pins we use
#define switchAPin 12
#define switchBPin 11
#define switchCPin 10
#define switchDPin 9
#define switchEPin 8

#define ledPin 13 // for midi out status
int switchAState = LOW;
int switchBState = LOW;
int switchCState = LOW;
int switchDstate = LOW;
int switchEstate = LOW;

//LED SETUP
int ledDelay = 500;
int redPin = 7;
int yellowPin = 6;
int greenPin = 5;
int whitePin = 4;
int speakerPin = 3;
int speakerDelay(1);

int currentSwitchState = LOW;
int val,t;
void setup() {
pinMode(redPin, OUTPUT ); 
pinMode(yellowPin, OUTPUT ); 
pinMode(greenPin, OUTPUT ); 
pinMode(whitePin,OUTPUT);
pinMode(speakerPin,OUTPUT);
pinMode(switchAPin, INPUT);
pinMode(switchBPin, INPUT);
pinMode(switchCPin, INPUT);
pinMode(switchDPin, INPUT);
pinMode(switchEPin, INPUT);
digitalWrite(switchAPin, HIGH); // turn on internal pullup
digitalWrite(switchBPin, HIGH); // turn on internal pullup
digitalWrite(switchCPin, HIGH); // turn on internal pullup
digitalWrite(switchDPin, HIGH);
digitalWrite(switchEPin, HIGH);
pinMode(ledPin, OUTPUT);
Serial.begin(57600); // set MIDI baud rate
}
void loop() {
// deal with switchA
currentSwitchState = digitalRead(switchAPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x1E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x1E, 0);
switchAState = currentSwitchState;

currentSwitchState = digitalRead(switchBPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x2E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x2E, 0);
switchBState = currentSwitchState;

currentSwitchState = digitalRead(switchCPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x3E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x3E, 0);
switchCState = currentSwitchState;

currentSwitchState = digitalRead(switchDPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x4E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x4E, 0);
switchCState = currentSwitchState;

currentSwitchState = digitalRead(switchEPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x5E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x5E, 0);
switchCState = currentSwitchState;

{
//LED LOOP
//RED LOOP
digitalWrite(redPin, HIGH); delay(ledDelay);
digitalWrite(redPin, LOW); delay(ledDelay);
digitalWrite(redPin, HIGH); delay(ledDelay);
digitalWrite(redPin, LOW); delay(ledDelay);
digitalWrite(redPin, HIGH); delay(ledDelay);
digitalWrite(redPin, LOW); delay(1500);

//YELLOW LOOP
digitalWrite(yellowPin, HIGH); delay(ledDelay);
digitalWrite(yellowPin, LOW); delay(ledDelay);
digitalWrite(yellowPin, HIGH); delay(ledDelay);
digitalWrite(yellowPin, LOW); delay(ledDelay);
digitalWrite(yellowPin, HIGH); delay(ledDelay);
digitalWrite(yellowPin, LOW); delay(1500);


//GREEN LOOP
digitalWrite(greenPin, HIGH); delay(ledDelay);
digitalWrite(greenPin, LOW); delay(ledDelay);
digitalWrite(greenPin, HIGH); delay(ledDelay);
digitalWrite(greenPin, LOW); delay(ledDelay);
digitalWrite(greenPin, HIGH); delay(ledDelay);
digitalWrite(greenPin, LOW); delay(1500);


//WHITE LOOP
digitalWrite(whitePin, HIGH); delay(ledDelay);
digitalWrite(whitePin, LOW); delay(ledDelay);
digitalWrite(whitePin, HIGH); delay(ledDelay);
digitalWrite(whitePin, LOW); delay(ledDelay);
digitalWrite(whitePin, HIGH); delay(ledDelay);
digitalWrite(whitePin, LOW); delay(ledDelay);


//RED & YELLOW LOOP
digitalWrite(redPin, HIGH); delay(ledDelay);
digitalWrite(yellowPin, HIGH); delay(ledDelay);
digitalWrite(redPin, LOW); delay(ledDelay);
digitalWrite(yellowPin, LOW); delay(ledDelay);
digitalWrite(redPin, HIGH); delay(ledDelay);
digitalWrite(yellowPin, HIGH); delay(ledDelay);
digitalWrite(redPin, LOW); delay(ledDelay);
digitalWrite(yellowPin, LOW); delay(ledDelay);
digitalWrite(redPin, HIGH); delay(ledDelay);
digitalWrite(yellowPin, HIGH); delay(ledDelay);
digitalWrite(redPin, LOW); delay(1500);
digitalWrite(yellowPin, LOW); delay(1500);


//GREEN & BLUE LOOP
digitalWrite(greenPin, HIGH); delay(ledDelay);
digitalWrite(whitePin, HIGH); delay(ledDelay);
digitalWrite(greenPin, LOW); delay(ledDelay);
digitalWrite(whitePin, LOW); delay(ledDelay);
digitalWrite(greenPin, HIGH); delay(ledDelay);
digitalWrite(whitePin, HIGH); delay(ledDelay);
digitalWrite(greenPin, LOW); delay(ledDelay);
digitalWrite(whitePin, LOW); delay(ledDelay);
digitalWrite(greenPin, HIGH); delay(ledDelay);
digitalWrite(whitePin, HIGH); delay(ledDelay);
digitalWrite(greenPin, LOW); delay(1500);
digitalWrite(whitePin, LOW); delay(ledDelay);
}
// Send a MIDI note on message. Like pressing a piano key

// channel ranges from 0 15

}
void noteOn(byte channel, byte note, byte velocity) {
midiMsg( (0x90 | channel), note, velocity);
}
// Send a MIDI note off message. Like releasing a piano key

void noteOff(byte channel, byte note, byte velocity) {
midiMsg( (0x80 | channel), note, velocity);
}
// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
digitalWrite(ledPin,HIGH); // indicate we're sending MIDI data
Serial.write(byte(cmd));
Serial.write(byte(data1));
Serial.write(byte(data2));
digitalWrite(ledPin,LOW);
}

Thanks

No you can't. Arduino can only do one thing at a time. You'll have to write the code so that it does both things. The first thing to do is look at the blink without delay example and study up on writing non-blocking code. Then you can do each thing so fast that it will seem as if they are happening simultaneously.

Yep, all these
delay(ledDelay);
and
delay(1500);
are killing your performance.
Need to rethink your software architecture.
You have LEDs changing at 1/2 second intervals.
So have your void loop checking the time, when a 1/2 second has passed (1500 is just three 1/2 seconds) then turn off the current LED and turn on the next one.
The rest of time, you can be processing the midi stuff.

Or, look into interrupts.

Thank you very much for your time guys!

I’ve taken a look at those options you are suggesting but I am at the very beginning with programming so it might take a while to go through everything. :fearful:
For now, I’ve rearranged the delays a bit and for time restrains (the hand in is tomorrow :grin: ) I’ve cut corners and used 2 codes, 1 for the LEDs and uploaded on 1 Duemilanove, then uploaded the link for the MIDI stuff to another Duemilanove and chained the 2 from the 5v pin into the VIN pin of the other…
I guess some of you might cringe but it was he fastest solution…

Too crazy?

1 for the LEDs and uploaded on 1 Duemilanove, then uploaded the link for the MIDI stuff to another Duemilanove

I did something like that once. I built two projects that would always be used together. It made the hardware & software design easier for me, but in hindsight an all-in-one unit would have been better.

For future thought...

  • The main thing is to avoid delay() because your program is "locked up" and can't do anything during the delay.
  • You can put loops inside loops (nested loops). Most of my programs have loops inside the main loop. (Be aware that the outside loop can't do anything while your program is "stuck" in the inside loop.)
  • You can run one loop after another. (Again, you may not want to be stuck in one loop too long.)
  • You can put everything in one big loop.
  • You can put everything into functions in one loop, and then call the functions in sequence. And, you can conditionally call (or skip) some functions (with if-statements, etc.) if the all of the functions don't need to be called every time through the loop.

quoob:
Too crazy?

You threw money at the problem; happens everyday.

Parallel processing, at it's finest!

You can also set up an elapsed timer of sorts using millis() or micros() and save the time T0, then periodically check the time in a loop that goes off and does something else, and when the periodically checked time is greater than some chosen time span after time T0, trigger the other event. Kinda like watching your stopwatch until so many seconds have passed before doing something else. This allows you to "parallel process" in a sense based on elapsed time.

Arrch:

quoob:
Too crazy?

You threw money at the problem; happens everyday.

Nah, university supplies the hardware. It's a matter of being lucky...it doesn't happen every day! :grin:

Sorry to bother everyone again, but I am still on the same project.

The 2 arduino solution explained b4 was fine for me back than, although now I would like to tidy things up a little and try to avoid to cut corners.
Although, it isn't coming that easy! :stuck_out_tongue_closed_eyes:

To recap: I am using piezos to trigger loops in Live9 with an Atmega328, and this works fine!
What I am trying to achieve now is to have an LED turning on every time a piezo it pressed (and the loop is running), so to have an idea which of the triggers has been activated and which one isn't.

I have my LED (4) connected to analog pin 1 (2, 3, 4)
Now, this is my code...

//LED connected to Pin 5
int ledPin = 5;


// what midi channel we're sending on
// ranges from 0 15
#define midichannal 9 // Equates to MIDI Channel 10
#define midichannal 10 // Equates to MIDI Channel 11
#define midichannal 11 // Equates to MIDI Channel 12
#define midichannal 12
#define midichannal 13

// general midi drum notes
#define note_F# 
#define note_D
#define note_A
#define note_G
#define note_F
// define the pins we use
#define switchAPin 12
#define switchBPin 11
#define switchCPin 10
#define switchDPin 9
#define switchEPin 8

#define ledPin 13 // for midi out status
int switchAState = LOW;
int switchBState = LOW;
int switchCState = LOW;
int switchDstate = LOW;
int switchEstate = LOW;



int currentSwitchState = LOW; //I am using this as well to read the pieezo's state to tell LED to turn on
int val,t;
void setup() {
pinMode(switchAPin, INPUT);
pinMode(switchBPin, INPUT);
pinMode(switchCPin, INPUT);
pinMode(switchDPin, INPUT);
pinMode(switchEPin, INPUT);
pinMode(ledPin, OUTPUT);

digitalWrite(switchAPin, HIGH); // turn on internal pullup
digitalWrite(switchBPin, HIGH); // turn on internal pullup
digitalWrite(switchCPin, HIGH); // turn on internal pullup
digitalWrite(switchDPin, HIGH);
digitalWrite(switchEPin, HIGH);
//Turn on LED
analogWrite(ledPin, HIGH);


// set MIDI baud rate
Serial.begin(57600); 
}
void loop() {
  

// deal with switchA
currentSwitchState = digitalRead(switchAPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x1E, 127)
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x1E, 0);
switchAState = currentSwitchState;
{
//read state of the piezo
currentSwitchState = digitalRead(switchAPin);

//check if piezo is pressed
//if so, the buttonState is HIGH
if(currentSwitchState == HIGH){
  
//turn on the LED
analogWrite(ledPin, HIGH);
}
currentSwitchState = digitalRead(switchBPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x2E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x2E, 0);
switchBState = currentSwitchState;

currentSwitchState = digitalRead(switchCPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x3E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x3E, 0);
switchCState = currentSwitchState;

currentSwitchState = digitalRead(switchDPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x4A, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x4A, 0);
switchCState = currentSwitchState;

currentSwitchState = digitalRead(switchEPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x5B, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x5B, 0);
switchCState = currentSwitchState;



// Send a MIDI note on message. Like pressing a piano key

// channel ranges from 0 15
}
}
void noteOn(byte channel, byte note, byte velocity); {
midiMsg( (0x90 | channel), note, velocity);
}
// Send a MIDI note off message. Like releasing a piano key

void noteOff(byte channel, byte note, byte velocity) {
midiMsg( (0x80 | channel), note, velocity);
}
// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
digitalWrite(ledPin,HIGH); // indicate we're sending MIDI data
Serial.write(byte(cmd));
Serial.write(byte(data1));
Serial.write(byte(data2));
digitalWrite(ledPin,LOW);

}

When I try to compile the code what is happening is I am getting errors where I didn't b4...
For instance, I get

interact_LED_PIEZOS:121: error: 'noteOn' was not declared in this scope

on this line

noteOn(0x90, 0x1E, 127)

I had a loop at the while loop() and do loop() but those basically stop the main loop until the subloop is completed (Am I right?).
Where here instead I want the LED to go on once the piezo is triggered and stay on till the "STOP MASTER" piezo will be triggered to stop the loops in Live.

Please remember that those are my first attempts to put things together and my syntax it is still pretty rubbish...
so, I apologies in advance! :blush:

noteOn(0x90, 0x1E, 127)

Missing a tiny something

Do yourself and us a favour before posting your code next time, and use the IDE's auto format tool.

AWOL:

noteOn(0x90, 0x1E, 127)

Missing a tiny something

You mean of the rest of the code?

No, that particular line.

I'd say a ";"?
I've tried but still gives the same error noteOne was not declared in this scope. Does it exactly mean that noteOn is being used in a wrong place?

if I have the code like this

#define midichannal 9 // Equates to MIDI Channel 10
#define midichannal 10 // Equates to MIDI Channel 11
#define midichannal 11 // Equates to MIDI Channel 12
#define midichannal 12
#define midichannal 13

// general midi drum notes
#define note_F# 
#define note_D
#define note_A
#define note_G
#define note_F
// define the pins we use
#define switchAPin 12
#define switchBPin 11
#define switchCPin 10
#define switchDPin 9
#define switchEPin 8

#define ledPin 13 // for midi out status
int switchAState = LOW;
int switchBState = LOW;
int switchCState = LOW;
int switchDstate = LOW;
int switchEstate = LOW;



int currentSwitchState = LOW;
int val,t;
void setup() {
pinMode(switchAPin, INPUT);
pinMode(switchBPin, INPUT);
pinMode(switchCPin, INPUT);
pinMode(switchDPin, INPUT);
pinMode(switchEPin, INPUT);

digitalWrite(switchAPin, HIGH); // turn on internal pullup
digitalWrite(switchBPin, HIGH); // turn on internal pullup
digitalWrite(switchCPin, HIGH); // turn on internal pullup
digitalWrite(switchDPin, HIGH);
digitalWrite(switchEPin, HIGH);

Serial.begin(57600); // set MIDI baud rate
}
void loop() {
  

// deal with switchA
currentSwitchState = digitalRead(switchAPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x1E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x1E, 0);
switchAState = currentSwitchState;

the code compiles perfectly and works once uploaded. :frowning:

noteOne was not declared in this scope

I don't see anything called noteOne, do you?

Please read and act on reply #11 - my eyes hurt.

the code compiles perfectly and works once uploaded

No, it doesn't.

Apologies, that was a typo...

it's possibly more frustrating being treated like an idiot on this forum
than actually not being able to get things out of the coding...

sorry for wasting your time....

have a nice day!

The trick is to pay attention to detail.
I might skim over your code and or your posting and get a little upset about "b4", but I'll vaguely understand it.
I might even see that code is missing (at least) a closing brace, and therefore cannot compile, download or work, despite what you tell me.

The compiler will not skim over anything; it will pick up and examine every single character, and if it doesn't like anything, it will tell you.

The compiler doesn't mind if youwriteallyourcodewithoutspaces, as long as it is syntactically correct, but humans like to see nicely formatted code, which is where the auto format tool in the IDE comes in useful.

I guess the "B4" might come down to a generational barrier and I do apologies about it!

I might have to explain again that I have been going around this all arduino, programming and coding for less than 2 months. Furthermore, I do not consider myself one of the brightest mind on earth so possibly it takes slightly longer for me to understand certain topics...but I am trying at least.

This leads to the fact that for you scanning my code and finding mistakes might take a second... for me to understand a line still takes 1 hour!
And you can believe me on the fact that I normally spend hours around the web trying to first find a solution myself BEFORE bothering you all on this forum. I guess it is quite useless anyway since so far I had learned nothing more out of this "community" than I did on my own.

Oh, and just to anticipate you on any post about my grammar, English it isn't my first language so my writing skills aren't the best either!

I once again apologies for wasting yours and everybody else's time, I will try elsewhere now on....

Kind Regards

Dear qoob,

Your real problem is on this line:

void noteOn(byte channel, byte note, byte velocity); {
midiMsg( (0x90 | channel), note, velocity);
}

Do it this way:

void noteOn(byte channel, byte note, byte velocity) {
midiMsg( (0x90 | channel), note, velocity);
}