Hello there
I'm sure some may have seen me on here asking for help and thanks to those people I've done my most advanced project. I feel I've somewhat got a basic understanding now on how code works but there a lot I'm not sure on or don't even know exists.
Any advice is welcome just don't scold me too hard I'm only like 40hrs or less in ![]()
After all the comments, bar the butchery of my interupt routine anything elsere commended likestructures or?
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
#include "pitches.h"
/*empire////////////////////////////////*/
#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
// change this to make the song slower or faster
int tempo = 120;
// Ns of the moledy followed by the duration.
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted Ns,
// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
int M[] = {
// Dart Vader theme (Imperial March) - Star wars
// Score available at https://musescore.com/user/202909/scores/1141521
// The tenor saxophone part was used
NOTE_A4,-4, NOTE_A4,-4, NOTE_A4,16, NOTE_A4,16, NOTE_A4,16, NOTE_A4,16, NOTE_F4,8, REST,8,
NOTE_A4,-4, NOTE_A4,-4, NOTE_A4,16, NOTE_A4,16, NOTE_A4,16, NOTE_A4,16, NOTE_F4,8, REST,8,
NOTE_A4,4, NOTE_A4,4, NOTE_A4,4, NOTE_F4,-8, NOTE_C5,16,
NOTE_A4,4, NOTE_F4,-8, NOTE_C5,16, NOTE_A4,2,//4
NOTE_E5,4, NOTE_E5,4, NOTE_E5,4, NOTE_F5,-8, NOTE_C5,16,
NOTE_A4,4, NOTE_F4,-8, NOTE_C5,16, NOTE_A4,2,
NOTE_A5,4, NOTE_A4,-8, NOTE_A4,16, NOTE_A5,4, NOTE_GS5,-8, NOTE_G5,16, //7
NOTE_DS5,16, NOTE_D5,16, NOTE_DS5,8, REST,8, NOTE_A4,8, NOTE_DS5,4, NOTE_D5,-8, NOTE_CS5,16,
NOTE_C5,16, NOTE_B4,16, NOTE_C5,16, REST,8, NOTE_F4,8, NOTE_GS4,4, NOTE_F4,-8, NOTE_A4,-16,//9
NOTE_C5,4, NOTE_A4,-8, NOTE_C5,16, NOTE_E5,2,
NOTE_A5,4, NOTE_A4,-8, NOTE_A4,16, NOTE_A5,4, NOTE_GS5,-8, NOTE_G5,16, //7
NOTE_DS5,16, NOTE_D5,16, NOTE_DS5,8, REST,8, NOTE_A4,8, NOTE_DS5,4, NOTE_D5,-8, NOTE_CS5,16,
NOTE_C5,16, NOTE_B4,16, NOTE_C5,16, REST,8, NOTE_F4,8, NOTE_GS4,4, NOTE_F4,-8, NOTE_A4,-16,//9
NOTE_A4,4, NOTE_F4,-8, NOTE_C5,16, NOTE_A4,2,
};
// sizeof gives the number of bytes, each int value is composed of two bytes (16 bits)
// there are two values per note (pitch and duration), so for each note there are four bytes
int Ns = sizeof(M) / sizeof(M[0]) / 2;
// this calculates the duration of a whole note in ms
int wholenote = (60000 * 4) / tempo;
int divider = 0, ND = 0;
/*empire////////////////////////////////*/
// 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,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E_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 上
volatile int count = 0;//Define the count
//This fixed the song issue volatile is crucial for interupts
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("59 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 5=pirate 1=em");
digitalWrite(redLED,LOW);
digitalWrite(greenLED,LOW);
digitalWrite(blueLED,LOW);
delay(1000);
lcd.clear();
}
/***************CCCP*********************/
if(count == 9 )
{
// Execute once button has been released
for ( int thisNote = 0; thisNote < sizeof( melody ) / 2; thisNote++ )
{
if (count != 9)
{
// Break from loop once button is released
lcd.print("reeeeeed");
delay(1000);
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 == 5 ){
const int totalNotes = sizeof(notes) / sizeof(int);
// Loop through each note
for (int i = 0; i < totalNotes; i++)
{
if(count != 5)
{
delay(1000);
lcd.clear();
lcd.print("yarrrr");
count = 14;
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*******************/
/***************IMPERIAL*****************/
/*
if(count == 5 ){
// iterate over the Ns of the M.
// Remember, the array is twice the number of Ns (Ns + durations)
for (int TN = 0; TN < Ns * 2; TN = TN + 2) {
if(count != 5)
{
delay(1000);
lcd.clear();
lcd.print("yarrrr");
count = 14;
break;
}
// calculates the duration of each note
divider = M[TN + 1];
if (divider > 0) {
// regular note, just proceed
ND = (wholenote) / divider;
} else if (divider < 0) {
// dotted Ns are represented with negative durations!!
ND = (wholenote) / abs(divider);
ND *= 1.5; // increases the duration in half for dotted Ns
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(buzzerPin, M[TN], ND*0.9);
// Wait for the specief duration before playing the next note.
delay(ND);
// stop the waveform generation before the next note.
noTone(buzzerPin);
}
}
*/
// this code works there just not enough memeory rn
/***************IMPERIAL*****************/
//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);
}
}
