6

I am working on a project that uses a potentiometer and an ultrasonic sensor to create MIDI signals. This is my first time working with an arduino. I took a class in C++, but am teaching myself some C in order to program the arduino. So far I have been looking at code that has been written, I've been trying to understand it and mess with it so that my arduino works. The first code creates notes, but I have no control over what notes play or when they play. The first code:

#include <SoftwareSerial.h>

int val;
int AnalogValue = 0;    
byte note = 0;  
SoftwareSerial midiSerial(2, 3);

void setup () 
{

  pinMode(Pingpin, OUTPUT);
  Serial.begin(9600);
  midiSerial.begin(31250);
}


void loop()
{
  val = 0;
  digitalWrite(Pingpin, LOW);
  delayMicroseconds(2);
  digitalWrite(Pingpin, HIGH);
  delayMicroseconds(5);
  digitalWrite(Pingpin, LOW);

  AnalogValue = analogRead(0);
  note = AnalogValue/8;

  pinMode(Pingpin, INPUT); 
  val = digitalRead(Pingpin); 
  while(val == LOW) 
  { 
    noteOn(val, note, 0x40);
  }
  while(val == HIGH) 
  {
     noteOn(val, note, 0x40);       

  }

}
void noteOn(byte cmd, byte data1, byte  data2) 
{
    midiSerial.write(cmd);
   midiSerial.write(data1);
   midiSerial.write(data2);
}  

The second code makes more sense to me, but I get no notes to play. I am lost, and can't seem to get the second code to work. Second code:

#include <SoftwareSerial.h>

int val; 
int timecount; 
int AnalogValue;  
int ultrasoundValue; 
const int Pingpin = 8;

byte note = 0;  

SoftwareSerial midiSerial(2, 3); 

void setup ()
{

  pinMode(Pingpin, OUTPUT); 
  Serial.begin(9600);
  midiSerial.begin(31250);
}
void loop()
{
  AnalogValue = analogRead(0); 
  note = AnalogValue/8; 
  Serial.print(note,HEX);
  Serial.print("\t");

  timecount = 0; 
  val = 0;  
  digitalWrite(Pingpin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(Pingpin, HIGH);
  delayMicroseconds(5);  
  digitalWrite(Pingpin, LOW); 

  pinMode(Pingpin, INPUT); 
  val = digitalRead(Pingpin); 
  while(val == LOW) 
  { 
   val = digitalRead(Pingpin);
  }

  while(val == HIGH)
  { 
   val = digitalRead(Pingpin);
   timecount++;       
  }

   ultrasoundValue = timecount; 
   ultrasoundValue = ultrasoundValue - 14; 
   ultrasoundValue = ultrasoundValue / 5;

  Serial.print(ultrasoundValue);

if(ultrasoundValue > 127)   
 {

 }

else 
  {

   noteOn(ultrasoundValue, note, 0x40); 

  }





}
void noteOn(byte cmd, byte data1, byte  data2)   
{
   midiSerial.write(cmd);
   midiSerial.write(data1);
   midiSerial.write(data2);
}  
3

There is quite a bit that needs to be addressed for these programs to work, and I don't know too much about midi, but i'll use the arduino midi tutorial for info. http://arduino.cc/en/Tutorial/Midi?from=Tutorial.MIDI

The first code will take one measurement of an analog value, set it to note, make one digital reading of pingPin, set that to val, and then write noteOn() forever afterwards. The second code will continually make analog and Ping readings and then write those to serial if the ping reading is greater than 127.

Infinite loop aside, Your programs don't work largely because they send the sensor data in the wrong ways. The first byte sent to noteOn is a command byte, it should not contain sensor data. the arduino tutorial uses the command 0x90 to control the note being played on channel one. the second byte is the note and the third byte is velocity, which seems to work like volume.

to set the pitch and volume of channel one, you need to repeatedly send

noteOn( 0x90, note, volume); 

If you want to play a note based on the analog readings that both of your codes set to "note" only when the ping value is larger than 127, you need something like this

if(ultrasoundValue > 127){
    noteOn(0x90, note, 0x45); //middle volume according to arduino tutorial
} else {
    noteOn(0x90, note, 0x00); //make the note silent by using 0 volume
}

if you instead wanted to control the pitch with the ultrasoundValue, you would need to scale the value to being within 0-255, and then use that as note instead of the analog reading. You can get much more complicated than this, but you will need to find a reference for the MIDI protocol you can use.

  • Thank you for the info, I was able to read more on MIDI. I have a good grasp on how these messages are composed. I still have not solved the main problem. I have a MIDI cable that flashes when MIDI signals are being sent. When I upload code #1 the light flashes, but when I make any adjustments to the code and upload the a modified version, the light stops blinking. I printed the value of UltrasoundValue and it is around 600, so the ping sensor is working. I don't know why it won't go into the "if" statement. – EarleAB Jul 9 '14 at 20:20
  • 1
    I recommend you start with the code from the arduino tutorial and then very slowly hack on that until it does what you need. Make changes one at a time so you can isolate which change makes the code stop working. – BrettAM Jul 10 '14 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.