Skip to content

Instantly share code, notes, and snippets.

@tadfmac
Created August 27, 2022 06:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tadfmac/8916deed75f60839810058b9485af5d4 to your computer and use it in GitHub Desktop.
// XIAO-RP2040-CHICKEN example
// 2022 @tadfmac
/*
D0 : MIC (ADC)
D1 : VIB Sensor (DI)
*/
#include <Arduino.h>
#include <Adafruit_TinyUSB.h>
#include <Adafruit_NeoPixel.h>
#include <MIDI.h>
Adafruit_USBD_MIDI usb_midi;
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);
#define LED_POWER 11
#define LED_PIN 12 // NeoPixel
Adafruit_NeoPixel pixels(1, LED_PIN);
uint32_t colors1[] = {
pixels.Color(255, 0, 0),
pixels.Color(255, 0, 0),
pixels.Color(0, 0, 0),
pixels.Color(0, 0, 0),
pixels.Color(0, 0, 255),
pixels.Color(0, 0, 255),
pixels.Color(0, 0, 0),
pixels.Color(0, 0, 0),
pixels.Color(255, 0, 0),
pixels.Color(255, 0, 0),
pixels.Color(0, 0, 0),
pixels.Color(0, 0, 0),
pixels.Color(0, 0, 255),
pixels.Color(0, 0, 255),
pixels.Color(0, 0, 0),
pixels.Color(0, 0, 0)
};
const uint8_t COLORS1_LEN = (uint8_t)(sizeof(colors1) / sizeof(colors1[0]));
uint32_t colors2[] = {
pixels.Color(0, 255, 0),
pixels.Color(0, 255, 0),
pixels.Color(128, 255, 0),
pixels.Color(128, 255, 0),
pixels.Color(0, 0, 0),
pixels.Color(0, 0, 0),
pixels.Color(0, 0, 0),
pixels.Color(0, 0, 0)
};
const uint8_t COLORS2_LEN = (uint8_t)(sizeof(colors2) / sizeof(colors2[0]));
uint8_t noteNum[] = {36,38,40,43,45,48};
#define RED 17
#define GREEN 16
#define BLUE 25
int flg = 0;
int vib = LOW;
int vibcnt = 0;
int adc1 = 0;
int adcAv8 = 0;
int adcAvCnt = 0;
int adcTrig = 0;
int trigNote = -1;
int latch1 = 0;
int latch2 = 0;
int colorMode = 0;
int vibOn = 0;
uint32_t *color = colors1;
uint8_t colorLen = COLORS1_LEN;
int colorIndex = 0;
#define ADC_THRESHOLD 240
void setup() {
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
TinyUSB_Device_Init(0);
#endif
pinMode(RED,OUTPUT);
pinMode(GREEN,OUTPUT);
pinMode(BLUE,OUTPUT);
digitalWrite(RED,HIGH);
digitalWrite(GREEN,HIGH);
digitalWrite(BLUE,HIGH);
pinMode(D1,INPUT);
pinMode(LED_POWER, OUTPUT);
digitalWrite(LED_POWER, HIGH);
SerialTinyUSB.begin(115200);
MIDI.begin(MIDI_CHANNEL_OMNI);
while( !TinyUSBDevice.mounted() ) {
Serial.print(".");
delay(1);
}
SerialTinyUSB.println("\nUSB mounted!");
}
void loop() {
// vib
if((latch1 == 0)&&(latch2 == 0)){
pixels.clear();
pixels.show();
}
if(latch1 == 0){
vib = digitalRead(D1);
if(vib == HIGH){
if(vibcnt < 6){
vibcnt++;
if((vibcnt >= 6)&&(vibOn == 0)){
SerialTinyUSB.println("VIB ON");
latch1 = 80;
colorMode = 1;
vibOn = 1;
color = colors2;
colorLen = COLORS2_LEN;
colorIndex = 0;
MIDI.sendNoteOn(20, 127, 1);
}
}
}else{
if(vibcnt > 0){
vibcnt --;
if(vibOn == 1){
SerialTinyUSB.println("VIB OFF");
vibOn = 0;
MIDI.sendNoteOff(20, 0, 1);
}
}
}
}
if(latch2 == 0){
// mic
adc1 = analogRead(D0);
// SerialTinyUSB.print("ADC : ");
// SerialTinyUSB.println(adc1);
if(adcAvCnt == 8){
int adcAv = adcAv8 / 8;
/*
SerialTinyUSB.print("adc1 : ");
SerialTinyUSB.print(adc1);
SerialTinyUSB.print(" adcAv : ");
SerialTinyUSB.println(adcAv);
*/
if((adc1 > (adcAv + ADC_THRESHOLD))||(adc1 < (adcAv - ADC_THRESHOLD))){
if((trigNote == -1)&&(adcTrig == 0)){
uint8_t idx = random(0,sizeof(noteNum));
trigNote = (int)(noteNum[idx]);
SerialTinyUSB.println("TRIG : ON!!!!");
MIDI.sendNoteOn(trigNote, 127, 1);
latch2 = 100;
colorMode = 0;
color = colors1;
colorLen = COLORS1_LEN;
colorIndex = 0;
}
if(adcTrig < 16){
adcTrig ++;
}
}else{
adcAv8 = ((adcAv8-adcAv) + adc1);
if(adcTrig > 0){
adcTrig --;
if((adcTrig == 0)&&(trigNote != -1)){
SerialTinyUSB.println("TRIG : OFF");
MIDI.sendNoteOff(trigNote, 0, 1);
trigNote = -1;
}
}
}
}else{
if(adcAvCnt < 8){
adcAvCnt ++;
}
adcAv8 = (adcAv8 + adc1);
}
}
if((latch1 > 0)||(latch2 > 0)){
pixels.setPixelColor(0, color[colorIndex]);
pixels.show();
colorIndex ++;
if(colorIndex >= colorLen){
colorIndex = 0;
}
}
if(latch1 > 0){
latch1 --;
}
if(latch2 > 0){
latch2 --;
}
if(SerialTinyUSB.available() > 0) {
uint8_t data = SerialTinyUSB.read();
if(10 == data) {
data = '\n';
}
SerialTinyUSB.print(data);
}
MIDI.read();
TinyUSB_Device_FlushCDC();
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment