Actually my original post goes back to the end of April, but I kept posting on the same link so I didn't have to go all over again with the explanation, maybe another mistake of mine.
Anyway, I guess I am being a bit slow but what I would like to achieve is having "two reactions from one action". I mean, hit the piezo = trigger loop + turn on LED.
Is that still a matter of using delay() ?
So far the bit of code I wrote makes the LED go on as soon as I upload the sketch and independently I can still trigger the loops in LIVE9. But I cannot work out how to make the two action related...
My LEDs are connected to analog Pin A5, A4, A3, A2, A1 (although, so far I only tried working with A5 & A4 to keep it simple)
I think I should state a value to tell the program the state of the trigger but I cannot figure it out on my own.
All the example I have looked at were related to LEDs being switched on by buttons where you can use the values 0 and 1 to determine the state PRESSED or non-PRESSED of the button. But I realised it doesn't really work for a piezo since in works with electrical signals that vary depending on the intensity of the hit on the sensor. (is that right?)
I am coping here below the code again for you to have an idea of what I am trying to write
// what midi channel we're sending on
// ranges from 0 15
#define midichannal 9 // Equates to MIDI Channel 10
#define midichannal 10 // Equates to MIDI Channel 11
#define midichannal 11 // Equates to MIDI Channel 12
#define midichannal 12
#define midichannal 13
// general midi drum notes
#define note_F#
#define note_D
#define note_A
#define note_G
#define note_F
// define the pins we use
#define switchAPin 12
#define switchBPin 11
#define switchCPin 10
#define switchDPin 9
#define switchEPin 8
#define ledPin 13 // for midi out status
int switchAState = LOW;
int switchBState = LOW;
int switchCState = LOW;
int switchDstate = LOW;
int switchEstate = LOW;
int ledPinA = A5; //LED connected to analog Pin 5
int ledPinB = A4; //LED connected to analog Pin 4
int currentSwitchState = LOW;
int val,t;
void setup() {
pinMode(switchAPin, INPUT);
pinMode(switchBPin, INPUT);
pinMode(switchCPin, INPUT);
pinMode(switchDPin, INPUT);
pinMode(switchEPin, INPUT);
pinMode(A5, OUTPUT);
pinMode(A4, OUTPUT);
digitalWrite(switchAPin, HIGH); // turn on internal pullup
digitalWrite(switchBPin, HIGH); // turn on internal pullup
digitalWrite(switchCPin, HIGH); // turn on internal pullup
digitalWrite(switchDPin, HIGH);
digitalWrite(switchEPin, HIGH);
//Turn on LED
digitalWrite(A5, HIGH);
digitalWrite(A4, HIGH);
// set MIDI baud rate
Serial.begin(57600);
}
void loop() {
// deal with switchA
currentSwitchState = digitalRead(switchAPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x1E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x1E, 0);
switchAState = currentSwitchState;
{
//read state of the piezo
currentSwitchState = digitalRead(switchAPin);
//check if piezo is pressed
//if so, the buttonState is HIGH
if(currentSwitchState == LOW);
//turn on the LED
digitalWrite(A5, HIGH);
}
currentSwitchState = digitalRead(switchBPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x2E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x2E, 0);
switchBState = currentSwitchState;
{
//read state of the piezo
currentSwitchState = digitalRead(switchBPin);
//check if piezo is pressed
//if so, the buttonState is HIGH
if(currentSwitchState == HIGH);
//turn on the LED
digitalWrite(A4, HIGH);
}
currentSwitchState = digitalRead(switchCPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x3E, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x3E, 0);
switchCState = currentSwitchState;
currentSwitchState = digitalRead(switchDPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x4A, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x4A, 0);
switchCState = currentSwitchState;
currentSwitchState = digitalRead(switchEPin);
if( currentSwitchState == LOW && switchAState == HIGH ) // push
noteOn(0x90, 0x5B, 127);
if( currentSwitchState == HIGH && switchAState == LOW ) // release
noteOff(0x90, 0x5B, 0);
switchCState = currentSwitchState;
// Send a MIDI note on message. Like pressing a piano key
// channel ranges from 0 15
}
void noteOn(byte channel, byte note, byte velocity) {
midiMsg( (0x90 | channel), note, velocity);
}
// Send a MIDI note off message. Like releasing a piano key
void noteOff(byte channel, byte note, byte velocity) {
midiMsg( (0x80 | channel), note, velocity);
}
// Send a general MIDI message
void midiMsg(byte cmd, byte data1, byte data2) {
digitalWrite(ledPin,HIGH); // indicate we're sending MIDI data
Serial.write(byte(cmd));
Serial.write(byte(data1));
Serial.write(byte(data2));
digitalWrite(ledPin,LOW);
}
I have attempted only with the first functions related to switchAPin and switchBPin so far.
Hope it makes sense.
thanks again
Regards