reading from serial issue

Hello to all... I have a program running on a Mega that listens for incoming midi and positions two servos that play a glockenspiel with two more servos. This all works perfectly without issue.
What I need to do is exchange listening for midi data to listening for single characters through serial instead. This is where I'm having trouble and need advice.

Below is what I have that works perfectly for midi data:
Suitcase1arduinoforum (attachment)

and below is my adaptation of this code to read from the serial monitor. Using letters a-j instead of midi notes. I have used the built in LED to test that it will blink when receiving 'a' but while the code compiles ok, the led won't blink.

Suitcase2arduinoforum (attachment)

Any help much appreciated!

Suitcase1arduinoforum.txt (4.91 KB)

Suitcase2arduinoforum.txt (4.94 KB)

There no attachments so I can't see your code.

Just uploaded again.
What's up with this now...."The message exceeds the maximum allowed length (9000 characters)."
forced me to make attachments which I've never had to do before.

The .txt file seems a bit mangled, but in the Suitcase2 version you don't appear to actually be calling the code which interprets the byte read from the serial port:

void loop() {
  // read serial
  if (Serial.available() > 0) {
    int inByte = Serial.read();
  }
}

Nothing calls hitBeater or moveToMidiNote. Is something being lost in translation here?

goatboyrobbie:
Just uploaded again.
What's up with this now...."The message exceeds the maximum allowed length (9000 characters)."
forced me to make attachments which I've never had to do before.

Your code is greater than 9K. You have to attach.

Also, the files you posted have some kind of formatting issues. In any case, in loop() you are performing a Serial.read() into a local variable inByte but doing nothing with it, therefore nothing is happening.

void loop() {
  // read serial
  if (Serial.available() > 0) {
    int inByte = Serial.read();
  } 
}

That I understand, but not sure how to do something with it. Any suggestions?

goatboyrobbie:
That I understand, but not sure how to do something with it. Any suggestions?

Just call a function to do something with it.

void loop() {
  // read serial
  if (Serial.available() > 0) {
    int inByte = Serial.read();
    getGlockNoteFromMidiNote(inByte);
  }
}

Ok, thats beginning to work! I'll have to digest that now! Thank ye both!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.