Seial print showing garbage.

Hi,

I have moved on with the project but am now getting problems with the serial print output.

Please ignore the TFT screen completely as it is not functioning due to I suspect a hardware fault, so I have removed it.

The serial Monitor detects the button presses but the screen displays only garbage not the expected text. :o

// Mdidi Foot Switch Controller
// By Martin Shaw

// Using a different TFT library. Enrico Gueli 2013

#include <TFT.h>
#include <SPI.h>
#include <MIDI.h>
#include <SoftwareSerial.h>
MIDI_CREATE_DEFAULT_INSTANCE();

// pin definition for Arduino UNO
#define cs    A5
#define dc    A3
#define mosi  A2
#define sclk  A1
#define rst   A4

// create an instance of the library
// TFT TFTscreen = TFT(cs, dc, rst);

//TFT(cs, dc, mosi, sclk, rst);
TFT TFTscreen = TFT(A5, A3, A2, A1, A4);

void setup()
{
  //initialize the library
  TFTscreen.begin();

  // clear the screen with a black background
  TFTscreen.background(0, 0, 0);
  //set the text size
  TFTscreen.setTextSize(2);
  // print Hello, World! in the middle of the screen
  TFTscreen.text("Hello, World!", 6, 57);

  for (byte i = 0; i < 5; i++)  pinMode(2 + i, INPUT); // Set pins 2 - 6 to input
  MIDI.begin(16);
}

void speed()
{
  // Set MIDI baud rate:
  Serial.begin(9600);
}

void loop()
{
  int myswitch [5];
  byte last_switch[5];

  // Loop to read switch input
  for (byte i = 0; i < 5; i++) myswitch[i] = digitalRead(2 + i);

  // Check for buttonj press 1 to 5
  if (myswitch[1] != last_switch[1]) {
    last_switch[1] = myswitch[1];
    if (myswitch[1] == HIGH) {  // When button pressed the voltage goes high
      MIDI.sendNoteOn(13, 5, 16);  // (note, velocity, channel)
      Serial.print("Button 1 pressed");
      TFTscreen.background(0, 0, 0);  // Clear Screen
      TFTscreen.text("B1 N13", 6, 57); // Print on screen which button pressed and note sent
      delay(100);

      // If button not pressed then send note off, may not be required.
    } else {
      MIDI.sendNoteOff(13, 0, 16);
      Serial.print("Button 1 off");
      delay(100);
    }
  }
  if (myswitch[2] != last_switch[2]) {
    last_switch[2] = myswitch[2];
    if (myswitch[2] == HIGH) {
      MIDI.sendNoteOn(14, 5, 16);
      Serial.print("Button 2 pressed");
      TFTscreen.background(0, 0, 0);
      TFTscreen.text("B2 N14", 6, 57);
      delay(100);
    } else {
      MIDI.sendNoteOff(14, 0, 16);
      Serial.print("Button 2 off");
      delay(100);
    }
  }
  if (myswitch[3] != last_switch[3]) {
    last_switch[3] = myswitch[3];
    if (myswitch[3] == HIGH) {
      MIDI.sendNoteOn(15, 5, 16);
      Serial.print("Button 3 pressed");
      TFTscreen.background(0, 0, 0);
      TFTscreen.text("B3 N15", 6, 57);
      delay(100);
    } else {
      MIDI.sendNoteOff(15, 0, 16);
      Serial.print("Button 3 off");
      delay(100);
    }
  }
  if (myswitch[4] != last_switch[4]) {
    last_switch[4] = myswitch[4];
    if (myswitch[4] == HIGH) {
      MIDI.sendNoteOn(16, 5, 16);
      Serial.print("Button 4 pressed");
      TFTscreen.background(0, 0, 0);
      TFTscreen.text("B4 N16", 6, 57);
      delay(100);
    } else {
      MIDI.sendNoteOff(16, 0, 16);
      Serial.print("Button 4 off");
      delay(100);
    }
  }
  if (myswitch[5] != last_switch[5]) {  // Stops all Multipads
    last_switch[5] = myswitch[5];
    if (myswitch[5] == HIGH) {
      MIDI.sendNoteOn(17, 5, 16);
      Serial.print("Button 5 pressed");
      TFTscreen.background(0, 0, 0);
      TFTscreen.text("B5 N17", 6, 57);
      delay(100);
    } else {
      MIDI.sendNoteOff(17, 0, 16);
      Serial.print("Button 5 off");
      delay(100);
    }
  }
}

