/*
       
        Copyright (C) 2022 Maurilio Pizzurro 
        
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 1, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <IRremote.h>
#include <LiquidCrystal.h>
#include <Servo.h>

#define PIN_RECEIVER 2   // Signal Pin of IR receiver

IRrecv receiver(PIN_RECEIVER);
Servo RCservo;

LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

//#define BTN_VOL_PLUS  17
//#define BTN_VOL_MINUS 16
//#define IRCMD_PLUS      10
//#define IRCMD_MINUS     67
#define BTN_VOL_PLUS  4
#define BTN_VOL_MINUS A0

#define IRCMD_PLUS      2
#define IRCMD_MINUS     152

uint32_t t_volAct;
int volSetpoint;
int oldVolSetpoint;
int volume;

uint32_t usRepeat;

void setup() {
    Serial.begin(115200);
    
    pinMode(BTN_VOL_PLUS, INPUT_PULLUP);
    pinMode(BTN_VOL_MINUS, INPUT_PULLUP);
    receiver.enableIRIn(); // Start the receiver

    lcd.begin(16, 2);

    lcd.print("Press play");
    
    RCservo.attach(5);
    RCservo.write(volume);
}

uint32_t t_motorAct;
int deltaVol;

uint32_t countPerSec;

void countPerSecond() {
    if (millis() - t_volAct >= 1000) {
        Serial.println(countPerSec);
        countPerSec = 0;
        t_volAct = millis();
    } else {
        countPerSec++;
    }
}

void loop() {
    if (oldVolSetpoint != volSetpoint) {
        oldVolSetpoint = volSetpoint;
        Serial.println(volSetpoint);
    }
    //if (millis() - t_motorAct >= 35) {
        //t_motorAct = millis();
        deltaVol = volSetpoint - volume;
    //}
    if (deltaVol != 0) {
        volume = volSetpoint;
        RCservo.write(volume);
        //deltaVol = 0;
    }

    uint8_t command = 0;

    if (receiver.decode()) {
        command = receiver.decodedIRData.command;
        receiver.resume();  // Receive the next value
        Serial.println(command);
    } else {
        if (digitalRead(BTN_VOL_PLUS) == LOW) {
            command = IRCMD_PLUS;
        } else if (digitalRead(BTN_VOL_MINUS) == LOW) {
            command = IRCMD_MINUS;
        }

    }

    switch (command) {
        
        case IRCMD_PLUS:
            
            if (millis() - t_volAct > 10) {
                
                t_volAct = millis();
                if (volSetpoint < 100) {
                    volSetpoint++;
                }    
            }   
            break;

        case IRCMD_MINUS:
            
            if (millis() - t_volAct > 10) {
                /* CHANGE STATE */
                t_volAct = millis();
                if (volSetpoint > 0) {
                    volSetpoint--;
                }    
            }      
            break;

        
    } // end switch

} // end loop()