It's just some play code, so some things might be written a bit funny, maybe that's the issue... Well here it is:
#include <IRremote.h>
#include <NewTone.h>
const int irPin = 4;
IRrecv irrecv(irPin);
decode_results results;
const int RLED = 13;
const int GLED = 11;
const int YLED = 10;
const int Button1 = 3;
const int Button2 = 2;
const int Speaker = 12;
int i = 0;
// Mario sound functions
int length = 295; // the number of notes
char notes[] = "EE E CE G g C g e a b ia gEGA FG E CDb C g e a b ia gEGA FG E CDb GNFR E uaC aCD GNFR E 1 11 GNFR E uaC aCD L D C CC C CD EC ag CC C CDE CC C CD EC ag EE E CE G g C g e a b ia gEGA FG E CDb C g e a b ia gEGA FG E CDb EC g u aF Fa bAAAGFEC ag EC g u aF Fa bF FFEDCe ec "; // a space represents a rest
float beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //Page 1
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 4, //Page 2
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, //Page4
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //Page 5
1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1.3, 1.3, 1.3, 1.3, 1.3, 1.3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1.3, 1.3, 1.3, 1, 1, 1, 1, 1, 1, 2 }; //Page 6
int tempo = 95;
int l = 0; // LED control during musid
void playTone(int ton1, int duration) {
for (long i = 0; i < duration * 1000L; i += ton1) {
switch (l) {
case 0:
digitalWrite(GLED, LOW);
digitalWrite(YLED, HIGH);
l++;
break;
case 1:
digitalWrite(YLED, LOW);
digitalWrite(RLED, HIGH);
l++;
break;
case 2:
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
l = 0;
break;
}
NewTone(Speaker, ton1);
delayMicroseconds(ton1);
}
noNewTone(Speaker);
}
void playNote(char note, int duration) {
// c c# d d# e f f# g g# a a# b
char names[] = { ' ', '!', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', 'i', 'N', 'R', 'u', '1', 'L', 'k'}; // [i = b flat] [N = G flat] [R = D#] [u = g#] [1 = C oct. 5] [L = E flat]
int tones[] = { 0, 1046, 138, 146, 155, 164, 174, 184, 195, 207, 220, 233, 246, 261, 293, 329, 349, 391, 440, 493, 523, 587, 659, 698, 783, 880, 987, 466, 740, 622, 415, 1046, 622u, 227};
// play the tone corresponding to the note name
for (int i = 0; i < 34; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
// End of Mario sound functions
void setup()
{
irrecv.enableIRIn();
pinMode(RLED, OUTPUT);
pinMode(GLED, OUTPUT);
pinMode(YLED, OUTPUT);
pinMode(Button1, INPUT);
pinMode(Button2, INPUT);
pinMode(Speaker, OUTPUT);
// Check output connections
digitalWrite(RLED, HIGH);
delay(50);
digitalWrite(RLED, LOW);
digitalWrite(GLED, HIGH);
delay(50);
digitalWrite(GLED, LOW);
digitalWrite(YLED, HIGH);
delay(50);
digitalWrite(YLED, LOW);
NewTone(Speaker, 1000, 50);
//Serial.begin(9600);
}
void loop()
{
if (irrecv.decode(&results)) {
//Serial.println(results.value);
if (results.value == 16767015)
{
digitalWrite(GLED, HIGH);
delay(200);
}
if (results.value == 16726215)
{
digitalWrite(YLED, HIGH);
delay(200);
}
if (results.value == 16750695)
{
digitalWrite(RLED, HIGH);
delay(200);
}
if (results.value == 16748655)
{
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 4);
}
}
irrecv.resume();
}
int value = analogRead(5);
if (value <= 50)
{
digitalWrite(YLED, HIGH);
}else{
digitalWrite(YLED, LOW);
}
if (digitalRead(Button1) == HIGH && digitalRead(Button2) == LOW)
{
digitalWrite(RLED, HIGH);
}else{
digitalWrite(RLED, LOW);
}
if (digitalRead(Button2) == HIGH && digitalRead(Button1) == LOW)
{
for (i=0; i<255; i++)
{
analogWrite(GLED, i);
delay(5);
if (digitalRead(Button1) == HIGH)
{
break;
}
}
for (i=255; i>0; i--)
{
analogWrite(GLED, i);
delay(5);
if (digitalRead(Button1) == HIGH)
{
break;
}
}
}else{
digitalWrite(GLED, LOW);
}
if (digitalRead(Button1) == HIGH && digitalRead(Button2) == HIGH)
{
digitalWrite(RLED, HIGH);
digitalWrite(GLED, LOW);
NewTone(Speaker, 2000, 250);
delay(250);
digitalWrite(GLED, HIGH);
digitalWrite(RLED, LOW);
NewTone(Speaker, 1000, 250);
delay(250);
}
}
Everything is wired correctly, every single thing works, except for the fading, again only when I introduce the one line in the setup() function...