115 lines
2.7 KiB
C++
115 lines
2.7 KiB
C++
|
|
#include "Adafruit_NeoPixel.h"
|
|
|
|
float sensorValue[8] = {
|
|
0, 0, 0, 0, 0, 0, 0, 0}; // variable to store the value coming from the sensor
|
|
int sensorPin[8] = {
|
|
A1, A2, A3, A4, A5, A6, A7, A8};
|
|
unsigned long time[8] = {
|
|
0, 0, 0, 0, 0, 0, 0, 0};
|
|
boolean state[8] = {
|
|
true, true, true, true, true, true, true, true };
|
|
MIDIEvent e1 = {
|
|
0x09, 0x90, 60, 64};
|
|
MIDIEvent e2 = {
|
|
0x08, 0x80, 60, 0};
|
|
|
|
#define DEBUG 1
|
|
|
|
// Pin definitions for the 74HC164 SIPO shift register (drives button rows high)
|
|
#define DATAPIN 5
|
|
#define CLOCKPIN 7
|
|
|
|
#define POWER_ON 10
|
|
|
|
int NUM_LEDS = 8;
|
|
#define PIN 12
|
|
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
|
|
|
|
void setup() {
|
|
// 164 Setup
|
|
pinMode(DATAPIN, OUTPUT);
|
|
pinMode(CLOCKPIN, OUTPUT);
|
|
pinMode(POWER_ON , OUTPUT);
|
|
digitalWrite(POWER_ON,LOW);
|
|
strip.begin(); // Initialization of led matrix
|
|
strip.setPixelColor(0, 0, 0, 0);
|
|
strip.show();
|
|
|
|
if(DEBUG)
|
|
Serial.begin(115200);
|
|
}
|
|
|
|
void loop() {
|
|
//strip.show();
|
|
// read the value from the sensor:
|
|
for (int i = 0; i<8; i++)
|
|
{
|
|
sensorValue[i] = average(sensorPin[i]);
|
|
|
|
// turn the ledPin on
|
|
if((sensorValue[i]>5)&&(state[i]))
|
|
{
|
|
delayMicroseconds(2000);
|
|
//sensorValue = analogRead(sensorPin)*(4850/1023.);
|
|
sensorValue[i] = average(sensorPin[i]);
|
|
|
|
if(sensorValue[i])
|
|
{
|
|
if ((sensorValue[i]>341)&(sensorValue[i]<=682)) strip.setPixelColor(i, 0, map(sensorValue[i],342, 682, 0, 255), 0);
|
|
else if(sensorValue[i]>682) strip.setPixelColor(i, 0, 0,map(sensorValue[i], 883, 1023, 0, 255));
|
|
else strip.setPixelColor(i, map(sensorValue[i], 0, 341, 0, 255), 0, 0);
|
|
|
|
MIDIEvent e1 = {
|
|
0x09, 0x90, 60+i, sensorValue[i]/8 };
|
|
MIDIUSB.write(e1);
|
|
strip.show();
|
|
|
|
time[i] = millis();
|
|
state[i] = false;
|
|
|
|
if(DEBUG)
|
|
{
|
|
Serial.print("Sensor");
|
|
Serial.print(i);
|
|
Serial.print(": ");
|
|
Serial.print((sensorValue[i]*5)/1.023);
|
|
Serial.println("mV");
|
|
}
|
|
}
|
|
}
|
|
|
|
if(((millis()-time[i])>=60)&&(!state[i]))
|
|
{
|
|
state[i] = true;
|
|
MIDIEvent e2 = { 0x09, 0x90, 60+i, 0 };
|
|
MIDIUSB.write(e2);
|
|
ShiftWrite(i, HIGH);
|
|
ShiftWrite(i, LOW);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
float average(int anaPin) {
|
|
int lecturas = 10;
|
|
long total = 0;
|
|
float average = 0;
|
|
for(int i=0; i<lecturas; i++)
|
|
{
|
|
//delay(1);
|
|
total = total + analogRead(anaPin);
|
|
}
|
|
average = (float)total / lecturas;
|
|
return(average);
|
|
}
|
|
|
|
uint8_t val_shift = 0x00; //Valor del shift register
|
|
void ShiftWrite(uint8_t pin, boolean state){
|
|
bitWrite(val_shift, pin, state);
|
|
shiftOut(DATAPIN, CLOCKPIN, MSBFIRST, val_shift);
|
|
}
|
|
|
|
|
|
|