+/*
+  The circuit:
+  * A connect to digital 2
+  * B connect to digital 3
+  * C connect to digital 4
+  * D connect to digital 5
+  * OE connect to digital 6
+  * STB connect to digital 10
+  * R1 connect to digital 11
+  * CLK connect to digital 13
+  * GND connect to GND
+*/
+
+
 #include <SoftwareSerial.h>
+#include <SPI.h>
+#include "bars.h"
+#include "words.h"
 
 #define SOUND_SENSOR A0
 #define SOUND_SENSOR_2 A2
-#define GREEN 2      // the number of the LED pin
-#define RED 3
-#define THRESHOLD_GREEN 200//The threshold to turn the led on 400.00*5/1024 = 1.95v
-#define THRESHOLD_RED 500
+#define THRESHOLD_FAIL 200//The threshold to turn the led on 400.00*5/1024 = 1.95v
+#define THRESHOLD_GOOD 400
+#define THRESHOLD_WIN 600
 #define LCD_OUT 8
 
-#include <SPI.h>
-#include "bars.h"
 
 #define RowA 2
 #define RowB 3

     int normValue = value/DBNORM;
     normValue = min(normValue*2,32);
     if (normValue == 0){
      hz = zero; 
     }
     else if (normValue == 1){
      hz = one; 
     }
     else if (normValue == 2){
      hz = two; 
     }
     else if (normValue == 3){
      hz = three; 
     }
     else if (normValue == 4){
      hz = four; 
     }
     else if (normValue == 5){
      hz = five; 
     }
     else if (normValue == 6){
      hz = six; 
     }
     else if (normValue == 7){
      hz = seven; 
     }
     else if (normValue == 8){
      hz = eight; 
     }
     else if (normValue == 9){
      hz = nine; 
     }
     else if (normValue == 10){
      hz = ten; 
     }
     else if (normValue == 11){
      hz = eleven; 
     }
     else if (normValue == 12){
      hz = twelve; 
     }
     else if (normValue == 13){
      hz = thirteen; 
     }
     else if (normValue == 14){
      hz = fourteen; 
     }
     else if (normValue == 15){
      hz = fifteen; 
     }
     else if (normValue == 16){
      hz = sixteen; 
     }
     else {
      hz = sixteen; 
     }



     //Serial.println(hz.size);
     
 
/**     
     for (int i=0;i<64;i++){
     	 if (i<normValue){
     	     hz1[i] = ~0xFF;
     }
         else if(i>32 && i<normValue+32){
     	     hz1[i] = ~0xFF;
     	 }
         else{
             hz1[i] = ~0x00;
         }

	 }
  */   


}
void setup()
{

  
    Serial.begin(9600);
    pins_init();
    //Serial.println("HHHA");
    pinMode(LCD_OUT, OUTPUT);
    lcd.begin(9600);
  
    lcd_init();
    lcd.print("HOLA");

     pinMode(RowA, OUTPUT);
     pinMode(RowB, OUTPUT);
     pinMode(RowC, OUTPUT);
     pinMode(RowD, OUTPUT);
     pinMode(OE, OUTPUT);
     pinMode(R1, OUTPUT);
     pinMode(CLK, OUTPUT);
     pinMode(STB, OUTPUT);
     SPI.begin();
     delay(10);

}

void loop()
{
    
    sensor = analogRead(SOUND_SENSOR);
    sensor_2 = analogRead(SOUND_SENSOR_2);

//    sensor = 300;
//    sensor_2 = 300;
    if(min(sensor,sensor_2) > 0)
    {
        sensorValue = max(sensor, sensor_2);
        setHistogram(sensorValue);
  //SPI.begin();
  //delay(100);

  for(row=0;row<16;row++){
       for (int i=0;i<2;i++){
         SPI.transfer(~(hz[i*32+row*2]));
         SPI.transfer(~(hz[i*32+row*2+1]));
  }
  digitalWrite(OE,HIGH);
  hc138sacn(row);
  digitalWrite(STB,LOW);
  digitalWrite(STB,HIGH);
  delayMicroseconds(500);
  digitalWrite(OE,LOW);
  delayMicroseconds(500);
  
  }
  
  Serial.print("sensorValue ");
  Serial.println(sensorValue);
       
    if(sensorValue > THRESHOLD_GREEN)
        {
            turnOnLED(GREEN);

            if(counter==0){
                //Serial.println("********************* INICIO APLAUSOMETRO");
            }
            clapMeterOn = true;
            clapMeter = clapMeter + sensorValue;
            counter++;
        }
        if(sensorValue > THRESHOLD_RED)
        {
            turnOnLED(RED);
        }
        //delay(500);
        turnOffLEDS();
    
    if (clapMeterOn == true && sensorValue < 50)
    {
        average = clapMeter/counter;
        clapMeterOn = false;
        //Serial.println("HA SALIDO ===============> ");
        //Serial.println(average);
        lcd_blink(average);
        counter = 0;
        clapMeter = 0;
        //delay(1000);
    }
    }

}

void pins_init()
{
    pinMode(GREEN, OUTPUT);
    pinMode(RED, OUTPUT);
    pinMode(SOUND_SENSOR, INPUT);
}

void turnOnLED(int led)
{
    digitalWrite(led,HIGH);
}

void turnOffLEDS()
{
    digitalWrite(RED,LOW);
    digitalWrite(GREEN,LOW);
}

void lcd_init()


byte *hz;

int sensorValue;

int average;