Thanks,

Martin

The serial Monitor detects the button presses but the screen displays only garbage not the expected text. :o

what is connected to pin 0 and 1?
where is MIDI connected?
what's the speed of the serial console?

  // Set MIDI baud rate:
  Serial.begin(9600);

Unusual.

Hi,

Thanks for the replies. :slight_smile:

Pin 0, RX is unconnected.

Pin 1, TX is connected to the midi connector via a 220 ohm resistor.

The serial monitor tool is set to 9600.

// Set MIDI baud rate:
Serial.begin(9600);

Is an oops!

I have attached a circuit diagram if it helps.

Martin

You can’t have two “devices” on Tx

Hi J-M-L,

Thanks for the reply.

I don't have 2 devices on TX as far as I can see?

The output only occurs when a switch is pressed, so that one of the input pins on the digital side goes high and sends a Midi signal as defined by the code.

I need to check that works by seeing if the serial output to the monitor tool is sending the correct parameters, that is to be able to see which button is pressed, (just 2 as a test).

The monitor screen just shows garbage as each switch is pressed, not Button 1 or 2.

Thanks,

Martin

Arnak:
I don’t have 2 devices on TX as far as I can see?

hum you said

Pin 1, TX is connected to the midi connector via a 220 ohm resistor.

and pin 1 is also connected through USB to your Serial console… so every Serial.print you do goes “both ways”…

Hi J-M-L,

OK, thanks I didn't realise that it would work that way. :confused:

So I disconnected pin 1 and tried again, the serial output to the monitor still displays garbage when a button is pressed. :o

I can't understand why that should be....

Also, the code should be sending Button off messages when no key is pressed but it isn't doing that either.

The serial screen is blank until a button is pressed.

** Update, I am going to try // on all the lines dealing with the midi output and the TFT screen to see if that makes the serial print work OK as it should.

** Update 2 Tried that but that stops the buttons working altogether, so it must be relying on the Midi instructions to see the buttons.

More work is required I think.

Martin

Post cleaned up code where you got rid of the TFT and describe exact wiring

Hi J-M-L,

Cleaned up code as requested.

// Mdidi Foot Switch Controller
// By Martin Shaw

#include <TFT.h>
#include <SPI.h>
#include <MIDI.h>
#include <SoftwareSerial.h>
MIDI_CREATE_DEFAULT_INSTANCE();


void setup()
{
 
  for (byte i = 0; i < 5; i++)  pinMode(2 + i, INPUT); // Set pins 2 - 6 to input
  MIDI.begin(16);
}

void loop()
{
  int myswitch [5];
  byte last_switch[5];

  // Loop to read switch input
  for (byte i = 0; i < 5; i++) myswitch[i] = digitalRead(2 + i);
  // Check for buttonj press 1 to 5
  if (myswitch[1] != last_switch[1]) {
    last_switch[1] = myswitch[1];
    if (myswitch[1] == HIGH) {  // When button pressed the voltage goes high
      MIDI.sendNoteOn(13, 5, 16);  // (note, velocity, channel)
  Serial.print("Button 1 pressed");
          delay(100);
       } else {
          delay(100);
    }
  }
  if (myswitch[2] != last_switch[2]) {
    last_switch[2] = myswitch[2];
    if (myswitch[2] == HIGH) {
      MIDI.sendNoteOn(14, 5, 16);
    Serial.print("Button 2 pressed");
         delay(100);
    } else {
         delay(100);
    }
  }
  if (myswitch[3] != last_switch[3]) {
    last_switch[3] = myswitch[3];
    if (myswitch[3] == HIGH) {
      MIDI.sendNoteOn(15, 5, 16);
     Serial.print("Button 3 pressed");
         delay(100);
    } else {
         delay(100);
    }
  }
  if (myswitch[4] != last_switch[4]) {
    last_switch[4] = myswitch[4];
    if (myswitch[4] == HIGH) {
      MIDI.sendNoteOn(16, 5, 16);
     Serial.print("Button 4 pressed");
         delay(100);
    } else {
         delay(100);
    }
  }
  if (myswitch[5] != last_switch[5]) {  // Stops all Multipads
    last_switch[5] = myswitch[5];
    if (myswitch[5] == HIGH) {
      MIDI.sendNoteOn(17, 5, 16);
    Serial.print("Button 5 pressed");
         delay(100);
    } else {
         delay(100);
    }
  }
}

