Hello!
in this code everything works perfectly including the songs (cccp and pirates) as shown in the boxes. The songs are meant to respond to a rotary encoders (count) to stop or start.
This is where it gets strange the songs individually are fully operational and tested but, when there both in code and ones not commented out nothing works anymore.
the song plays when count is 9 but wont stop if not 9.
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
#include "pitches.h"
// Defining frequency of each music note
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_D5 587
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_G5 784
#define NOTE_A5 880
#define NOTE_B5 988
// Music notes of the song, 0 is a rest/pulse
int notes[] = {
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
NOTE_C5, NOTE_A4, NOTE_B4, 0,
NOTE_A4, NOTE_A4,
//Repeat of first part
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
NOTE_C5, NOTE_A4, NOTE_B4, 0,
//End of Repeat
NOTE_E5, 0, 0, NOTE_F5, 0, 0,
NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
NOTE_D5, 0, 0, NOTE_C5, 0, 0,
NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4,
NOTE_E5, 0, 0, NOTE_F5, 0, 0,
NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
NOTE_D5, 0, 0, NOTE_C5, 0, 0,
NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4};
// Durations (in ms) of each music note of the song
// Quarter Note is 250 ms when songSpeed = 1.0
int durations[] = {
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 125, 250, 125,
125, 125, 250, 125, 125,
250, 125, 250, 125,
125, 125, 250, 125, 125,
125, 125, 375, 375,
250, 125,
//Rpeat of First Part
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 125, 250, 125,
125, 125, 250, 125, 125,
250, 125, 250, 125,
125, 125, 250, 125, 125,
125, 125, 375, 375,
//End of Repeat
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 125, 125, 125, 375,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 500,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 125, 125, 125, 375,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 500};
int melody[] = {
NOTE_G4, NOTE_C5, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4,
NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_C4,
NOTE_D4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_D5,
NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_B4, NOTE_G4,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4,
NOTE_A4, NOTE_G4, NOTE_F4, NOTE_G4, NOTE_C4, NOTE_C4,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_B4, NOTE_C5, NOTE_D5,
NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4, NOTE_C5, NOTE_D5, NOTE_G4, NOTE_G4, NOTE_B4, NOTE_C5, NOTE_D5,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_E4, NOTE_E4, NOTE_G4, NOTE_A4, NOTE_B4,
NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_C5, NOTE_F5,
NOTE_F5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_C5, NOTE_C5,
NOTE_D5, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_B4, NOTE_C5, NOTE_A4, NOTE_A4,
NOTE_C5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_C4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};
int noteDurations[] = {
8, 4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 8, 8,
4, 8, 8, 4, 8, 8, 4, 8, 8, 2,
4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 8, 8,
4, 6, 16, 4, 6, 16,
4, 6, 16, 8, 8, 8, 8,
2, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8,
2, 8, 8, 8, 8, 3, 8, 8, 8, 8, 8,
4, 6, 16, 4, 6, 16, 4, 8, 8, 2,
2, 8, 8, 8, 8, 3, 8, 2,
2, 8, 8, 8, 8, 3, 8, 2,
4, 6, 16, 4, 4, 2, 4, 4, 1
};
const float songSpeed = 1.0;
#define redLED 6
#define greenLED 5
#define blueLED 13
int red = 6; //select the pin for the red LED
int green =5;// select the pin for the green LED
int blue =13; // select the pin for the blue LED
int CLK = 2;//CLK->D2
int DT = 3;//DT->D3
int SW = 4;//SW->D4
const int interrupt0 = 0;// Interrupt 0 ε¨ pin 2 δΈ
int count = 0;//Define the count
int lastCLK = 0;//CLK initial value
int btn = 1;
int btncount;
int buzzerPin = A3;
void setup() {
Serial.begin(9600);
pinMode(btn,INPUT);
//RGB LED set as output
pinMode(redLED,OUTPUT);
pinMode(greenLED,OUTPUT);
pinMode(blueLED,OUTPUT);
digitalWrite(redLED,LOW);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,LOW);
pinMode(btn, INPUT);
pinMode(buzzerPin, OUTPUT);
noTone(buzzerPin);
pinMode(SW, INPUT);
analogWrite(SW, HIGH);
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
attachInterrupt(interrupt0, ClockChanged, CHANGE);//Set the interrupt 0 handler, trigger level change
pinMode(btn, INPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(5, 1);
lcd.print("start");
delay(5000);
lcd.clear();
lcd.setCursor(4, 1);
lcd.print("welcome");
delay(2000);
count = 20;
}
void loop() {
/*********************** LEDS MENU ************************/
if(count <= 30 && count >= 15)
{
digitalWrite(redLED,LOW);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,LOW);
lcd.setCursor(6, 0);
lcd.print("Menu");
lcd.setCursor(0, 1);
lcd.print("30= LED 13=Song");
delay(500);
lcd.clear();
/*********************** LEDS ************************/
while(count >=30){
digitalWrite(redLED,LOW);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,LOW);
lcd.setCursor(3, 0);
lcd.print("LED menu");
delay(2000);
lcd.clear();
//MENUABOVE
lcd.setCursor(0, 1);
lcd.print("39 for red");
delay(3000);
lcd.clear();
while (count <= 39 && count >=35 )
{
delay(500);
lcd.clear();
lcd.print("red");
digitalWrite(redLED,HIGH);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,LOW);
}
lcd.setCursor(0, 0);
lcd.print("49 for green");
delay(2000);
lcd.clear();
while (count <= 49 && count >=45 )
{
delay(500);
lcd.clear();
lcd.print("Green");
digitalWrite(redLED,LOW);
digitalWrite(greenLED,HIGH);
digitalWrite(blueLED,LOW);
}
lcd.setCursor(1, 1);
lcd.print("49 for blue");
delay(2000);
lcd.clear();
while (count <= 59 && count >=55 )
{
delay(500);
lcd.clear();
lcd.print("Blue");
digitalWrite(redLED,LOW);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,HIGH);
}
lcd.setCursor(0, 0);
lcd.print("69 for purple");
delay(2000);
lcd.clear();
while (count <= 69 && count >=65)
{
delay(500);
lcd.clear();
lcd.print("Purple");
digitalWrite(redLED,HIGH);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,HIGH);
}
lcd.setCursor(1, 1);
lcd.print("79 for white");
delay(2000);
lcd.clear();
while (count <= 79 && count >=75)
{
delay(500);
lcd.clear();
lcd.print("White");
digitalWrite(redLED,HIGH);
digitalWrite(greenLED,HIGH);
digitalWrite(blueLED,HIGH);
}
lcd.setCursor(0, 0);
lcd.print("90 to menu");
delay(3000);
lcd.clear();
while (count >= 85){
lcd.print("going back");
delay(2000);
count = 20;
lcd.clear();
return;
}
}
}
/*************SONG MENU + CODE*************/
if(count <= 14 )
{
lcd.setCursor(6, 0);
lcd.print("Menu");
lcd.setCursor(0, 1);
lcd.print("9=cccp 3=pirate");
delay(1000);
lcd.clear();
}
/***************CCCP*********************/
if(count == 9 )
{
if( count !=9 ){
return;
}
// Execute once button has been released
for ( int thisNote = 0; thisNote < sizeof( melody ) / 2; thisNote++ )
{
// Check for a button press
if (count != 9)
{
// Break from loop once button is released
lcd.print("reeeeeed");
delay(3000);
count = 14;
break;
}
int noteDuration = 2000 / noteDurations[thisNote];
tone( buzzerPin, melody[thisNote], noteDuration );
lcd.clear();
int pauseBetweenNotes = noteDuration * 1.30;
delay( pauseBetweenNotes );
digitalWrite(redLED,HIGH);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,LOW);
}
}
/***************CCCP*********************/
/**************PIRATES*****************/
if(count == 3 ){
const int totalNotes = sizeof(notes) / sizeof(int);
// Loop through each note
for (int i = 0; i < totalNotes; i++)
{
if(count != 3)
{
delay(100);
lcd.clear();
lcd.print("yarrrr");
count = 14;
return;
break;
}
const int currentNote = notes[i];
float wait = durations[i] / songSpeed;
// Play tone if currentNote is not 0 frequency, otherwise pause (noTone)
digitalWrite(redLED,HIGH);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,HIGH);
if (currentNote != 0)
{
tone(buzzerPin, notes[i], wait); // tone(pin, frequency, duration)
}
else
{
noTone(buzzerPin);
}
// delay is used to wait for tone to finish playing before moving to next loop
delay(wait);
}
}
/***************PIRATES*******************/
//VoidLoop end
}
//
//The interrupt handlers
void ClockChanged()
{
int clkValue = digitalRead(CLK);//Read the CLK pin level
int dtValue = digitalRead(DT);//Read the DT pin level
if (lastCLK != clkValue)
{
lastCLK = clkValue;
count += (clkValue != dtValue ? 1 : -1);//CLK and inconsistent DT + 1, otherwise - 1
delay(500);
lcd.clear();
lcd.print(count);
}
}