serial print - extra characters

I have a niggling problem using serial.print. For example, the code

Serial.println("Button HIGH ");

produces c Button High

and

Serial.print("variable_CC_val = "); Serial.println(variable_CC_val)

produces b"variable_CC_val = 218

I set the baud rate with

Serial.begin(9600);

It doesn't affect the functioning of the code, but it's really annoying me - any clues?

If only we could see your code {sigh}

Sorry, I thought it would get in the way. here’s the code, it’s an ultrasonic midi controller. As I said, the code works, it’s just the serial output that isn’t correct. I was using serial print for debugging purposes :wink:

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

int current;
int state = 0;
int latched = 0;
int buttonState = 0;         // variable for reading the pushbutton status
long duration;
int distance;
int variable_CC_val;

const int CC_num = 99;      // CC for on/off
const int CC_num_2 = 98;    // CC for "theremin"
const int maxi = 20;        // max value for hand distance
const int midi_channel = 11;
const int LEDpin = 13 ;
const int switchPin = 4;     // the number of the switch pin
const int trigPin = 3;        // sensor read
const int echoPin = 2;        // sensor send

void setup() {
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); 
}

void loop() 
  {
  ReadDistance();  
  buttonState = digitalRead(switchPin);
  if (buttonState == HIGH) 
   {
    if (state == 0 && distance > maxi)
       {
       state = 1; // hand swipe has started
       }
    if (state == 1 && distance < maxi) // swipe over
       {
       state = 0;  
       if (latched == 1)
         {
         MIDI.sendControlChange(CC_num,0,midi_channel);
         latched = 0;
         digitalWrite(LEDpin, HIGH);
         Serial.println("Button HIGH ");
         }
      else 
         {
         MIDI.sendControlChange(CC_num,127,midi_channel);
         digitalWrite(LEDpin, LOW);
         latched = 1;
           Serial.println("Button LOW ");
         }
      }   // end value low
    }
  else 
    {
     if (distance > 34)  // ignore anything above this range
        {
        distance=34;
        }
     variable_CC_val = (distance * 4)-10;
     if (distance < 0)  // ignore anything below zero
        {
        distance=0;
        }     
     MIDI.sendControlChange(CC_num_2,distance,midi_channel);
     Serial.print("variable_CC_val = ");
     Serial.println(variable_CC_val);
    }  

     delay(200);   
} // end main loop


void ReadDistance()
{
  digitalWrite(trigPin, LOW);   // Clears the trigPin
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034/2;
}

Which pins is the output to MIDI connected to ?

UKHeliBob: Which pins is the output to MIDI connected to ?

I'd say that you probably win the prize, Bob. :) :- (Well spotted.)

This library enables MIDI I/O communications on the Arduino [u]serial ports[/u].

@Nick, I think you need to look at this in the "MIDI" library documentation:-

To specify which serial port to use:

MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, MIDI);

You will have trouble with an UNO which only has one serial port. If you use an Leonardo or Micro you can use Serial (USB) for Serial Monitor and Serial1 (Pins 0 and 1) for MIDI. If you use a MEGA you can use Serial1, Serial2, or Serial3 for MIDI.

johnwasser: You will have trouble with an UNO which only has one serial port. If you use an Leonardo or Micro you can use Serial (USB) for Serial Monitor and Serial1 (Pins 0 and 1) for MIDI. If you use a MEGA you can use Serial1, Serial2, or Serial3 for MIDI.

The library provides support for 'SoftwareSerial' as well, as of February 2014:-

13/02/2014 : Version 4.0 released. Moved to GitHub, added multiple instances & software serial support, and a few bug fixes.

I just copied that quote in my last post directly from the documentation. I didn't go so far as to alter it for SoftwareSerial, because I didn't know which board he has, and thought he might have a board with multiple hardware serial ports like a Mega2560 etc.

How stupid of me. Of course, it's the midi command. When the circuit logic is tested "virtually" using the comments, I switch the baud rate so it works for midi and remove the serial print output.

Doh & apologies...

Actually, there's one more thing you might be able to advise on.

The code reads values from an ultrasonic sensor. What I'd like to do is have a button that would "freeze" the value until the button was pressed again. Would this be best using an interrupt, or just adding another "if" statement?

Would this be best using an interrupt,

No. When the button [u]becomes[/u] pressed change the state of a boolean variable. Read the ultrasonic sensor only when the boolean variable is true (or false, you choose)

The StateChangeDetection example in the IDE will show you the principle.