The wiring diagram is available a few posts above as an attachment.

What I have concluded is that the easiest way to check the Midi output is to purchase a Midi to USB adapter cable so I can connect to my PC and use some software to see what is being transmitted.

That is just to avoid connecting it to my Yamaha Keyboard until I am satisfied that I have the code correct.

The switches appear to be working fine but I suspect they need de-bouncing. :frowning:

Thanks for the replies,

Martin

thanks for sharing - couple comments

  • arrays starts at 0, so you are should not access an element at index 5 (goes from 0 to 4)
  • as you have arrays, it’s too bad to do things 5 times manually when they look the same. look at this code:
// Mdidi Foot Switch Controller
// By Martin Shaw

#include <TFT.h>
#include <SPI.h>
#include <MIDI.h>
#include <SoftwareSerial.h>
MIDI_CREATE_DEFAULT_INSTANCE();


void setup()
{
  for (byte i = 0; i < 5; i++)  pinMode(2 + i, INPUT); // Set pins 2 - 6 to input
  MIDI.begin(16);
}

void loop()
{
  byte aSwitch;
  byte last_switch[5];

  // Loop to read and test switches input
  for (byte i = 0; i < 5; i++) {
    aSwitch = digitalRead(2 + i);
    if (aSwitch != last_switch[i]) {
      last_switch[i] = aSwitch;
      if (aSwitch == HIGH) {  // When button pressed the voltage goes high
        MIDI.sendNoteOn(13 + i, 5, 16); // (note, velocity, channel)
        Serial.print("Button ");
        Serial.print(i);
        Serial.println(" pressed");
        delay(100);
      } else {
        delay(100);
      }
    }
  }
}

(untested)

it feels shorter, doesn’t it ? :slight_smile:

PS: not sure about your delay(100); they will make the user interface not very reactive

// Mdidi Foot Switch Controller
// By Martin Shaw

#include <TFT.h>
#include <SPI.h>
#include <MIDI.h>

What’s an Mdidi foot switch?

PaulS:
What's an Mdidi foot switch?

it's a revolutionary invention I'm sure !! :slight_smile:

https://en.wiktionary.org/wiki/mdidi

mdidi

Chichewa

Noun
A confident step or loud stamping of the foot.

It means a light touch won't activate the switch, you must really step on it.

Hi J-M-L,

Thank you very much for the shortened code, that looks great. :slight_smile:

I agree the delay is a leftover from the else statement which once I have a debounce script will most likely not be needed.

For those wondering what a Midi footswitch is, it is used to enable control of musical instruments through Midi to control different functions on the instrument.

It saves having to use your hands to change settings when playing.

In my case, I have a Yamaha arranger keyboard with multiple buttons that change the way the instrument plays.

Such as infills, sound effects, style changes, etc, can be tricky playing a fast piece of music and touching the controls without messing up my playing.

A common problem for Theatre Organists who always wished the were an Octopus!

Martin

Arnak:
For those wondering what a Midi footswitch is, …

I think he was making fun of your spelling :smiley: :wink:

// Mdidi Foot Switch Controller
// By Martin Shaw

M[color=red]d[/color]idi

Hi J-M-L,

I tried your new routine and it works very well!!!!

Now I just need to add more switches.8-))

Thanks,

Martin

cool :slight_smile: