Hi Jen,
Try using this code below.
It's basically the SIDaster SW with a note on command inserted in the main loop. It's working fine on my SIDaster and should generate a mellow sounding triangle signal, with fast attack and slow release.
Use the SHield V4 pushbutton as in the schematic to trigger note on/off by pressing it.
One key thing here : the SIDaster should not be powerfull enough to drive a speaker. Try plugin it to your soundcard line-in instead, this is what i'm doing and it works great.
1 - Look for the global Synth init section and update the registers as follow
2 - Go to the main loop and add the code below the midi call
//==============================
// GLOBAL SYNTH VARIABLES INIT
//==============================
// Coarse Frequency, base 32, value from 0 (-16) to 31 (+15)
static byte coarse1 = 16;
static byte coarse2 = 16;
static byte coarse3 = 16;
// Fine Frequency, base 0 value from 0 to 127
static byte fine1 = 0;
static byte fine2 = 0;
static byte fine3 = 0;
// Default midi channel
static byte mchannel = 1;
// Synth State Managment
static boolean trigged = false; // indicating if synth is playing
static boolean retrigger = true; // indicating if retriggering ADSR is on
// Local Osc Frequency
// Registers @ B00000 and B00001
// Default 440hz = 0x1CD5
static word freq1 = 0x1CD5;
static word freq2 = 0x1CD5;
static word freq3 = 0x1CD5;
// PWM
// Default Square Wave = 0x800
// Formula : PWout = (PWn/40.95) %
static word pw1 = 0x800;
static word pw2 = 0x800;
static word pw3 = 0x800;
// Control Register
// Default: Saw
// Content NOI|SQR|SAW|TRI|TST|MOD|SYN|GAT
static byte ctrl1 = 0X10;
static byte ctrl2 = 0X00;
static byte ctrl3 = 0X00;
// Attack and Decay
// Default : Shortest
static byte ad1 = 0x40;
static byte ad2 = 0x80;
static byte ad3 = 0xF0;
// Sustain and Release
// Default: Max sustain and no release
static byte sr1 = 0xF8;
static byte sr2 = 0xF0;
static byte sr3 = 0xF0;
// Cutoff Frequency
static word freq = 0x7FF;
// Resonnance and CTRL
// Res= 0 no resonnance, Res=F max resonnance
// Routing 0 is no filter, 1 is filter
// RES(4)|EXT|F3|F2|F1
static byte res = 0x00;
// Mode
// Content EXT|HP|BP|LP|VOL(4)
static byte mode = 0x0F;
// Mono Note Memory
static byte last_note = 0;
static byte current_note = 0;
static byte notes = 0;
// ########################################################
SID sid;
// Give voices some initial params
//
void synth_init()
{
// Freq init
sid.send(SID_FREQ1LO,char(freq1));
sid.send(SID_FREQ1HI,char(freq1>>8));
sid.send(SID_FREQ2LO,char(freq2));
sid.send(SID_FREQ2HI,char(freq2>>8));
sid.send(SID_FREQ3LO,char(freq3));
sid.send(SID_FREQ3HI,char(freq3>>8));
// PWM Init
sid.send(SID_PW1LO,char(pw1));
sid.send(SID_PW1HI,char(pw1>>8));
sid.send(SID_PW2LO,char(pw2));
sid.send(SID_PW2HI,char(pw2>>8));
sid.send(SID_PW3LO,char(pw3));
sid.send(SID_PW3HI,char(pw3>>8));
// CTRL
sid.send(SID_CTRL1,ctrl1);
sid.send(SID_CTRL2,ctrl2);
sid.send(SID_CTRL3,ctrl3);
// ADSR
sid.send(SID_AD1,ad1);
sid.send(SID_SR1,sr1);
sid.send(SID_AD2,ad2);
sid.send(SID_SR2,sr2);
sid.send(SID_AD3,ad3);
sid.send(SID_SR3,sr3);
// Filter + Main
sid.send(SID_FCLO,char(freq));
sid.send(SID_FCHI,char(freq>>3));
sid.send(SID_RES,res);
sid.send(SID_MODE,mode);
}
// Play sound at frequency specified on voice specified
//
void synth_on(int note)
{
freq1=NOTES[note+coarse1-16]+fine1;
sid.send(SID_FREQ1LO,char(freq1));
sid.send(SID_FREQ1HI,char(freq1>>8));
freq2=NOTES[note+coarse2-16]+fine2;
sid.send(SID_FREQ2LO,char(freq2));
sid.send(SID_FREQ2HI,char(freq2>>8));
freq3=NOTES[note+coarse3-16]+fine3;
sid.send(SID_FREQ3LO,char(freq3));
sid.send(SID_FREQ3HI,char(freq3>>8));
ctrl1|=1;
sid.send(SID_CTRL1,ctrl1);
ctrl2|=1;
sid.send(SID_CTRL2,ctrl2);
ctrl3|=1;
sid.send(SID_CTRL3,ctrl3);
}
void synth_off(int note)
{
ctrl1&=~1;
sid.send(SID_CTRL1,ctrl1);
ctrl2&=~1;
sid.send(SID_CTRL2,ctrl2);
ctrl3&=~1;
sid.send(SID_CTRL3,ctrl3);
}
void setup()
{
pinMode(LED,OUTPUT);
pinMode(A5,INPUT);
digitalWrite(LED,HIGH);
delay(1000);
// Set up the SID chip
sid.clk_setup();
sid.SPI_setup();
// Give the voices some initial values
// Set up the MIDI input
MIDI.begin();
MIDI.setHandleNoteOn(DoHandleNoteOn);
MIDI.setHandleNoteOff(DoHandleNoteOff);
MIDI.setHandleControlChange(DoHandleControlChange);
synth_init();
digitalWrite(LED,LOW);
}
// Main program loop
//
void loop() {
MIDI.read();
if (!digitalRead(A5))
{ synth_on(40);
digitalWrite(LED,HIGH);
} else {
synth_off(40);
digitalWrite(LED,LOW);
}
}