hello, I am new here i´ve got about a month learning on my own arduino and have a small proyect that i am working on I dont have the schetch of the instalation yet but that is not the problem I have. I am making an alarmclock using a ir control and tempeture and humidity sensor its got a led rbg as a night ligth and mario tone.
every thing was working ok when i added the code for the mario tone,the code with the ir module is not working any more how it should. ieave my code in case somebody wants to help it almost 800 lines, its nonfinished program and it not working a 100% but what i have is working except for ir sensor
this is the mesage, in serial monitor power wehen it starts to play then i hit power again or any other botton and the results are difrence numbers that don´t corespond to the ir code
POWER
1
other button
2718691957
other button
1079351337
ps i don´t use delay don´t like to stop the ic
`
#include <SPI.h>
#include <LiquidCrystal.h>
#include "IRremote.h"
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
#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
//pines del arduino 3,4,5,6,7,8,9,10,11,13
//no se usa los analogicos o el 0,1,2,12
#define BLUE 9
#define GREEN 5
#define RED 6
//pin de los colores del led
#define DHT_SENSOR_PIN 7
//pin sensor de temperatura
#define lcdPin 3
//pin de luz de lcd
#define buzzer 4
//pin de buzzer
#define wholenote 1200
DHT_nonblocking dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
LiquidCrystal lcd(10); //11 and 13 also are used
IRrecv irrecv(8); //pin de IR
byte smiley[8] = {
B00000,
B10001,
B01010,
B00100,
B10001,
B01010,
B00100,
};
byte heart[8] = {
B00000,
B01010,
B11111,
B01110,
B00100,
B00000,
B00000,
};
byte alarm[8] = {
B00000,
B00000,
B00100,
B01010,
B01010,
B11111,
B00100,
};
int melody[] = {
NOTE_E5,
8,
NOTE_E5,
8,
REST,
8,
NOTE_E5,
8,
REST,
8,
NOTE_C5,
8,
NOTE_E5,
8, //1
NOTE_G5,
4,
REST,
4,
NOTE_G4,
8,
REST,
4,
NOTE_C5,
-4,
NOTE_G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E_C5,
8,
REST,
8,
NOTE_C5,
8,
NOTE_D5,
8,
NOTE_E5,
8, //13
REST,
1,
NOTE_C5,
8,
NOTE_C5,
4,
NOTE_C5,
8,
REST,
8,
NOTE_C5,
8,
NOTE_D5,
4,
NOTE_E5,
8,
NOTE_C5,
4,
NOTE_A4,
8,
NOTE_G4,
2,
NOTE_E5,
8,
NOTE_E5,
8,
REST,
8,
NOTE_E5,
8,
REST,
8,
NOTE_C5,
8,
NOTE_E5,
4,
NOTE_G5,
4,
REST,
4,
NOTE_G4,
4,
REST,
4,
//game over sound
NOTE_C5,
-4,
NOTE_G4,
-4,
NOTE_E4,
4, //45
NOTE_A4,
-8,
NOTE_B4,
-8,
NOTE_A4,
-8,
NOTE_GS4,
-8,
NOTE_AS4,
-8,
NOTE_GS4,
-8,
NOTE_G4,
8,
NOTE_D4,
8,
NOTE_E4,
-2,
};
int thisNote = 0, divider = 0, noteDuration = 0; //variables de musica
int notes = sizeof(melody) / sizeof(melody[0]) / 2; //tamaño de cancion
byte redvalue = 230, greenvalue = 0, bluevalue = 126; //valor de los colores
uint32_t last_decodedRawData = 0;
byte h1 = 1, h2 = 2, m1 = 0, m2 = 0, sec = 0, lcdlightcount = 0, edith1 = 0, edith2 = 0, editm1 = 0, editm2 = 0, timepos = 0, alh1 = 1, alh2 = 0, alm1 = 0, alm2 = 0;
bool s = false, func = true, edtime = false, isalarm = false, alarmActive = false, istone = true;
static unsigned long time = 0, nextNote = 0, lasttime = 0, firstTime = 0;
void setup() {
Serial.begin(9600);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
pinMode(buzzer, OUTPUT);
lcd.begin(16, 2);
pinMode(lcdPin, OUTPUT);
lcd.createChar(0, smiley);
lcd.createChar(1, heart);
lcd.createChar(2, alarm);
irrecv.enableIRIn();
lcd.setCursor(0, 0);
lcd.write(byte(1));
lcd.print("Hola, Yare.");
lcd.write(byte(1));
lcd.setCursor(0, 1);
lcd.print("te quiero mucho!");
digitalWrite(lcdPin, HIGH);
}
void advancetime() {
if (s) {
sec = sec + 1;
if (sec > 59) {
sec = 0;
if (m2 > 8) {
m2 = 0;
m1 = m1 + 1;
}
else
m2 = m2 + 1;
if (m1 > 5) {
m1 = 0;
h2 = h2 + 1;
}
if (h2 > 9) {
h1 = h1 + 1;
h2 = 0;
}
if (h1 * 10 + h2 > 23) {
h1 = 0;
h2 = 0;
}
}
} //end of if s
}
void chkalarm() {
if (isalarm && h1 == alh1 && h2 == alh2 && m1 == alm1 && m2 == alm2) {
alarmActive = true;
}
}
void lcdlight(bool x) {
if (x) {
digitalWrite(lcdPin, HIGH);
lcdlightcount = 0;
} else
digitalWrite(lcdPin, LOW);
}
void shownewtime(int pos) {
if (pos == 4)
pos = 0;
Serial.print("pos ");
Serial.println(pos);
if (edith1 > 2)
edith1 = 2;
if (editm1 > 5)
editm1 = 5;
if (edith1 == 2 && edith2 > 3)
edith2 = 3;
lcd.setCursor(9, 1);
lcd.print(edith1);
lcd.print(edith2);
lcd.print(":");
lcd.print(editm1);
lcd.print(editm2);
lcd.print(" P");
lcd.setCursor(9, 0);
lcd.print(" ");
if (pos > 1)
lcd.setCursor(pos + 10, 0);
else
lcd.setCursor(pos + 9, 0);
lcd.write(byte(0));
}
void shownewalarm(int pos) {
if (pos == 4)
pos = 0;
Serial.print("pos ");
Serial.println(pos);
if (alh1 > 2)
alh1 = 2;
if (alm1 > 5)
alm1 = 5;
if (alh1 == 2 && alh2 > 3)
alh2 = 3;
lcd.setCursor(9, 1);
lcd.print(alh1);
lcd.print(alh2);
lcd.print(":");
lcd.print(alm1);
lcd.print(alm2);
lcd.print(" P");
lcd.setCursor(9, 0);
lcd.print(" ");
if (pos > 1)
lcd.setCursor(pos + 10, 0);
else
lcd.setCursor(pos + 9, 0);
lcd.write(byte(0));
}
void editTime() {
func = false;
edtime = true;
lcd.clear();
edith1 = h1, edith2 = h2, editm1 = m1, editm2 = m2;
lcd.setCursor(0, 0);
lcd.print("hora");
lcd.setCursor(0, 1);
lcd.print(edith1);
lcd.print(edith2);
lcd.print(":");
lcd.print(editm1);
lcd.print(editm2);
lcd.print(" P");
shownewtime(0);
}
void editalarm() {
func = false;
edtime = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.write(byte(2));
lcd.print("Alarma");
lcd.write(byte(2));
lcd.setCursor(9, 1);
lcd.print(alh1);
lcd.print(alh2);
lcd.print(":");
lcd.print(alm1);
lcd.print(alm2);
lcd.print(" P");
}
void savetime(bool x) {
h1 = edith1, h2 = edith2, m1 = editm1, m2 = editm2;
func = true;
edtime = x;
lcd.clear();
}
void settime(int x, int pos) {
pos = pos + 1;
timepos = pos;
switch (pos) {
case 1:
edith1 = x;
shownewtime(timepos);
break;
case 2:
edith2 = x;
shownewtime(timepos);
break;
case 3:
editm1 = x;
shownewtime(timepos);
break;
case 4:
editm2 = x;
shownewtime(timepos);
break;
default:
Serial.print("no");
}
if (pos > 3)
timepos = 0;
}
void setalarm(int x, int pos) {
pos = pos + 1;
timepos = pos;
switch (pos) {
case 1:
edith1 = x;
shownewalarm(timepos);
break;
case 2:
edith2 = x;
shownewalarm(timepos);
break;
case 3:
editm1 = x;
shownewalarm(timepos);
break;
case 4:
editm2 = x;
shownewalarm(timepos);
break;
default:
Serial.print("no");
}
if (pos > 3)
timepos = 0;
}
void colorluz(int r, int b, int g) {
if (redvalue < 5)
redvalue = 1;
else if (redvalue > 245)
redvalue = 245;
if (bluevalue < 5)
bluevalue = 1;
else if (bluevalue > 245)
bluevalue = 245;
if (greenvalue < 5)
greenvalue = 1;
else if (greenvalue > 245)
greenvalue = 245;
switch (r) {
case 0:
redvalue = redvalue + 5;
break;
case 1:
redvalue = redvalue - 5;
break;
case 2:
redvalue = 122;
break;
}
switch (b) {
case 0:
bluevalue = bluevalue + 5;
break;
case 1:
bluevalue = bluevalue - 5;
break;
case 2:
bluevalue = 122;
break;
}
switch (g) {
case 0:
greenvalue = greenvalue + 5;
break;
case 1:
greenvalue = greenvalue - 5;
break;
case 2:
greenvalue = 122;
break;
}
if (redvalue < 5)
redvalue = 0;
else if (redvalue > 245)
redvalue = 245;
if (bluevalue < 5)
bluevalue = 0;
else if (bluevalue > 245)
bluevalue = 245;
if (greenvalue < 5)
greenvalue = 0;
else if (greenvalue > 245)
greenvalue = 245;
Serial.println("r = ");
Serial.println(redvalue);
Serial.println("b = ");
Serial.println(bluevalue);
Serial.println("g = ");
Serial.println(greenvalue);
analogWrite(RED, redvalue);
analogWrite(GREEN, greenvalue);
analogWrite(BLUE, bluevalue);
}
void number(int x) {
if (!func)
settime(x, timepos);
else if (edtime)
setalarm(x, timepos);
else {
Serial.println("in colors");
Serial.println(x);
switch (x) {
case 0:
digitalWrite(GREEN, 0);
digitalWrite(RED, 0);
digitalWrite(BLUE, 0);
redvalue = 0;
bluevalue = 0;
greenvalue = 0;
break;
case 1:
colorluz(0, 3, 3);
break;
case 2:
colorluz(3, 0, 3);
break;
case 3:
colorluz(3, 3, 0);
break;
case 4:
colorluz(2, 3, 3);
break;
case 5:
colorluz(3, 2, 3);
break;
case 6:
colorluz(3, 3, 2);
break;
case 7:
colorluz(1, 3, 3);
break;
case 8:
colorluz(3, 1, 3);
break;
case 9:
colorluz(3, 3, 1);
break;
}
}
}
void IRcontrol() {
alarmActive = false;
lcdlight(true);
lcd.setCursor(0, 1);
Serial.println(irrecv.decodedIRData.decodedRawData);
switch (irrecv.decodedIRData.decodedRawData) {
case 0xBA45FF00:
Serial.println("POWER");
if (alarmActive)
alarmActive = false;
else
alarmActive = true;
break;
case 0xB847FF00:
Serial.print("FUNC/STOP");
if (func)
editTime();
else if (edtime)
editalarm();
else {
func = true;
edtime = true;
}
break;
case 0xB946FF00: Serial.print("VOL+"); break;
case 0xBB44FF00: Serial.print("FAST BACK"); break;
case 0xBF40FF00:
Serial.print("PAUSE");
if (!func && edtime) {
savetime(false);
lcd.clear();
}
break;
case 0xBC43FF00: lcd.print("FAST FORWARD"); break;
case 0xF807FF00: lcd.print("DOWN"); break;
case 0xEA15FF00: lcd.print("VOL-"); break;
case 0xF609FF00: lcd.print("UP"); break;
case 0xE619FF00: lcd.print("EQ"); break;
case 0xF20DFF00: lcd.print("ST/REPT"); break;
case 0xE916FF00: number(0); break;
case 0xF30CFF00: number(1); break;
case 0xE718FF00: number(2); break;
case 0xA15EFF00: number(3); break;
case 0xF708FF00: number(4); break;
case 0xE31CFF00: number(5); break;
case 0xA55AFF00: number(6); break;
case 0xBD42FF00: number(7); break;
case 0xAD52FF00: number(8); break;
case 0xB54AFF00: number(9); break;
default:
Serial.println("other button");
}
last_decodedRawData = irrecv.decodedIRData.decodedRawData;
}
//DHT sensor check if available
static bool measure_environment(float *temperature, float *humidity) {
if (dht_sensor.measure(temperature, humidity) == true) {
return (true);
}
return (false);
}
void printDHT() {
float temperature;
float humidity;
if (measure_environment(&temperature, &humidity) == true) {
lcd.setCursor(0, 0);
lcd.print(temperature, 1);
lcd.print(" C, H= ");
lcd.print(humidity, 1);
lcd.println("%");
}
}
void setMillis(unsigned long new_millis) {
uint8_t oldSREG = SREG;
cli();
unsigned long timer0_millis = new_millis;
SREG = oldSREG;
time = 0;
lasttime = 0;
}
void printTime() {
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(h1);
lcd.print(h2);
lcd.print(":");
lcd.print(m1);
lcd.print(m2);
lcd.print(" ");
lcd.print(sec);
}
void loop() {
if (999ul < firstTime && firstTime < 1040ul) {
lcd.clear();
firstTime = 1050ul;
} else if (firstTime < 1050ul) {
Serial.println(time);
firstTime = firstTime + 1ul;
}
if (time > lasttime + 499ul && firstTime > 1000ul) { //start half a second
if (lcdlightcount < 120)
lcdlightcount = lcdlightcount + 1;
else if (lcdlightcount < 125)
lcdlight(false);
if (func)
printDHT();
if (s)
s = false;
else
s = true;
advancetime();
chkalarm();
lcd.setCursor(0, 1);
if (s && func) {
printTime();
}
else if (func) {
printTime();
}
if (irrecv.decode()) {
IRcontrol();
irrecv.resume();
}
lasttime = time;
} // end half a second
// musica
if (time > nextNote && firstTime > 1000ul && alarmActive) {
if (irrecv.decode()) {
IRcontrol();
irrecv.resume();
}
digitalWrite(lcdPin, LOW);
if (istone) {
analogWrite(RED, 200);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
} else {
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 200);
}
//noTone(buzzer);
if (thisNote >= 0 && thisNote < notes * 2) {
divider = melody[thisNote + 1];
if (divider > 0) {
noteDuration = (wholenote) / divider;
} else if (divider < 0) {
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
tone(buzzer, melody[thisNote], noteDuration * 0.9);
thisNote = thisNote + 2;
nextNote = noteDuration + millis();
if (istone)
istone = false;
else
istone = true;
}
}
//reset millis after a month
if (time > 28002880ul)
setMillis(0);
time = millis();
}
`