Hi , i'm struggling a bit trying to get the Teensy 3.2 to trigger a sensor. I'm using an optical slotted sensor H22A1. This operates by blocking the slot, it switches the on state to an off. The other sensor in use is a pressure sensor, this part works fine with the code listed below. The optical sensor is giving me grief. I have recently swapped over from using the arduino to the Teensy. I managed to get this working using the Arduino.
The idea is to use the pressure sensor to deliver the expression values, then this will be used alongside the optical sensor which will trigger midi notes on or off.
I'm new to the coding and what I have here is bits and pieces from other examples. Can anyone see where i'm going wrong. Cheers
The outlook is to have twenty optical sensors available to play alike the harmonica. Any help will be welcome.
(some of this code is taken from Gordophone website regards his breath instrument........Cheers for that)
#define EXPRESSION_CONTROLLER 11
#define MIDI_CHANNEL 1
// Send continuous controller message no more than
// every CC_INTERVAL milliseconds
#define CC_INTERVAL 20
// Only send CC data if the pressure sensor reading
// a value larger than this.
#define BREATH_THRESHOLD 110
// The last time we sent a CC value
unsigned long ccSendTime = 0L;
// The value read from the sensor
int sensorValue;
// The CC value we will send
int ccVal;
// The last CC value we sent
int lastCcVal = 0;
int buttonPin = 7; // Slot sensor input
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
void setup() {
pinMode( 7,INPUT_PULLUP);// set the pin as INPUT /// also tried putting this to six in case 0 was one and six was seven input
// Nothing to initialize for this sketch
}
void loop() {
if (millis() - ccSendTime > CC_INTERVAL) { // Only read the sensor if enough time has passed
sensorValue = analogRead(A0); // read the input on analog pin 0
if (sensorValue > BREATH_THRESHOLD)
{
// Map the value, which may range from BREATH_THRESHOLD
// to 1023, to a value in the range 0 to 127, which is
// the valid range for a MIDI continuous controller
ccVal = lastCcVal = map(sensorValue, BREATH_THRESHOLD, 1023, 0, 127);
// And send the value as a MIDI CC message
usbMIDI.sendControlChange(EXPRESSION_CONTROLLER, ccVal, MIDI_CHANNEL);
ccSendTime = millis();
delay(2);
}
else if (lastCcVal > 0)
{
// The pressure has just dropped below the threshold, so
// send a CC value of zero
usbMIDI.sendControlChange(EXPRESSION_CONTROLLER, 0, MIDI_CHANNEL);
ccSendTime = millis();
lastCcVal = 0;
delay(2);
currentState = digitalRead(buttonPin);
if (currentState == HIGH && lastState == LOW){//if button has just been pressed
usbMIDI.sendNoteOn(60, ccVal, MIDI_CHANNEL);//turn note 60 on with 127 velocity // I used ccVal as the velocity to register the values from the press sensor
delay(2);//crude form of button debouncing
} else if(currentState == LOW && lastState == HIGH){
usbMIDI.sendNoteOff(60, ccVal, MIDI_CHANNEL);//turn note 60 off
delay(2);//crude form of button debouncing
}
lastState = currentState;
//Serial.println(val);
}
}
}
The idea is to use the pressure sensor to deliver the expression values, then this will be used alongside the optical sensor which will trigger midi notes on or off.
I'm new to the coding and what I have here is bits and pieces from other examples. Can anyone see where i'm going wrong. Cheers
The outlook is to have twenty optical sensors available to play alike the harmonica. Any help will be welcome.
(some of this code is taken from Gordophone website regards his breath instrument........Cheers for that)
#define EXPRESSION_CONTROLLER 11
#define MIDI_CHANNEL 1
// Send continuous controller message no more than
// every CC_INTERVAL milliseconds
#define CC_INTERVAL 20
// Only send CC data if the pressure sensor reading
// a value larger than this.
#define BREATH_THRESHOLD 110
// The last time we sent a CC value
unsigned long ccSendTime = 0L;
// The value read from the sensor
int sensorValue;
// The CC value we will send
int ccVal;
// The last CC value we sent
int lastCcVal = 0;
int buttonPin = 7; // Slot sensor input
boolean currentState = LOW;//stroage for current button state
boolean lastState = LOW;//storage for last button state
void setup() {
pinMode( 7,INPUT_PULLUP);// set the pin as INPUT /// also tried putting this to six in case 0 was one and six was seven input
// Nothing to initialize for this sketch
}
void loop() {
if (millis() - ccSendTime > CC_INTERVAL) { // Only read the sensor if enough time has passed
sensorValue = analogRead(A0); // read the input on analog pin 0
if (sensorValue > BREATH_THRESHOLD)
{
// Map the value, which may range from BREATH_THRESHOLD
// to 1023, to a value in the range 0 to 127, which is
// the valid range for a MIDI continuous controller
ccVal = lastCcVal = map(sensorValue, BREATH_THRESHOLD, 1023, 0, 127);
// And send the value as a MIDI CC message
usbMIDI.sendControlChange(EXPRESSION_CONTROLLER, ccVal, MIDI_CHANNEL);
ccSendTime = millis();
delay(2);
}
else if (lastCcVal > 0)
{
// The pressure has just dropped below the threshold, so
// send a CC value of zero
usbMIDI.sendControlChange(EXPRESSION_CONTROLLER, 0, MIDI_CHANNEL);
ccSendTime = millis();
lastCcVal = 0;
delay(2);
currentState = digitalRead(buttonPin);
if (currentState == HIGH && lastState == LOW){//if button has just been pressed
usbMIDI.sendNoteOn(60, ccVal, MIDI_CHANNEL);//turn note 60 on with 127 velocity // I used ccVal as the velocity to register the values from the press sensor
delay(2);//crude form of button debouncing
} else if(currentState == LOW && lastState == HIGH){
usbMIDI.sendNoteOff(60, ccVal, MIDI_CHANNEL);//turn note 60 off
delay(2);//crude form of button debouncing
}
lastState = currentState;
//Serial.println(val);
}
}
}