Go Down

Topic: Can you run 2 different loops in 1 sketch?? need more help.... (Read 2 times) previous topic - next topic

quoob

Apr 18, 2013, 05:54 pm Last Edit: May 11, 2013, 11:41 am by quoob Reason: 1
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...
Code: [Select]

#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

Delta_G

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.

CrossRoads

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.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

TanHadron


quoob

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. :smiley-eek-blue:
For now, I've rearranged the delays a bit and for time restrains (the hand in is tomorrow  :smiley-mr-green: ) 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?

DVDdoug

Quote
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.

Arrch


TanHadron


rickso234

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.

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!  :smiley-mr-green:

quoob

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! :smiley-yell:

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...


Code: [Select]


//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!  :smiley-red:

AWOL

#11
May 11, 2013, 11:44 am Last Edit: May 11, 2013, 12:00 pm by AWOL Reason: 1
Code: [Select]
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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

quoob


Code: [Select]
noteOn(0x90, 0x1E, 127)
Missing a tiny something


You mean of the rest of the code?

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

quoob

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
Code: [Select]
#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.  :(




Go Up