Lab_interaccio/2014/Oval3/Oval3.ino

149 lines
3.7 KiB
Arduino
Raw Permalink Normal View History

2025-02-25 21:29:42 +01:00
#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
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))))
{
sensorValue[i] = average(sensorPin[i]);
//while (average(sensorPin[i])>sensorValue[i]) sensorValue[i] = average(sensorPin[i]);
*/
if ( ( sensorValue[i]>5 ) && state[i] )
{
delayMicroseconds(2000);
sensorValue[i] = analogRead(sensorPin[i]);
if(sensorValue[i]>5)
{
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]/2, 0, 127) };
MIDIUSB.write(e1);
//strip.show();
if(DEBUG)
{
Serial.print("Sensor");
Serial.print(i);
Serial.print(": ");
Serial.print((sensorValue[i]*5)/1.023);
Serial.println("mV");
}
// sensorValueAnt[i]=sensorValue[i];
}
}
if(((millis()-time[i])>=60)&&(!state[i]))
{
state[i] = true;
MIDIEvent e2 = { 0x09, 0x90, 60+i, 0 };
MIDIUSB.write(e2);
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<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);
}