Hi. I have one LCD and an IR remote control. I made one program, that will control my amplifier trought IR. This is my code:
Part 1 (main code):
#include <IRremote.h>
int RECV_PIN = 6;
int pwr=0; //POWER
int pwrPin=7; //POWER PIN
int defVlm=111; //VOLUME
int vlm=0; //VOLUME
int vlmPin=9; //VOLUME PIN
int in=0; //INPUT MODE
int out=0; //OUTPUT MODE
int mt=0; //MUTE
int last=0; //LAST FUNCTION (VOLUME +-)
int pp=0; //PLAY/PAUSE
int playpausePin=8; //PLAY/PAUSE PIN
int rpt=0; //REPEAT MODE
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
//Serial.println(results.value, HEX);
switch (results.value) {
case 0xFFA25D: last=0;power();break; //POWER BUTTON
case 0xFF906F: last=1;plus();break; //VOLUME +
case 0xFFA857: last=2;minus();break; //VOLUME -
case 0xFFE21D: last=0;mute();break; //MUTE
case 0xFF629D: last=0;input();break; //INPUT MODE
case 0xFFB04F: last=0;output();break; //OUTPUT MODE
case 0xFF22DD: last=0;playpause();break; //PLAY/PAUSE
case 0xFF02FD: last=0;prew();break; //PREV. TRACK
case 0xFFC23D: last=0;next();break; //NEXT TRACK
case 0xFF9867: last=0;repeat();break; //REPEAT MODE
}
delay(10);
irrecv.resume(); // Receive the next value
if (results.value==4294967295) {
switch (last) {
case 1: plus();break;
case 2: minus();break;
}
}
}
}
Part 2 (functions):
//POWER
void power() {
pinMode(pwrPin,OUTPUT);
if (pwr==0) {
pwr=1;
//Reset to default
analogWrite(vlmPin, vlm);out=0;digitalWrite(pwrPin,HIGH);mt=0;pp=0;digitalWrite(playpausePin,LOW);rpt=0;in=0;vlm=defVlm;displayRefresh();
Serial.println("Power on");
}
else {
pwr=0;digitalWrite(pwrPin,LOW);
analogWrite(vlmPin, 0);
pp=0;digitalWrite(playpausePin,LOW);
Serial.println("Power off");
digitalWrite(pwrPin,LOW);
displayClear();
vlm=defVlm;
}
}
//VOLUME CONTROL
//VOLUME -
void minus() {
pinMode(vlmPin,OUTPUT);
if (vlm>0 && mt!=1 && pwr==1) {
vlm=vlm-3;
analogWrite(vlmPin, vlm);
Serial.print("Volume:");
Serial.println(vlm);
displayVol(vlm);
}
}
//VOLUME +
void plus() {
pinMode(vlmPin,OUTPUT);
if (vlm<255 && mt!=1 && pwr==1) {
vlm=vlm+3;
analogWrite(vlmPin, vlm);
Serial.print("Volume:");
Serial.println(vlm);
displayVol(vlm);
}
}
//MUTE
void mute() {
pinMode(vlmPin,OUTPUT);
if (pwr==1) {
if (mt==0) {
mt=1;analogWrite(vlmPin, 0);
Serial.println("Mute on");
}
else {
mt=0;analogWrite(vlmPin, vlm);
Serial.println("Mute off");
}
displayRefresh();
}
}
//INPUT SELECTING
void input() {
if (pwr==1) {
switch (in) {
case 4: in=0;Serial.println("Input: LineIn");pp=0;digitalWrite(playpausePin,LOW);displayRefresh();break;
case 0: in=1;Serial.println("Input: AUX");pp=0;digitalWrite(playpausePin,LOW);displayRefresh();break;
case 1: in=2;Serial.println("Input: CD");displayRefresh();break;
case 2: in=3;Serial.println("Input: USB");pp=0;digitalWrite(playpausePin,LOW);displayRefresh();break;
case 3: in=4;Serial.println("Input: Wireless Device");pp=0;digitalWrite(playpausePin,LOW);displayRefresh();break;
}
}
pinMode(playpausePin,OUTPUT);
}
//OUTPUT SELECTING
void output() {
if (pwr==1) {
if (out==0) {
out=1;digitalWrite(1,LOW);digitalWrite(2,HIGH);pwrPin=2;
Serial.println("Output: headphones");
}
else {
out=0;digitalWrite(2,LOW);digitalWrite(1,HIGH);pwrPin=1;
Serial.println("Output: speakers");
}
}
pinMode(pwrPin,OUTPUT);
}
//PLAYPAUSE
void playpause() {
if (pwr==1) {
if (in==2 || in==3) {
if (pp==0) {
pp=1;digitalWrite(playpausePin,HIGH);
Serial.println("Play");
}
else {
pp=0;digitalWrite(playpausePin,LOW);
Serial.println("Pause");
}
}
}
pinMode(playpausePin,OUTPUT);
}
//CHANGING TRACKS
//PREW. TRACK
void prew() {
if (pwr==1) {
if (in==2 || in==3) {
Serial.println("Prew. track");
}
}
}
//NEXT TRACK
void next() {
if (pwr==1) {
if (in==2 || in==3) {
Serial.println("Next track");
}
}
}
//REPEAT MODE
void repeat() {
if (pwr==1) {
if (in==2 || in==3) {
if (rpt==0) {
rpt=1;
Serial.println("Repeat mode: repeat one");
}
else {
rpt=0;
Serial.println("Repeat mode: repeat all");
}
}
pinMode(playpausePin,OUTPUT);
}
}
Part 3 (LCD):
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte speaker[8] = {
0b11000,
0b10100,
0b10010,
0b10001,
0b10001,
0b10010,
0b10100,
0b11000
};
byte mon[8] = {
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
byte moff[8] = {
0b11100,
0b00011,
0b00000,
0b11111,
0b00000,
0b00011,
0b11100,
0b00000
};
//REFRESH DISPLAY
void displayRefresh() {
lcd.begin(16, 2);
//OUTPUT TYPE's ICONS
//lcd.createChar(1, box);
//lcd.createChar(2, hp);
//MUTE ICONS
lcd.createChar(3, speaker);
lcd.createChar(4, mon);
lcd.createChar(5, moff);
lcd.setCursor(0, 0);
switch (in) {
case 0: lcd.write("Sat.");break;
case 1: lcd.write("AUX");break;
case 2: lcd.write("CD");break;
case 3: lcd.write("USB");break;
case 4: lcd.write("WD");break;
}
lcd.setCursor(14, 0);
switch (mt) {
case 0: lcd.write(5);break;
case 1: lcd.write(4);break;
}
lcd.write(3);
}
//CLEAR DISPLAY
void displayClear() {
lcd.clear();
}
//DISPLAY CURRENT VOLUME
void displayVol(int volUme) {
int volUmeP = volUme/2.55;
/*lcd.createChar(1, jeden);
lcd.createChar(2, dva);
lcd.createChar(3, tri);
lcd.createChar(4, styri);
lcd.createChar(5, pet);*/
if (volUmeP==100) {lcd.setCursor(2, 1);}
if (volUmeP<100) {lcd.setCursor(3, 1);}
lcd.write("Volume: ");
lcd.write(volUme);
lcd.write("%");
}
PS: Please do not use my code. I was hard-working on it for more than 3 days.
The problem is, that Idk how to clear display after 1sec, after I stop pushing buttons on IR remote. When I use delay(1000);lcd.clear(); it will stop my program for 1 sec, so I can't push any buttons :(. If u have any ideas how to do it, please post a reply to this post :D. Thank u :).