Hello! I need help to add a fade effect to all my LEDs when pressing button 9 from my infrared remote. I tried to code it but it doesn't work. The function for fading effect is named "void Fading()" and is the very last one from this code:
//www.plusivo.com
//
#include "IRremote.h"
#include "pitches.h"
#include "math.h"
int receiver = 9; // Signal Pin of IR receiver to Arduino Digital Pin 11
int tDelay = 200;
int latchPin = 11; // (11) ST_CP [RCK] on 74HC595
int clockPin = 10; // (10) SH_CP [SCK] on 74HC595
int dataPin = 12; // (12) DS [S1] on 74HC595
int index = 0;
int buzzerPin = 13;
int brightness = 0;
float R;
const int pwmIntervals = 100;
byte leds = 0;
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
void translateIR() // takes action based on IR code received
{
switch(results.value)
{
case 0xFFA25D: Serial.println("CH-"); break;
case 0xFFE21D: Serial.println("CH+"); break;
case 0xFF629D: Serial.println("CH"); break;
case 0xFF22DD: Serial.println("Play Pacman Song"); Pacman_Song(); break;
case 0xFF02FD: Serial.println("Play Star Wars Theme"); Star_Wars_Song(); break;
case 0xFFC23D: Serial.println("Play Christmas Song"); Play_XMas_Song(); break;
case 0xFFE01F: Serial.println("VOL-"); break;
case 0xFFA857: Serial.println("VOL+"); break;
case 0xFF906F: Serial.println("EQ"); break;
case 0xFF9867: Serial.println("100+"); break;
case 0xFFB04F: Serial.println("200+"); break;
case 0xFF6897: Serial.println("Button 0"); break;
case 0xFF30CF: Serial.println("Button 1 - One After Another"); One_After_Another(); break;
case 0xFF18E7: Serial.println("Button 2 - One At a Time"); One_At_a_Time(); break;
case 0xFF7A85: Serial.println("Button 3 - Ping Pong"); Ping_Pong(); break;
case 0xFF10EF: Serial.println("Button 4 - Random LED ON"); Random_LED_ON(); break;
case 0xFF38C7: Serial.println("Button 5 - Marquee"); Marquee(); break;
case 0xFF5AA5: Serial.println("Button 6 - Binary Count"); Binary_Count(); break;
case 0xFF42BD: Serial.println("Button 7 - Zig Zag"); Zig_Zag(); break;
case 0xFF4AB5: Serial.println("Button 8 - Rotation"); Rotation(); break;
case 0xFF52AD: Serial.println("Button 9 - Fading"); Fading(); break;
case 0xFFFFFFFF: Serial.println("REPEAT");break;
default:
Serial.println("other button");
}// End Case
delay(500); // Do not get immediate repeat
}
void setup()
{
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, 0);
digitalWrite(latchPin, HIGH);
R = (pwmIntervals*log10(2))/(log10(255));
}
void shiftWrite(int Pin, boolean State){
bitWrite(leds,Pin,State);
digitalWrite(latchPin, HIGH);
shiftOut(dataPin, clockPin, MSBFIRST, leds);
digitalWrite(latchPin, LOW);
}
void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
irrecv.resume(); // receive the next value
}
}
void Pacman_Song(){
int melody[] = {
NOTE_B4, NOTE_B5, NOTE_FS5, NOTE_DS5,
NOTE_B5, NOTE_FS5, NOTE_DS5, NOTE_C5,
NOTE_C6, NOTE_G6, NOTE_E6, NOTE_C6, NOTE_G6, NOTE_E6,
NOTE_B4, NOTE_B5, NOTE_FS5, NOTE_DS5, NOTE_B5,
NOTE_FS5, NOTE_DS5, NOTE_DS5, NOTE_E5, NOTE_F5,
NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_B5
};
int durations[] = {
16, 16, 16, 16,
32, 16, 8, 16,
16, 16, 16, 32, 16, 8,
16, 16, 16, 16, 32,
16, 8, 32, 32, 32,
32, 32, 32, 32, 32, 16, 8
};
int size = sizeof(durations)/sizeof(int);
for(int i=0;i<=1;i++){
for(int note=0; note<size; note++){
int duration = 1900/durations[note];
tone(buzzerPin, melody[note], duration);
int pauseBetweenNotes = duration*1.30;
delay(pauseBetweenNotes);
noTone(buzzerPin);
}
}
}
void Star_Wars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
};
int size = sizeof(durations)/sizeof(int);
for(int i=0;i<=1;i++){
for(int note=0; note<size; note++){
int duration = 1000/durations[note];
tone(buzzerPin, melody[note], duration);
int pauseBetweenNotes = duration*1.30;
delay(pauseBetweenNotes);
noTone(buzzerPin);
}
}
}
void Play_XMas_Song(){
int melody[] = {
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,
NOTE_E5,
NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,
NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,
NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,
NOTE_D5, NOTE_G5
};
int durations[] = {
8, 8, 4,
8, 8, 4,
8, 8, 8, 8,
2,
8, 8, 8, 8,
8, 8, 8, 16, 16,
8, 8, 8, 8,
4, 4
};
int size = sizeof(durations)/sizeof(int);
for(int i=0;i<=1;i++){
for(int note=0; note<size; note++){
int duration = 1000/durations[note];
tone(buzzerPin, melody[note], duration);
int pauseBetweenNotes = duration*1.30;
delay(pauseBetweenNotes);
noTone(buzzerPin);
}
}
}
void One_After_Another(){
for(int i=0;i<=4;i++){
for(index=0; index<=7; index++){
shiftWrite(index, HIGH);
delay(tDelay);
}
for(index=7; index>=0; index--){
shiftWrite(index, LOW);
delay(tDelay);
}
}
}
void One_At_a_Time(){
for(int i=0;i<=7;i++){
for(index=0; index<=7; index ++){
shiftWrite(index, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
}
}
}
void Ping_Pong(){
for(int i=0; i<=4; i++){
for(index=0; index<=7; index++){
shiftWrite(index, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
}
for(index=7; index>=0; index--){
shiftWrite(index, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
}
}
}
void Random_LED_ON(){
for(int i=0; i<=69; i++){
index=random(8);
shiftWrite(index, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
}
}
void Marquee(){
for(int i=0; i<=4;i++){
for(index=0; index<=3; index++){
shiftWrite(index, HIGH);
shiftWrite(index+4, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
shiftWrite(index+4, LOW);
}
}
}
void Binary_Count(){
for(int i=0;i<=255;i++){
int delayCount = 500;
shiftOut(dataPin, clockPin, MSBFIRST, leds);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin, LOW);
leds++;
delay(delayCount);
}
}
void Zig_Zag(){
for(int i=0;i<4;i++){
index=0;
shiftWrite(index, HIGH);
delay(tDelay);
index=5;
shiftWrite(index, HIGH);
delay(tDelay);
index=2;
shiftWrite(index, HIGH);
delay(tDelay);
index=7;
shiftWrite(index, HIGH);
delay(tDelay);
index=3;
shiftWrite(index, HIGH);
delay(tDelay);
index=6;
shiftWrite(index, HIGH);
delay(tDelay);
index=1;
shiftWrite(index, HIGH);
delay(tDelay);
index=4;
shiftWrite(index, HIGH);
delay(tDelay);
index=4;
shiftWrite(index, LOW);
delay(tDelay);
index=1;
shiftWrite(index, LOW);
delay(tDelay);
index=6;
shiftWrite(index, LOW);
delay(tDelay);
index=3;
shiftWrite(index, LOW);
delay(tDelay);
index=7;
shiftWrite(index, LOW);
delay(tDelay);
index=2;
shiftWrite(index, LOW);
delay(tDelay);
index=5;
shiftWrite(index, LOW);
delay(tDelay);
index=0;
shiftWrite(index, LOW);
delay(tDelay);
}
}
void Rotation(){
for(int i=0;i<=4;i++){
for(index=0;index<=3;index++){
shiftWrite(index, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
}
for(index=7;index>=4;index--){
shiftWrite(index, HIGH);
delay(tDelay);
shiftWrite(index, LOW);
}
}
}
void Fading(){
for(int interval=0; interval<=pwmIntervals; interval++){
brightness=pow(2,(interval/R))-1;
for(int i=0;i<=7;i++){
analogWrite(i, brightness);
}
delay(30);
}
}