// C++ code
#ifndef ezBuzzer_h
#define ezBuzzer_h
#define NOTE_A4 440
#define NOTE_A5 880
#define BUZZER_IDLE 0
#define BUZZER_BEEP_DELAY 1
#define BUZZER_BEEPING 2
#define BUZZER_MELODY 3
class ezBuzzer
{
private:
int _buzzerPin;
int _buzzerState;
unsigned long _delayTime;
unsigned long _beepTime;
unsigned long _startTime;
int *_melody;
int *_noteDurations;
int _melodyLength;
int _melodyIndex;
unsigned long _notePauseTime;
public:
ezBuzzer(int pin);
void stop(void);
void playMelody(int *melody, int *noteDurations, int length);
int getState(void);
void loop(void);
};
#endif
ezBuzzer::ezBuzzer(int pin) {
_buzzerPin = pin;
_buzzerState = BUZZER_IDLE;
_delayTime = 0;
_beepTime = 0;
_startTime = 0;
_melody = 0;
_noteDurations = 0;
_melodyLength = 0;
_melodyIndex = 0;
_notePauseTime = 0;
pinMode(_buzzerPin, OUTPUT);
}
void ezBuzzer::stop(void){
noTone(_buzzerPin);
digitalWrite(_buzzerPin, LOW);
_buzzerState = BUZZER_IDLE;
}
void ezBuzzer::playMelody(int *melody, int *noteDurations, int length) {
_melody = melody;
_noteDurations = noteDurations;
_melodyLength = length;
_melodyIndex = 0;
_notePauseTime = 0;
_buzzerState = BUZZER_MELODY;
_startTime = millis();
}
int ezBuzzer::getState(void) {
return _buzzerState;
}
void ezBuzzer::loop(void) {
switch(_buzzerState) {
case BUZZER_MELODY:
if(_melodyIndex < _melodyLength) {
if(!_notePauseTime) {
int duration = 1000 / _noteDurations[_melodyIndex];
tone(_buzzerPin, _melody[_melodyIndex], duration);
_notePauseTime = duration * 1.30;
_startTime = millis();
}
if ((unsigned long)(millis() - _startTime) >= _notePauseTime) {
noTone(_buzzerPin); // stop the tone playing:
_notePauseTime = 0;
_melodyIndex++; // play next node
}
} else {
noTone(_buzzerPin);
_buzzerState = BUZZER_IDLE;
}
break;
default:
break;
}
}
#include <Adafruit_NeoPixel.h>
int neoPixelPin = 13;
int numled1 = 15;
Adafruit_NeoPixel Ledstrip = Adafruit_NeoPixel(numled1, neoPixelPin, NEO_GRB + NEO_KHZ800);
int neoPixelPin1 = 12;
//int numled1 = 15;
Adafruit_NeoPixel Ledstrip1 = Adafruit_NeoPixel(numled1, neoPixelPin1, NEO_GRB + NEO_KHZ800);
#define AnInput0 A0
#define AnInput1 A1
#define buzzer 5
#define vibration0 3
int ledsmin=0;
int numcolor= 3;
int limidL1=numled1/numcolor-1;
int limidL2=numled1-(numled1/numcolor)+1;
int dswitch=0;
bool Lnote=false;
bool L2note=false;
bool Lnote1=false;
bool L2note1=false;
ezBuzzer buzzers(buzzer);
int Lmelody[]={NOTE_A4,NOTE_A4};
int Hmelody[]={NOTE_A5,NOTE_A5};
int notetime[] = {32, 32};
#define logo 11 // Arduino pin that connects to NeoPixel
#define NUM_PIXELS 16 // The number of LEDs (pixels) on NeoPixel
Adafruit_NeoPixel NeoPixel(NUM_PIXELS, logo, NEO_GRB + NEO_KHZ800);
// a pre-processor macro
void setup() {
Ledstrip.begin(); // initialize the strip
Ledstrip1.begin();
NeoPixel.begin();
Serial.begin(9600);
}
void loop() {
static int signalmin0=values();
static int signalmin1=values();
static int signalmax0=values();
static int signalmax1=values();
int diference0 = signalmax0 - signalmin0;
int firstpartA0 = signalmin0 + diference0/numcolor;
int secondpartA0 = signalmax0 - diference0/numcolor + 2*(diference0/numled1);
int diference1 = signalmax1 - signalmin1;
int firstpartA1 = signalmin1 + diference1/numcolor;
int secondpartA1 = signalmax1 - diference1/numcolor + 2*(diference1/numled1);
float Input0=analogRead(AnInput0);
//Serial.println(Input0);
int map0=map(Input0,signalmin0,signalmax0,ledsmin,numled1);
//Serial.println(map0);
float Input1=analogRead(AnInput1);
//Serial.println(Input0);
int map1=map(Input1,signalmin1,signalmax1,ledsmin,numled1);
colors();
allOff0(map0);
allOff1(map1);
Ledstrip.show();
Ledstrip1.show();
buzzers.loop(); // MUST call the buzzer.loop() function in loop()
//A0
if(Lnote == false) //Si interrupor en false...
{
if (firstpartA0<Input0 && Input0<firstpartA0+100)
{
tone(buzzer,NOTE_A4,50);
Lnote = true; //Cambio de false a true.
}
}
if(Lnote == true) //si interrupor en true...
{
if (firstpartA0-100<Input0 && Input0<firstpartA0)
{
tone(buzzer,NOTE_A5,50);
Lnote = false; //Cambio de true a false.
}
}
if(L2note == true) //Si interrupor 2 en false...
{
if (secondpartA0-100<Input0 && Input0<secondpartA0)
{
buzzers.playMelody(Hmelody, notetime, 2);
L2note = false; //Cambio de true a false.
}
}
if(L2note == false) //Si interrupor 2 en true...
{
if (secondpartA0<Input0 && Input0<secondpartA0+100)
{
buzzers.playMelody(Lmelody, notetime, 2);
L2note = true;//Cambio de false a true.
}
}
//A1
if(Lnote1 == false) //Si interrupor en false...
{
if (firstpartA1<Input1 && Input1<firstpartA1+100)
{
tone(buzzer,NOTE_A5,50);
Lnote1 = true; //Cambio de false a true.
}
}
if(Lnote1 == true) //si interrupor en true...
{
if (firstpartA1-100<Input1 && Input1<firstpartA1)
{
tone(buzzer,NOTE_A4,50);
Lnote1 = false; //Cambio de true a false.
}
}
if(L2note1 == true) //Si interrupor 2 en false...
{
if (secondpartA1-100<Input1 && Input1<secondpartA1)
{
buzzers.playMelody(Hmelody, notetime, 2);
L2note1 = false; //Cambio de true a false.
}
}
if(L2note1 == false) //Si interrupor 2 en true...
{
if (secondpartA1<Input1 && Input1<secondpartA1+100)
{
buzzers.playMelody(Lmelody, notetime, 2);
L2note1 = true;//Cambio de false a true.
}
}
vibrations(Input0);
vibrations(Input1);
neopixels(Input0,Input1);
// delay for the purposes of debouncing the switch
delay(10);
}
void allOff0( int map0) {
for (int a=map0; a < numled1; a++)
{
Ledstrip.setPixelColor(a, Ledstrip.Color(0, 0, 0));
}
/*for (int a=map1; a < numled1; a++)
{
Ledstrip1.setPixelColor(a, Ledstrip1.Color(0, 0, 0));
}*/
}
void allOff1( int map1) {
/*for (int a=map0; a < numled1; a++)
{
Ledstrip.setPixelColor(a, Ledstrip.Color(0, 0, 0));
}*/
for (int a=map1; a < numled1; a++)
{
Ledstrip1.setPixelColor(a, Ledstrip1.Color(0, 0, 0));
}
}
void colors() {
for( int i = 0; i < limidL1; i++ ) {
Ledstrip.setPixelColor(i, 0, 255, 0 );
Ledstrip1.setPixelColor(i, 0, 255, 0 );
}
for( int i = limidL1; i < limidL2 ; i++ ) {
Ledstrip.setPixelColor(i, 255, 255, 0 );
Ledstrip1.setPixelColor(i, 255, 255, 0 );
}
for( int i = limidL2; i < 15; i++ ) {
Ledstrip.setPixelColor(i, 255, 0, 0 );
Ledstrip1.setPixelColor(i, 255, 0, 0 );
}
}
void vibrations(int Input0){
//for (int i=0; i < mapA0; i++)
//{
uint8_t effect;
if (Input0 < (1023/3))
{
analogWrite(vibration0,255);
effect = 123;
}
else if (Input0 > (1023/3) && Input0 < ((1023/3)*2))
{
analogWrite(vibration0,255);
effect = 51;
}
else
{
analogWrite(vibration0,255*2);
effect = 47;
}
Serial.println("Playing effect: " + String(effect));
/*drv.setWaveform(0, effect);
drv.setWaveform(1, 0);
drv.go();*/
//}
}
void neopixels(int Input0,int Input1){
NeoPixel.clear(); // set all pixel colors to 'off'.
if (Input0 < 20 && Input1 < 20){
// Potentiometer is closed, set LEDs to one color (e.g., red)
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
NeoPixel.setPixelColor(pixel, NeoPixel.Color(0, 100, 0)); // verde medio. más alto es más claro, menor número, más oscuro
}
} else {
// Potentiometer is opened, set LEDs to another color (e.g., blue)
for (int pixel = 0; pixel < NUM_PIXELS; pixel++) {
NeoPixel.setPixelColor(pixel, NeoPixel.Color(0, 64, 200));
}
}
NeoPixel.show();
}
int values(){
int signalmin0,signalmax0;
int signalmin1,signalmax1;
if (dswitch==0){
Serial.println("MIN");
delay(1000);
int Input0=analogRead(AnInput0);
signalmin0=Input0;
delay(1000);
dswitch=dswitch+1;
return signalmin0;
}
if (dswitch==2){
Serial.println("MAX");
delay(1000);
int Input2=analogRead(AnInput0);
signalmax0=Input2;
delay(1000);
dswitch=dswitch+1;
return signalmax0;
}
if (dswitch==1){
Serial.println("MIN1");
delay(1000);
int Input0=analogRead(AnInput1);
signalmin1=Input0;
delay(1000);
dswitch=dswitch+1;
return signalmin1;
}
if (dswitch==3){
Serial.println("MAX1");
delay(1000);
int Input2=analogRead(AnInput1);
signalmax1=Input2;
delay(1000);
dswitch=dswitch+1;
return signalmax1;
}
}