/* PixMob Music Rhythm 0.1v
   Original Version - Copyright (c) 2022, Carlos Ganoza Plasencia
   url: http://carlosganoza.com
   github.com/drneox
   This update by davejavu 06/2024
*/

#include <Arduino.h>
#include <IRremote.hpp>
const int IRLedPin = 5;  // led GPIO pin
const int microAnalog = A0; // analog pin for the sound sensor
const int microDigital = 6; // digital pin for the sound sensor

uint16_t yellow[21] = {1400, 1400, 700, 700, 700, 1400, 700, 2800, 700, 2100, 1400, 700, 700, 700, 700, 1400, 1400, 2800, 1400, 2800, 700};
uint16_t red[21] =   {1400, 1400, 700, 700, 700, 1400, 700, 2800, 700, 2100, 1400, 700, 700, 700, 700, 1400, 1400, 2800, 1400, 2800, 700};
uint16_t green[21] = {1400, 1400, 700, 700, 700, 700, 1400, 2800, 700, 1400, 700, 1400, 700, 1400, 700, 1400, 1400, 2800, 1400, 2800, 700};
uint16_t pink[23] = {700, 700, 700, 2100, 1400, 700, 700, 2800, 700, 1400, 700, 700, 700, 2800, 1400, 1400, 700, 2100, 700, 700, 700, 2100, 700};
uint16_t blue[27] =   {700, 700, 700, 2100, 1400, 700, 700, 2800, 700, 1400, 700, 700, 700, 1400, 1400, 700, 700, 1400, 700, 700, 700, 700, 700, 700, 700, 2100, 700};

uint16_t* colors[] = {yellow, green, pink, blue, red};
int colorLengths[] = {21, 21, 23, 27, 21};

int currentColorIndex = 0;
int flashCounter = 0;

void setup() {
  Serial.begin(115200);
  IrSender.begin(IRLedPin);
  IrSender.enableIROut(38);
  pinMode(microAnalog, INPUT);
  pinMode(microDigital, INPUT);
}

void loop() {
  if (digitalRead(microDigital) == HIGH) {
    Serial.print("send ");
    switch (currentColorIndex) {
      case 0: Serial.println("yellow code"); break;
      case 1: Serial.println("green code"); break;
      case 2: Serial.println("pink code"); break;
      case 3: Serial.println("blue code"); break;
      case 4: Serial.println("red code"); break;
    }

    IrSender.sendRaw(colors[currentColorIndex], colorLengths[currentColorIndex], 38);

    flashCounter++;
    if (flashCounter >= 8) {
      flashCounter = 0;
      currentColorIndex = (currentColorIndex + 1) % 5;  // Cycle through colors
    }
    
    delay(200);
    Serial.print("Flash count: ");
    Serial.println(flashCounter);
    Serial.println("-----");
  }
}
