Pages: 1 [2]   Go Down
Author Topic: Can you run 2 different loops in 1 sketch?? need more help....  (Read 1665 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25785
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quote
the code compiles perfectly and works once uploaded
No, it doesn't.
« Last Edit: May 11, 2013, 05:24:06 am by AWOL » Logged

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25785
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Sr. Member
****
Karma: 25
Posts: 499
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Dear qoob,

Your real problem is on this line:
Code:
void noteOn(byte channel, byte note, byte velocity); {
midiMsg( (0x90 | channel), note, velocity);
}

Do it this way:
Code:
void noteOn(byte channel, byte note, byte velocity) {
midiMsg( (0x90 | channel), note, velocity);
}
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1204
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The question in the subject has a strong "deja vu" feeling ... oh yes, here it is. http://arduino.cc/forum/index.php/topic,164024.msg1224850.html#msg1224850
Same discussion about not using delay(), and applogies for being "new" ....

No one gets treated "like an idiot" here (OK, some peoples sense of funny/sarcasm can be inappropiate). There is a distinction between being a "newbie" (new, eager to learn, improve, explores on his/her own) and "noob" (new, remains new, learns nothing, expects the full solution to all problems in full and immediatly). To encourage the learning the answer is like "this line is wrong", because it is missing or has a missplaced, a syntactic necessary ";".

If the program "uploads and runs correctly", why is there a need to continue asking smiley-wink ?  (note the smiley)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually my original post goes back to the end of April, but I kept posting on the same link so I didn't have to go all over again with the explanation, maybe another mistake of mine.

Anyway, I guess I am being a bit slow but what I would like to achieve is having "two reactions from one action". I mean, hit the piezo = trigger loop + turn on LED.
Is that still a matter of using delay() ?

 
So far the bit of code I wrote makes the LED go on as soon as I upload the sketch and independently I can still trigger the loops in LIVE9. But I cannot work out how to make the two action related...

My LEDs are connected to analog Pin A5, A4, A3, A2, A1 (although, so far I only tried working with A5 & A4 to keep it simple)

I think I should state a value to tell the program the state of the trigger but I cannot figure it out on my own.

 All the example I have looked at were related to LEDs being switched on by buttons where you can use the values 0 and 1 to determine the state PRESSED or non-PRESSED of the button. But I realised it doesn't really work for a piezo since in works with electrical signals that vary depending on the intensity of the hit on the sensor. (is that right?)

I am coping here below the code again for you to have an idea of what I am trying to write

Code:
// 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 ledPinA = A5; //LED connected to analog Pin 5
int ledPinB = A4; //LED connected to analog Pin 4


int currentSwitchState = LOW;
int val,t;



void setup() {
pinMode(switchAPin, INPUT);
pinMode(switchBPin, INPUT);
pinMode(switchCPin, INPUT);
pinMode(switchDPin, INPUT);
pinMode(switchEPin, INPUT);
pinMode(A5, OUTPUT);
pinMode(A4, 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
digitalWrite(A5, HIGH);
digitalWrite(A4, 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 == LOW);
 
//turn on the LED
digitalWrite(A5, 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;
{
//read state of the piezo
currentSwitchState = digitalRead(switchBPin);

//check if piezo is pressed
//if so, the buttonState is HIGH
if(currentSwitchState == HIGH);
 
//turn on the LED
digitalWrite(A4, HIGH);
}

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);

}

I have attempted only with the first functions related to switchAPin and switchBPin so far.


Hope it makes sense.

thanks again

Regards

Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 32
Posts: 1204
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lets drop all the notes and midi, and just discuss the "abstract" problem.

Your last post asks if one action can have two actions:
Code:
B = digitalRead(pin) ;
if ( B == HIGH) {
  digitalWrite(pinB,HIGH);
  digitalWrite(pinC,HIGH);
  }
No problem.

If you want to run a "loop", inside the other loop and NOT being a simple nested loop,, then you keep its counter seperate
Code:
int MyLoop ;

void loop() {
  B = digitalRead(pin) ;

  // count up loop if B is on
  if ( B == HIGH ) {
    MyLoop = MyLoop + 1;
    if ( MyLoop > 1000 ) {
      digitalWrite(pinB,HIGH) ;// do something here
      MyLoop = 0 ;  // reset the loop
    }
  }
To do two, just repeat the code with a My2ndLoop variable.

If the "B" can go low before the loop is finished, but you must have the loop run to the end, then use a boolean BSeenHigh.
Code:
  if ( B == HIGH ) {   becomes
  if ( B == HIGH && BSeenHigh == false)
    BSeenHigh = true ;
  if (BSeenHigh ) {
and then you reset BSeenHigh when the loop is done.

NB to the smartasses smiley-wink out there - yes, the two long test lines can be rewritten in the more compact and cryptic
BSeenHigh=digitalRead(pin)==HIGH&&!BSeenHigh; (and the ==HIGH could be omitted, but it depends on the value of HIGH)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25785
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hope it makes sense.
Sort-of.

I think I've already suggested using the auto format tool in the IDE, which would correct the eye-aching flat format you've chosen to use.
Logged

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

Offline Offline
Sr. Member
****
Karma: 25
Posts: 499
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This code has some syntax issues:
Note:  I copied the code into my Arduino IDE and clicked the auto format menu option so I could read the code.

Code:
  switchAState = currentSwitchState;
  {
    //read state of the piezo
    currentSwitchState = digitalRead(switchAPin);

    //check if piezo is pressed
    //if so, the buttonState is HIGH
    if(currentSwitchState == LOW);

    //turn on the LED
    digitalWrite(A5, HIGH);
  }

I don't know exactly what you were trying to do with the first line there.  If it was indeed supposed to be a simple assignment statement, why is the following block enclosed in {}?

The if(currentSwitchState == LOW); should not have a semicolon.  The compiler interprets the semicolon as a null statement, and terminates the if block.  Basically, it turns that statement into a no-op, and the following digitalWrite() gets executed every time.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Basically I was try to tell the system to use the "state" of the switchAPin (meaning "on or off") to determine the state of the LED ("on? Off?").

then check the state of the piezo from the SwitchAPin:

currentSwitchState = digitalRead(switchAPin);


so telling then that IF the state of the switchAPin is LOW (piezo has been pressed)

if(currentSwitchState == LOW)



then send to the output pin A5 the command to turn the LED on

digitalWrite(A5, HIGH);


but I guess I am way off, ain't I ?!


I have removed the semi-colon and the { } you suggested but the LEDs still go on as soon as I upload the sketch.  smiley-sad


P.S. apologies if the lines of code aren't appropriately framed but the reply page doesn't allow me to add anything from the "#" button.

Pp.Ss.
could you be so kind to suggest a good IDE with auto-format application?
I've downloaded CODE::BLOCKS but it looks like it goes a bit loopy on my Mac...


Thanks a lot!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25785
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
could you be so kind to suggest a good IDE with auto-format application?
The one that comes as part of the Arduino install is a good starting point.

The text format icons aren't working at the moment (forum migration issue), but you can type the tags yourself.
Logged

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

SG
Offline Offline
Sr. Member
****
Karma: 11
Posts: 497
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is this what you want?
Code:
// 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 ledPinA = A5; //LED connected to analog Pin 5
int ledPinB = A4; //LED connected to analog Pin 4


int currentSwitchState = LOW;
int val,t;



void setup() {
  pinMode(switchAPin, INPUT);
  pinMode(switchBPin, INPUT);
  pinMode(switchCPin, INPUT);
  pinMode(switchDPin, INPUT);
  pinMode(switchEPin, INPUT);
  pinMode(A5, OUTPUT);
  pinMode(A4, 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
  digitalWrite(A5, HIGH);
  digitalWrite(A4, 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 == LOW) {

//turn on the LED
digitalWrite(A5, 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;
 
 
    //read state of the piezo
    currentSwitchState = digitalRead(switchBPin);

    //check if piezo is pressed
    //if so, the buttonState is HIGH
    if(currentSwitchState == HIGH) {

//turn on the LED
digitalWrite(A4, HIGH);
}
 

  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);

}

Logged

Offline Offline
Sr. Member
****
Karma: 25
Posts: 499
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually, I think you're getting pretty close.

If you use the standard IDE, ctrl-t will execute the auto-format.  Or, there is a tools->auto format option.

This code looks good to me:

Code:
  // 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 == LOW)

    //turn on the LED
    digitalWrite(A5, HIGH);

The second currentSwitchState = digitalRead(switchAPin); probably isn't necessary unless you think there is a good chance it has changed since the last time you read it 8 lines up.  You could, as BillHo did, put {} around the last digitalWrite(), but it isn't necessary.

I have questions about the next section of code:

Code:
  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;

  //read state of the piezo
  currentSwitchState = digitalRead(switchBPin);

  //check if piezo is pressed
  //if so, the buttonState is HIGH
  if(currentSwitchState == HIGH)

    //turn on the LED
    digitalWrite(A4, HIGH);

Did you really mean to check   if( currentSwitchState == LOW && switchAState == HIGH ), or should you be checking it against switchBState?

Did you really mean   if(currentSwitchState == HIGH), or should you be checking it against LOW?

Yeah, the # button isn't working for me any more.  I had to manually type the {leftsquarebracket}code{rightsquarebracket} and {leftsquarebracket}/code{rightsquarebracket} thing.
Logged

Pages: 1 [2]   Go Up
Jump to: