thanks for the help - sometimes I just miss the little details and remain totally blind to them.
I actually ended up going about writing the code in a different way, just because the way that I was sending the midi data did not work (i.e. when I wanted to send both midi CC and NoteON / OFF ). The new code (as can be seen below) works great, but i have reached a new stumbling block - as is usually the case.
I need the code to send a single NotON event when the rpm count goes over 120, this triggers a midi sequence of av events (Ableton and Modul8), however as the rpm count is constatly feeding out data the code continually sends the NoteON message for as long as the rpm count is above 120. Is there a way to make it so that it sends just one noteON event when the rpm count goes over 120 first time?
any help is greatly appreciated
/*
* Optical Tachometer
*
* Uses an IR LED and IR phototransistor to implement an optical tachometer.
* The IR LED is connected to pin 13 and ran continually. A status LED is connected
* to pin 12. Pin 2 (interrupt 0) is connected across the IR detector.
*
*
*/
//#define DEBUG
int ledPin = 13; // IR LED connected to digital pin 13
int statusPin = 12;
int lightPin = 11;// LED connected to digital pin 12
int brightness = 0;
int delay_time = 40; // delay for this amount each write cycle.
int noteDown = LOW;
byte MIDI_channel = 0;
byte cc_number = 0;
byte incomingByte;
byte button;
byte note;
byte printing_byte = 0;
int Value = 0;
int midi_pitch = 0;
int mappedrpm = 0;
int state=0;
#ifdef DEBUG
const int DEBUG_RATE = 9600; // Serial debugging communicates at 9600 baud
const int SERIAL_PORT_RATE = DEBUG_RATE;
#else
const int MIDI_BAUD_RATE = 31250; // MIDI communicates at 31250 baud
const int SERIAL_PORT_RATE = MIDI_BAUD_RATE;
#endif
volatile byte rpmcount;
volatile int status;
unsigned int rpm;
unsigned long timeold;
void rpm_fun()
{
//Each rotation, this interrupt function is run twice, so take that into consideration for
//calculating RPM
//Update count
rpmcount++;
//Toggle status LED
if (status == LOW) {
status = HIGH;
}
else {
status = LOW;
}
digitalWrite(statusPin, status);
}
void setup()
{
state = 0;
Serial.begin(SERIAL_PORT_RATE);
//Interrupt 0 is digital pin 2, so that is where the IR detector is connected
//Triggers on FALLING (change from HIGH to LOW)
attachInterrupt(0, rpm_fun, FALLING);
//Turn on IR LED
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
//Use statusPin to flash along with interrupts
pinMode(statusPin, OUTPUT);
rpmcount = 0;
rpm = 0;
timeold = 0;
status = LOW;
}
void loop()
{
//Update RPM every second
delay(800);
//Don't process interrupts during calculations
detachInterrupt(0);
//Note that this would be 60*1000/(millis() - timeold)*rpmcount if the interrupt
//happened once per revolution instead of twice. Other multiples could be used
//for multi-bladed propellers or fans
rpm = 3*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
attachInterrupt(0, rpm_fun, FALLING);
brightness = map(rpm, 0, 50, 0, 255);
analogWrite(lightPin, brightness);
printing_byte = map(rpm, 0, 50, 0, 127);{
midiOUT(0xB0, 7, printing_byte);
}
if(printing_byte >= 120){
midiOUT(0x90, 65, 127);
}
}
//Write it out to serial port
//Restart the interrupt processing
void midiOUT(char command, char value1, char value2) {
Serial.print(command, BYTE);
Serial.print(value1, BYTE);
Serial.print(value2, BYTE);
}