Hey guys, I feel dumb not being able to figure this out but my knowledge of coding is very minimal so I'm kind of just scrapping things together.
I have an LED controller with 5 touchpad triggers. I want it to light up when I press and hold the trigger, but only send one midi note. Right now I have a loop but it makes the LED strobe and send a new midi note every time the loop refreshes. Basically I need the note to be sent once, and the LED to hold as long as my finger stays on the touch pad. Here is the code I have now. Thank youuuu
#include <Adafruit_NeoPixel.h>
#ifdef AVR
#include <avr/power.h>
#endif
#include <Wire.h>
#include "Adafruit_MPR121.h"
Adafruit_MPR121 cap = Adafruit_MPR121();
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
#define PIN 6
#define NUMPIXELS 77
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
#include <MIDI.h>
int n1 = 2;
int n2 = 3;
int n3 = 4;
int n4 = 5;
int n5 = 7;
void setup() {
pixels.begin(); // This initializes the NeoPixel library.
Serial.begin(115200);
pinMode(n1,OUTPUT);
pinMode(n2,OUTPUT);
pinMode(n3,OUTPUT);
pinMode(n4,OUTPUT);
pinMode(n5,OUTPUT);
if (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?");
while (1);
}
for(uint8_t j=0;j<77;j++){
pixels.setPixelColor(j, pixels.Color(0,150,120)); // Moderately bright green color.
}
pixels.show(); // This sends the updated pixel color to the hardware.
delay(127);
for(uint8_t j=0;j<77;j++){
pixels.setPixelColor(j, pixels.Color(0,0,0)); // Moderately bright green color.
}
pixels.show(); // This sends the updated pixel color to the hardware.
delay(127);
for(uint8_t j=0;j<77;j++){
pixels.setPixelColor(j, pixels.Color(0,150,120)); // Moderately bright green color.
}
pixels.show(); // This sends the updated pixel color to the hardware.
delay(127);
for(uint8_t j=0;j<77;j++){
pixels.setPixelColor(j, pixels.Color(0,0,0)); // Moderately bright green color.
}
pixels.show(); // This sends the updated pixel color to the hardware.
cap.begin();
}
void loop() {
cap.touched();
for (uint16_t i=0; i<12; i++) {
if (cap.touched() & (1 << 0)) {
LED() ;
playMIDINote(1, 1, 127);
digitalWrite(n1,HIGH);
}
else if (cap.touched() & (1 << 2)) {
LED();
playMIDINote(1, 2, 127);
digitalWrite(n2,HIGH);
}
else if (cap.touched() & (1 << 4)) {
LED();
playMIDINote(1, 3, 127);
digitalWrite(n3,HIGH);
}
else if (cap.touched() & (1 << 6)) {
LED();
playMIDINote(1, 4, 127);
digitalWrite(n4,HIGH);
}
else if (cap.touched() & (1 << 8)) {
LED();
playMIDINote(1, 5, 127);
digitalWrite(n5,HIGH);
}
else
digitalWrite(n5,LOW);
LEDoff();
break;
}
// reset our state
}
void LED() {
for(int i=0;i<NUMPIXELS;i++){
// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(100,150,120)); // Moderately bright green color.
}
//}
pixels.show(); // This sends the updated pixel color to the hardware.
}
void LEDoff() {
for(int i=NUMPIXELS;i>0;i--){
pixels.setPixelColor(i, pixels.Color(0,0,0)); // Moderately bright green color.
pixels.show(); // This sends the updated pixel color to the hardware.
}
delay(1);
}
void playMIDINote(byte channel, byte note, byte velocity)
{
//MIDI channels 1-16 are really 0-15
byte noteOnStatus=0x90 + (channel-1);
//Send notes to MIDI output:
Serial.write(noteOnStatus);
Serial.write(note);
Serial.write(velocity);
}