If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.
Special Thanks To:
Aaron Holmes
Aaron Wheeler
Adam Horden
Alan Scrimgeour
Andre
Andrew Haynes
Anonymous000
asabase
Austin Weil
barney
Barry
Bert Hickman
Bill Kukowski
Blitzorn
Brandon Paradelas
Bruce Bowling
BubeeMike
Byong Park
Cesiumsponge
Chris F.
Chris Hooper
Corey Worthington
Derek Woodroffe
Dalus
Dan Strother
Daniel Davis
Daniel Uhrenholt
datasheetarchive
Dave Billington
Dave Marshall
David F.
Dennis Rogers
drelectrix
Dr. John Gudenas
Dr. Spark
E.TexasTesla
eastvoltresearch
Eirik Taylor
Erik Dyakov
Erlend^SE
Finn Hammer
Firebug24k
GalliumMan
Gary Peterson
George Slade
GhostNull
Gordon Mcknight
Graham Armitage
Grant
GreySoul
Henry H
IamSmooth
In memory of Leo Powning
Jacob Cash
James Howells
James Pawson
Jeff Greenfield
Jeff Thomas
Jesse Frost
Jim Mitchell
jlr134
Joe Mastroianni
John Forcina
John Oberg
John Willcutt
Jon Newcomb
klugesmith
Leslie Wright
Lutz Hoffman
Mads Barnkob
Martin King
Mats Karlsson
Matt Gibson
Matthew Guidry
mbd
Michael D'Angelo
Mikkel
mileswaldron
mister_rf
Neil Foster
Nick de Smith
Nick Soroka
nicklenorp
Nik
Norman Stanley
Patrick Coleman
Paul Brodie
Paul Jordan
Paul Montgomery
Ped
Peter Krogen
Peter Terren
PhilGood
Richard Feldman
Robert Bush
Royce Bailey
Scott Fusare
Scott Newman
smiffy
Stella
Steven Busic
Steve Conner
Steve Jones
Steve Ward
Sulaiman
Thomas Coyle
Thomas A. Wallace
Thomas W
Timo
Torch
Ulf Jonsson
vasil
Vaxian
vladi mazzilli
wastehl
Weston
William Kim
William N.
William Stehl
Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Registered Member #2424
Joined: Tue Oct 06 2009, 08:02AM
Location:
Posts: 17
Right, ive sorted out the pulsewidth issue by using annother timer and interrupt. The sketch works pretty well now. Ive also added pulsewidth limiting.
You need the timer1 library from here And the MIDI library from here
Let me know if you have any issues with it :)
and if anyone can tell me why it struggles with e4 (Midi number 64 ~ 330Hz) id really apreciate it :p (I suspect it's because the interupt for the pulse width and the main timer are getting in each others way, i should probably swap the timers round as with the 16 bit timer1 i hopefully won't need the divider routine)
EDIT: CODE REMOVED, PLEASE USE IMPROVED CODE BELOW
Registered Member #2424
Joined: Tue Oct 06 2009, 08:02AM
Location:
Posts: 17
No problem at all :) but try this one.
Just had a total DUH! moment.
All problems solved with much simpler code:
You need the timer1 library from here And the MIDI library from here
If you want to be able to play higher notes you'll need to edit the second line that says "#define NOTELIM 70 //Highest MIDI note to output." to a higher number USE WITH CATION!
#define OUTPIN 9 //Output pin to drive coil.
#define NOTELIM 70 //Highest MIDI note to output.
#define PWLIM 300 //Pulse Width limit in us.
#define PWMULT 3 // multiplier applied to velocity to get pulse width.
//Include librarys.
#include <MIDI.h>
#include "TimerOne.h"
//Define Global Variables
unsigned int midiperiod[128]; //Array to store midi note periods
unsigned int pw = 20; //Caurrent Pulase width
void setup(void){
// Prepare Timer1
Timer1.initialize(1000000);
// Set the pin we want the ISR to toggle for output.
pinMode(OUTPIN,OUTPUT);
//Start up the serial port
Serial.begin(9600);
//Define Midi Note Periods
midiperiod[0] =122312;
midiperiod[1] =115447;
midiperiod[2] =108968;
midiperiod[3] =102852;
midiperiod[4] =97079;
midiperiod[5] =91631;
midiperiod[6] =86488;
midiperiod[7] =81634;
midiperiod[8] =77052;
midiperiod[9] =72727;
midiperiod[10] =68645;
midiperiod[11] =64793;
midiperiod[36] =15289;
midiperiod[37] =14431;
midiperiod[38] =13621;
midiperiod[39] =12856;
midiperiod[40] =12135;
midiperiod[41] =11454;
midiperiod[42] =10811;
midiperiod[43] =10204;
midiperiod[44] =9631;
midiperiod[45] =9091;
midiperiod[46] =8581;
midiperiod[47] =8099;
midiperiod[72] =1911;
midiperiod[73] =1804;
midiperiod[74] =1703;
midiperiod[75] =1607;
midiperiod[76] =1517;
midiperiod[77] =1432;
midiperiod[78] =1351;
midiperiod[79] =1276;
midiperiod[80] =1204;
midiperiod[81] =1136;
midiperiod[82] =1073;
midiperiod[83] =1012;
midiperiod[108] =239;
midiperiod[109] =225;
midiperiod[110] =213;
midiperiod[111] =201;
midiperiod[112] =190;
midiperiod[113] =179;
midiperiod[114] =169;
midiperiod[115] =159;
midiperiod[116] =150;
midiperiod[117] =142;
midiperiod[118] =134;
midiperiod[119] =127;
midiperiod[12] =61156;
midiperiod[13] =57724;
midiperiod[14] =54484;
midiperiod[15] =51426;
midiperiod[16] =48540;
midiperiod[17] =45815;
midiperiod[18] =43244;
midiperiod[19] =40817;
midiperiod[20] =38526;
midiperiod[21] =36364;
midiperiod[22] =34323;
midiperiod[23] =32396;
midiperiod[48] =7645;
midiperiod[49] =7215;
midiperiod[50] =6810;
midiperiod[51] =6428;
midiperiod[52] =6067;
midiperiod[53] =5727;
midiperiod[54] =5405;
midiperiod[55] =5102;
midiperiod[56] =4816;
midiperiod[57] =4545;
midiperiod[58] =4290;
midiperiod[59] =4050;
midiperiod[84] =956;
midiperiod[85] =902;
midiperiod[86] =851;
midiperiod[87] =804;
midiperiod[88] =758;
midiperiod[89] =716;
midiperiod[90] =676;
midiperiod[91] =638;
midiperiod[92] =602;
midiperiod[93] =568;
midiperiod[94] =536;
midiperiod[95] =506;
midiperiod[120] =119;
midiperiod[121] =113;
midiperiod[122] =106;
midiperiod[123] =100;
midiperiod[124] =95;
midiperiod[125] =89;
midiperiod[126] =84;
midiperiod[127] =80;
midiperiod[24] =30578;
midiperiod[25] =28862;
midiperiod[26] =27242;
midiperiod[27] =25713;
midiperiod[28] =24270;
midiperiod[29] =22908;
midiperiod[30] =21622;
midiperiod[31] =20408;
midiperiod[32] =19263;
midiperiod[33] =18182;
midiperiod[34] =17161;
midiperiod[35] =16198;
midiperiod[60] =3822;
midiperiod[61] =3608;
midiperiod[62] =3405;
midiperiod[63] =3214;
midiperiod[64] =3034;
midiperiod[65] =2863;
midiperiod[66] =2703;
midiperiod[67] =2551;
midiperiod[68] =2408;
midiperiod[69] =2273;
midiperiod[70] =2145;
midiperiod[71] =2025;
midiperiod[96] =478;
midiperiod[97] =451;
midiperiod[98] =426;
midiperiod[99] =402;
midiperiod[100] =379;
midiperiod[101] =358;
midiperiod[102] =338;
midiperiod[103] =319;
midiperiod[104] =301;
midiperiod[105] =284;
midiperiod[106] =268;
midiperiod[107] =253;
MIDI.begin(); // Launch MIDI with default options
}
void pulse(){
digitalWrite(OUTPIN,HIGH);
delayMicroseconds(pw);
digitalWrite(OUTPIN,LOW);
}
void loop(){
int controlnote;
//Check for availible midi data
if (MIDI.read()) {
switch(MIDI.getType()) { // Get the type of the message we caught
case NoteOn:
//get note on number and check for limit
controlnote = MIDI.getData1();
if (controlnote > NOTELIM){
controlnote = 10;
}
Timer1.attachInterrupt(pulse,midiperiod[controlnote]);
//Calculate Pulse width from velocity and Limit to PWLIM
pw = PWMULT * MIDI.getData2();
if (pw > PWLIM){
pw = PWLIM;
}
break;
case NoteOff:
//Disable the interupt.
Timer1.detachInterrupt();
break;
default:
break;
}
}
}
Registered Member #1900
Joined: Fri Jan 02 2009, 06:44PM
Location: Texas
Posts: 29
Thanks, this will be a good jump off point. I have the Diecimilanove Arduino and it will be good to try with it before trying another processor.
I am a little concerned. It sounds like you have tested this on a DRSSTC already... but the code could let the duty cycle get very high - just under 50% for the highest note (max velocity) allowed (90 on the MIDI table). If you sustain that note there could be a problem... right?
Registered Member #2424
Joined: Tue Oct 06 2009, 08:02AM
Location:
Posts: 17
Hi Angstrom, Thanks for pointing that out, you are correct, I wanted to be able to play the very high notes so left the note limit quite high (I use the sequencer to insure that the pulsewidth (velocity) is very low for the highest notes. I've edited the code above to make it safer for people to test with. At some point ill probably add a max duty cycle limit so the code automatically adjusts the pulsewidth for the highest notes.
Is it worth me making a projects thread for this with the circuit diagram etc?
Registered Member #1900
Joined: Fri Jan 02 2009, 06:44PM
Location: Texas
Posts: 29
By all means don't wait for permission.
I'm also a little confused here: if (controlnote > NOTELIM){ controlnote = 10; }
Did you mean to make it so it defaults to a lower note if the input exceeds the limit? The only reason I can think of is to be safe and let the operator know that it happened.
The code seems far too simple at first glance, but I guess that's something nice about this development board. Maybe you could post a video of it working?
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.