This was a continuation from the end of my last post and thought it best to start another.
Ok, I have added the ability to output the midi notes from the chosen array to my original tinkered sketch. This works fine with the audible notes and the midi notes playing for each of the steps in the sequence. However, while I am able to change the pitch of the audible notes through the AnalogInFrequency pot for each of the steps within the "if" statements, I can't seem to do so for the midi notes. What have I missed? I really appreciate all this help, I swear I'm learning but I'm stumped again!
/* ======================================================================
Arduino Punk Console
A simple programmable 8 step tone sequencer
by dano/beavisaudio.com
Revs
-----------------------------------
15 Sept djh initial version
======================================================================*/
// Map all the input and output pins
#define AnalogInFrequency 1
#define AnalogInTempo 2
#define AnalogInDuration 0
#define DigitalOutSignal 11
#define DigitalInSwitch0 2
#define DigitalInSwitch1 3
#define DigitalInSwitch2 4
#define DigitalInSwitch3 5
#define DigitalInStartStop 10
#define DigitalOutLED 12
// Set up the array for each step
int steps[] = {49,55,66,74};
int mynotes[] = {49,55,66,74,82,98,110,131,147,165,196,220,262,294,330,
392,440,523,587,659,784,880,1047,1175,1319};
int mymidi[] = {31,33,36,38,40,43,45,48,50,52,55,57,60,62,64,67,69,72,74,
76,79,81,84,86,88};
// misc housekeeping
int duration = 50;
int pitchval = 1;
int fPlayMode = true;
int lastPushedStep = -1;
// Initialize the tempo
int tempo = 100;
void setup()
{
// initialize the digital pin as an output:
for(int i = 0; i <= 4; i++)
// setup pin modes (Digital pins are input by default, but
// I like to set 'em explicitly just so the code is clear.
pinMode (DigitalInSwitch0, INPUT);
pinMode (DigitalInSwitch1, INPUT);
pinMode (DigitalInSwitch2, INPUT);
pinMode (DigitalInSwitch3, INPUT);
pinMode (DigitalInStartStop, INPUT);
pinMode (DigitalOutSignal, OUTPUT);
pinMode (DigitalOutLED, OUTPUT);
Serial.begin(31250);
}
void loop()
{
// Main sequence loop
for (int i=0; i<4; i++)
{
// Are we playing or stopping?
fPlayMode = digitalRead (DigitalInStartStop);
digitalWrite (DigitalOutLED, HIGH);
noteOn(0x90, mymidi[i], 0x40);
// Check the Hardware
readSwitches();
readPots();
// Make the noise
if (fPlayMode)
{
freqout (steps[i], duration);
noteOn(0x90, mymidi[i], 0x00);
}
digitalWrite (DigitalOutLED, LOW);
//digitalWrite(ledpin[i], LOW);
// Pause between steps
delay (tempo);
}
}
// Read the current values of the pots, called from the loop.
void readPots ()
{
tempo = (analogRead (AnalogInTempo) * 1.9);
duration = (analogRead (AnalogInDuration));
}
// Read the current values of the switches and
// if pressed, replace the switch's slot frequency
// by reading the frequency pot.
void readSwitches()
{
// reset last pushed button number
lastPushedStep = -1;
// check switch 0, if pressed, get the current freq into step 0, etc. etc.
int val = analogRead(AnalogInFrequency);
int index = map(val, 0, 1024, 0, 20);
int myNote = mynotes[index];
int val2 = analogRead(AnalogInFrequency);
int value = map(val2, 0, 1024, 0, 20);
int myMidi = mymidi[value];
if (digitalRead (DigitalInSwitch0) == HIGH)
{
steps[0] = myMidi;
steps[0] = myNote;
lastPushedStep = 1;
}
else if (digitalRead (DigitalInSwitch1) == HIGH)
{
steps[1] = myMidi;
steps[1] = myNote;
lastPushedStep = 2;
}
else if (digitalRead (DigitalInSwitch2) == HIGH)
{
steps[2] = myMidi;
steps[2] = myNote;
lastPushedStep = 3;
}
else if (digitalRead (DigitalInSwitch3) == HIGH)
{
steps[3] = myMidi;
steps[3] = myNote;
lastPushedStep = 4;
}
}
//freqout code by Paul Badger
// freq - frequency value
// t - time duration of tone
void freqout(int freq, int t)
{
int hperiod; //calculate 1/2 period in us
long cycles, i;
// subtract 7 us to make up for digitalWrite overhead - determined empirically
hperiod = (500000 / ((freq - 0) * pitchval));
// calculate cycles
cycles = ((long)freq * (long)t) / 1000; // calculate cycles
for (i=0; i<= cycles; i++)
{ // play note for t ms
digitalWrite(DigitalOutSignal, HIGH);
delayMicroseconds(hperiod);
digitalWrite(DigitalOutSignal, LOW);
delayMicroseconds(hperiod - 1); // - 1 to make up for fractional microsecond in digitaWrite overhead
}
}
// plays a MIDI note. Doesn't check to see that
// cmd is greater than 127, or that data values are less than 127:
void noteOn(byte cmd, byte data1, byte data2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}
:)thanks