// First LED program
#include <Adafruit_NeoPixel.h>
/* Digital pins for LEDs */
//#define PIN1
#define PIN2 16 //
//#define PIN3
//#define PIN4
#define PIN5 13 //
// How many NeoPixels?
#define NUMPIXELS 14
Adafruit_NeoPixel pixels2(NUMPIXELS, PIN2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels5(NUMPIXELS, PIN5, NEO_GRB + NEO_KHZ800);
unsigned long startMillis2 = millis(); // timestamp
unsigned long startMillis5 = millis();
unsigned long now2;
unsigned long now5;
int i = 0; // iterator, from for-loop
int k = 0;
bool latch2 = false;
bool latch5 = false;
const int period = 200;
int x5_a, y5_a;
int x5_b, y5_b;
int x2_a, y2_a;
int x2_b, y2_b;
void setup()
{
pixels2.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels5.begin();
}
void loop()
{
//first strand, activate if touched
now2 = millis();
if ( /*abs(x5_a) > 15 || abs(y5_a) > 15*/ )
latch2 = true;
if (latch2 == true)
{
if (now2 - startMillis2 >= period)
{
startMillis2 = now2; // save timestamp
if (i >= NUMPIXELS)
{
i = 0; // reset i at max value
pixels2.clear(); // Set all pixel colors to 'off'
latch2 = false;
}
pixels2.setPixelColor( i, pixels2.Color(150 - i * 20, i * 20, 0) );
pixels2.show(); // Send the updated pixel colors to the hardware
i++; // go to next value
}
}
//second strand, activate if touched
now5 = millis();
if ( /*abs(x2_a) > 45 || abs(y2_a) > 12*/ )
latch5 = true;
if (latch5 == true)
{
if (now5 - startMillis5 >= period)
{
startMillis5 = now5; // save timestamp
if (k >= NUMPIXELS)
{
k = 0; // reset i at max value
pixels5.clear();
latch5 = false;
}
pixels5.setPixelColor( k, pixels5.Color(150 - k * 20, k * 20, 0) );
pixels5.show();
k++; // go to next value
}
}
}
I would like to combine these two programs.
ESP32
https://www.mouser.com/ProductDetail/Espressif-Systems/ESP32-DevKitC-32D
Pinout
ESP SparkFun Shield
GND GND (jumper all grounds together for Shield)
16 2
17 3
4 4
RX RX
TX TX
5V +5V
3V3 3.3V
*/
#include <Adafruit_NeoPixel.h>
#define RXD2 16 // was 2, not needed
#define TXD2 18 // was 3 MIDI-In
/* Digital pins for LEDs */
//#define PIN1
#define PIN2 16 //
//#define PIN3
//#define PIN4
#define PIN5 13 //
// How many NeoPixels?
#define NUMPIXELS 14
Adafruit_NeoPixel pixels2(NUMPIXELS, PIN2, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel pixels5(NUMPIXELS, PIN5, NEO_GRB + NEO_KHZ800);
unsigned long startMillis2 = millis(); // timestamp
unsigned long startMillis5 = millis();
unsigned long now2;
unsigned long now5;
int i = 0; // iterator, from for-loop
int k = 0;
bool latch2 = false;
bool latch5 = false;
const int period = 200;
byte note = 0; //The MIDI note value to be played
byte resetMIDI = 4; //Reset line
int instrument = 0;
byte x5pin = 12;
byte y5pin = 14;
//byte x4pin = 26;
//byte y4pin = 25;
//byte x3pin = 35;
//byte y3pin = 34;
byte x2pin = 4;
byte y2pin = 0;
//byte x1pin = 2;
//byte y1pin = 15;
int x5_a, y5_a;
int x5_b, y5_b;
int x2_a, y2_a;
int x2_b, y2_b;
void setup()
{
Serial.begin(115200);
//Setup soft serial for MIDI control
Serial2.begin(31250, SERIAL_8N1, RXD2, TXD2);
pixels2.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels5.begin();
//Reset
pinMode(resetMIDI, OUTPUT);
digitalWrite(resetMIDI, LOW);
delay(100);
digitalWrite(resetMIDI, HIGH);
delay(100);
talkMIDI(0xB0, 0x07, 120); //0xB0 is channel message, set channel volume to near max (127)
}
void loop()
{
x5_a = analogRead(x5pin);
y5_a = analogRead(y5pin);
delay(10);
x5_b = analogRead(x5pin);
y5_b = analogRead(y5pin);
x5_a = x5_b - x5_a;
y5_a = y5_b - y5_a;
x2_a = analogRead(x2pin);
y2_a = analogRead(y2pin);
delay(10);
x2_b = analogRead(x2pin);
y2_b = analogRead(y2pin);
x2_a = x2_b - x2_a;
y2_a = y2_b - y2_a;
if (abs(x5_a) > 15 || abs(y5_a) > 15)
{
// Serial.print("x5: ");
// Serial.print(x5_a, DEC); // acceleration in X axis
// Serial.println();
// Serial.print("y5: ");
// Serial.print(y5_a, DEC); // acceleration in Y axis
// Serial.println('\n');
talkMIDI(0xB0, 0, 0x00); //Default bank GM1
talkMIDI(0xC0, 98, 0); //Set instrument number. 0xC0 is a 1 data byte command
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0, 98, 60);
delay(50);
//Turn off the note with a given off/release velocity
noteOff(0, 98, 15000);
}
if (abs(x2_a) > 45 || abs(y2_a) > 12)
{
// Serial.print("x5: ");
// Serial.print(x2_a, DEC); // acceleration in X axis
// Serial.println();
// Serial.print("y5: ");
// Serial.print(y2_a, DEC); // acceleration in Y axis
// Serial.println('\n');
talkMIDI(0xB0, 0, 0x00); //Default bank GM1
talkMIDI(0xC0, 98, 0); //Set instrument number. 0xC0 is a 1 data byte command
//Note on channel 1 (0x90), some note value (note), middle velocity (0x45):
noteOn(0, 98, 60);
delay(50);
//Turn off the note with a given off/release velocity
noteOff(0, 98, 15000);
}
delay(100);
}
//Send a MIDI note-on message. Like pressing a piano key
//channel ranges from 0-15
void noteOn(byte channel, byte note, byte attack_velocity) {
talkMIDI( (0x90 | channel), note, attack_velocity);
}
//Send a MIDI note-off message. Like releasing a piano key
void noteOff(byte channel, byte note, byte release_velocity) {
talkMIDI( (0x80 | channel), note, release_velocity);
}
//Plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that data values are less than 127
void talkMIDI(byte cmd, byte data1, byte data2) {
Serial2.write(cmd);
Serial2.write(data1);
//Some commands only have one data byte. All cmds less than 0xBn have 2 data bytes
//(sort of: http://253.ccarh.org/handout/midiprotocol/)
if ( (cmd & 0xF0) <= 0xB0)
Serial2.write(data2);
}