#include "Adafruit_NeoPixel.h" // Define various ADC prescaler const unsigned char PS_16 = (1 << ADPS2); const unsigned char PS_32 = (1 << ADPS2) | (1 << ADPS0); const unsigned char PS_64 = (1 << ADPS2) | (1 << ADPS1); const unsigned char PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); float sensorValue[8] = { 0, 0, 0, 0, 0, 0, 0, 0}; // variable to store the value coming from the sensor float sensorValueAnt[8] = { 0, 0, 0, 0, 0, 0, 0, 0}; // variable to store the value coming from the sensor anterior int sensorPin[8] = { A1, A7, A2, A6, A3, A5, A4, A8}; int dischargePin[8] = { 0, 6, 1, 5, 2, 4, 3, 7}; int mapled[8] = { 0, 6, 1, 5, 2, 4, 3, 7}; 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 0 // 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 for (int i = 0; i<8; i++) strip.setPixelColor(0, 0, 0, 0); strip.show(); // set up the ADC ADCSRA &= ~PS_128; // remove bits set by Arduino library // you can choose a prescaler from above. // PS_16, PS_32, PS_64 or PS_128 ADCSRA |= PS_16; // set our own prescaler to 64 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])))//||(sensorValue[i]>(sensorValueAnt[i]+5))&&((millis()-time[i])>=10))) { // sensorValue[i] = average(sensorPin[i]); // delayMicroseconds(2000); while (average(sensorPin[i])>sensorValue[i]){ /*delayMicroseconds(2000);*/ sensorValue[i] = average(sensorPin[i]);} // delayMicroseconds(2000); //sensorValue = analogRead(sensorPin)*(4850/1023.); if(sensorValue[i]>5) { if(DEBUG) { Serial.print("ON Sensor"); Serial.print(i); Serial.print(": "); Serial.print((sensorValue[i]*5)/1.023); Serial.print("mV"); Serial.print(" Time: "); Serial.print(millis()-time[i]); Serial.println("ms"); } time[i] = millis(); state[i] = false; int Value = sensorValue[i]*8; // if ((Value>341)&(Value<=682)) strip.setPixelColor(mapled[i], 0, map(Value,342, 682, 0, 255), 0); // else if(Value>682) strip.setPixelColor(mapled[i], 0, 0,map(Value, 883, 1023, 0, 255)); // else strip.setPixelColor(mapled[i], map(Value, 0, 341, 0, 255), 0, 0); MIDIEvent e1 = { 0x09, 0x90, 60+i, constrain(sensorValue[i]/4, 0, 127) }; MIDIUSB.write(e1); strip.show(); sensorValueAnt[i]=sensorValue[i]; } } } for (int i = 0; i<8; i++) { if(((millis()-time[i])>=60)&&(!state[i])) { state[i] = true; // MIDIEvent e2 = { 0x09, 0x90, 60+i, 0 }; // MIDIUSB.write(e2); if(DEBUG) { Serial.print("OFF Sensor"); Serial.print(i); Serial.println(); } ShiftWrite(dischargePin[i], HIGH); ShiftWrite(dischargePin[i], LOW); } } } float average(int anaPin) { int lecturas = 4; long total = 0; float average = 0; for(int i=0; i