#ifndef NewTone_h
#define NewTone_h
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
#define TIMSK1 TIMSK
#endif
void NewTone(uint8_t pin, unsigned long frequency, unsigned long length = 0);
void noNewTone(uint8_t pin = 0);
#endif
unsigned long _nt_time; // Time note should end.
uint8_t _pinMask = 0; // Pin bitmask.
volatile uint8_t *_pinOutput; // Output port register
void NewTone(uint8_t pin, unsigned long frequency, unsigned long length) {
uint8_t prescaler = _BV(CS10); // Try using prescaler 1 first.
unsigned long top = F_CPU / frequency / 4 - 1; // Calculate the top.
if (top > 65535) { // If not in the range for prescaler 1, use prescaler 256 (61 Hz and lower @ 16 MHz).
prescaler = _BV(CS12); // Set the 256 prescaler bit.
top = top / 256 - 1; // Calculate the top using prescaler 256.
}
if (length > 0) _nt_time = millis() + length; else _nt_time = 0xFFFFFFFF; // Set when the note should end, or play "forever".
if (_pinMask == 0) {
_pinMask = digitalPinToBitMask(pin); // Get the port register bitmask for the pin.
_pinOutput = portOutputRegister(digitalPinToPort(pin)); // Get the output port register for the pin.
uint8_t *_pinMode = (uint8_t *) portModeRegister(digitalPinToPort(pin)); // Get the port mode register for the pin.
*_pinMode |= _pinMask; // Set the pin to Output mode.
}
ICR1 = top; // Set the top.
if (TCNT1 > top) TCNT1 = top; // Counter over the top, put within range.
TCCR1B = _BV(WGM13) | prescaler; // Set PWM, phase and frequency corrected (ICR1) and prescaler.
TCCR1A = _BV(COM1B0);
TIMSK1 |= _BV(OCIE1A); // Activate the timer interrupt.
}
void noNewTone(uint8_t pin) {
TIMSK1 &= ~_BV(OCIE1A); // Remove the timer interrupt.
TCCR1B = _BV(CS11); // Default clock prescaler of 8.
TCCR1A = _BV(WGM10); // Set to defaults so PWM can work like normal (PWM, phase corrected, 8bit).
*_pinOutput &= ~_pinMask; // Set pin to LOW.
_pinMask = 0; // Flag so we know note is no longer playing.
}
ISR(TIMER1_COMPA_vect) { // Timer interrupt vector.
if (millis() >= _nt_time) noNewTone(); // Check to see if it's time for the note to end.
*_pinOutput ^= _pinMask; // Toggle the pin state.
}
#include <IRremote.hpp>
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
#define REST 0
const int pinBuzzer = 13;
const int numLights = 3;
// For note type:
// 1 - Whole
// 2 - Half
// 4 - Quarter
// 8 - Eightheenth
// 16 - Sixteenth
// -4 - Dotted Quarter
// -8 - Dotted Eightheenth
// -16 - Dotted Sixteenth
int songhere[] = { // Per row, format: NOTE, note_type
NOTE_G4, 16,
NOTE_G4, 16,
NOTE_G4, 8,
REST, 16,
NOTE_F4, 16,
NOTE_F4, 16,
NOTE_F4, 16,
REST, 8,
NOTE_D4, 16,
NOTE_D4, 16,
NOTE_D4, 16,
};
void playSong(int tempo){
int divider = 0;
int noteDuration = 0;
int noteCount = sizeof(songhere) / sizeof(songhere[0]) / 2;
int wholenote = (60000 * 2) / tempo; // Duration, in seconds, of a whole note
for (int thisNote = 0; thisNote < noteCount * 2; thisNote = thisNote + 2)
{
divider = songhere[thisNote + 1];
if (divider > 0)
{ // For whole notes
noteDuration = (wholenote) / divider;
}
else if (divider < 0)
{ // Dotted Notes
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
NewTone(pinBuzzer, songhere[thisNote], noteDuration * 0.9);
delay(noteDuration);
noNewTone(pinBuzzer);
// Random lights
for(int i=0; i<numLights; i++){
digitalWrite(random(5, 16), HIGH);
delay(100);
offAllLights();
}
}
// Post song lights
for(int i=5; i<16; i++){
digitalWrite(i, HIGH);
delay(50);
}
offAllLights();
}
#define BUTTON_MODE 501415680
#define BUTTON_PLUS 4244832000
#define BUTTON_MINUS 1738080000
#define BUTTON_REVERSE 1036189440
#define BUTTON_USD 3710058240
#define BUTTON_0 2540240640
#define BUTTON_1 3476094720
#define BUTTON_2 3877175040
#define BUTTON_3 2239430400
#define BUTTON_4 4010868480
#define BUTTON_5 3342401280
#define BUTTON_6 2774204160
#define BUTTON_7 3175284480
#define BUTTON_8 3041591040
#define BUTTON_9 2907897600
int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
auto value= IrReceiver.decodedIRData.decodedRawData;
// decode_results results;
int mode = 1;
bool mode_changeMode = false;
int currentVolume = 0;
void setup()
{
// 3 4 5 6 8 9 10 11 (8 lights)
for(int i=3; i<11; i++){
pinMode(i, OUTPUT);
}
pinMode(pinBuzzer, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn();
//NewTone(4, 1232 , 500);
Serial.println("umabot ka ba dito?");
}
void pianoKeyboard(){
if(value == BUTTON_0){
digitalWrite(3, HIGH); // fIRST LED
NewTone(pinBuzzer, NOTE_C6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(3, LOW);
}else if(value == BUTTON_REVERSE){
digitalWrite(3, HIGH); // First LED
digitalWrite(4, HIGH); // Second LED
NewTone(pinBuzzer, NOTE_CS6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
} else if(value == BUTTON_USD){
digitalWrite(4, HIGH); // Second LED
NewTone(pinBuzzer, NOTE_D6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(4, LOW);
} else if(value == BUTTON_1){
digitalWrite(4, HIGH); // Second LED
digitalWrite(5, HIGH); // Third LED
NewTone(pinBuzzer, NOTE_DS6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
} else if(value == BUTTON_2){
digitalWrite(5, HIGH); // Third LED
NewTone(pinBuzzer, NOTE_E6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(5, LOW);
} else if(value == BUTTON_3){
digitalWrite(6, HIGH); // Fourth LED
NewTone(pinBuzzer, NOTE_F6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(6, LOW);
} else if(value == BUTTON_4){
digitalWrite(6, HIGH); // Fourth LED
digitalWrite(7, HIGH); // Fifth LED
NewTone(pinBuzzer, NOTE_FS6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
} else if(value == BUTTON_5){
digitalWrite(7, HIGH); // Fifth LED
NewTone(pinBuzzer, NOTE_G6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(7, LOW);
} else if(value == BUTTON_6){
digitalWrite(7, HIGH); // Fifth LED
digitalWrite(8, HIGH); // Sixth LED
NewTone(pinBuzzer, NOTE_GS6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
} else if(value == BUTTON_7){
digitalWrite(8, HIGH); // Sixth LED
NewTone(pinBuzzer, NOTE_A6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(8, LOW);
} else if(value == BUTTON_8){
digitalWrite(8, HIGH); // Sixth LED
digitalWrite(9, HIGH); // Seventh LED
NewTone(pinBuzzer, NOTE_AS6 , 500);
delay(500);
noNewTone(pinBuzzer);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
} else if(value == BUTTON_9){
digitalWrite(9, HIGH); // Seventh LED
NewTone(pinBuzzer, NOTE_B6 , 500);
delay(500);
noNewTone(pinBuzzer);
} else{
//Serial.println("User pressed other keys not applicable to piano keyboard");
}
return;
}
int previousTime = 0;
bool isRunning = false;
bool isPaused = true;
int garageMode = 1; // 1 - Open, 2 - Close
int currentLevel = 0;
void garageDoorOpener(){
if(!IrReceiver.decode()){
value = 0 ;
}else{
value = IrReceiver.decodedIRData.decodedRawData;
}
if(isRunning){
if(isPaused){
if(value == BUTTON_REVERSE){
if(garageMode == 1){
garageMode=2;
}else{
garageMode=1;
}
isPaused = false;
Serial.println("Garage door is resumed");
}else{
return;
}
}else {
if(value == BUTTON_REVERSE){
noNewTone(pinBuzzer);
isPaused = true;
Serial.println("Garage door is paused");
return;
}
}
int currentTime = millis();
if(currentTime - previousTime < 1000) return;
Serial.println("1 sec has passed. Should do something.");
previousTime = currentTime;
if(garageMode == 1){
NewTone(pinBuzzer, NOTE_AS4); // Increasing
if(currentLevel ==8){
Serial.println("Garage door is fully opened");
isRunning = false;
isPaused = true;
garageMode = 0;
noNewTone(pinBuzzer);
return;
}
currentLevel+=1;
} else{
NewTone(pinBuzzer, NOTE_DS3); // Decreasing
if(currentLevel ==0){
Serial.println("Garage door is fully closed");
isRunning = false;
isPaused = true;
garageMode = 1;
noNewTone(pinBuzzer);
return;
}
currentLevel-=1;
}
offAllLights();
for( int i=0; i<currentLevel; i++){
int pin = 3+i;
digitalWrite(pin, HIGH);
}
} else if(value == BUTTON_REVERSE){
previousTime = millis();
isRunning = true;
isPaused = false;
Serial.println("Garage door starts running");
}
}
void soundVolumeControl(){
if(value == BUTTON_PLUS){
if (currentVolume < 9) currentVolume++;
offAllLights();
for( int i=0; i<currentVolume; i++){
int pin = 3+i;
digitalWrite(3+i, HIGH);
}
Serial.println("Volume up");
} else if(value == BUTTON_MINUS){
if (currentVolume > 0) currentVolume--;
offAllLights();
for( int i=0; i<currentVolume; i++){
int pin = 3+i;
digitalWrite(3+i, HIGH);
}
Serial.println("Volume down");
} else{
Serial.println("User pressed other keys not applicable to sound volume control");
}
}
void offAllLights(){
for(int i=3; i<11; i++){
digitalWrite(i, LOW);
}
}
void loop()
{
if (!IrReceiver.decode()) {
if(mode == 2){
garageDoorOpener(); // garageDoorOpener is a passively running function
}
return;
}
value = IrReceiver.decodedIRData.decodedRawData;
if(value == BUTTON_MODE){
mode_changeMode = true;
Serial.println("Change mode");
} else if(mode_changeMode){
if(value == BUTTON_1){
mode = 1;
mode_changeMode = false;
offAllLights();
Serial.println("Change mode to 1");
} else if(value == BUTTON_2){
mode = 2;
mode_changeMode = false;
offAllLights();
Serial.println("Change mode to 2");
} else if(value == BUTTON_3){
mode = 3;
mode_changeMode = false;
currentVolume = 0;
offAllLights();
Serial.println("Change mode to 3");
} else if(value == BUTTON_4){
mode = 4;
mode_changeMode = false;
offAllLights();
Serial.println("Change mode to 4");
} else {
Serial.println("User pressed other keys, resetting mode_changeMode");
mode_changeMode = false;
}
} else{
Serial.println("User pressed normal key");
if(mode == 1){
pianoKeyboard();
} else if(mode == 2){
garageDoorOpener();
} else if(mode == 3){
soundVolumeControl(); // soundVolumeControl is a passively running function
} else if(mode == 4){
playSong(140);
}
}
Serial.println(value);
IrReceiver.resume();
delay(100);
}