Programming Questions

Greetings everyone, I would like to introduce myself as someone who has just gotten into Arduino only about a month ago in their High school Class and I have been going through the lessons provided to me. Towards a certain point of completion I was wanting to bring in three components of what I learned together and making something for the Holidays. Flashing the RGB LED to a fast pace enough for it to mimic Christmas lights & colors, having the Piezo Buzzer buzzer in frequencies similar to the song “We Wish You A Merry Christmas”, and lastly displaying a message that states “Merry Christmas& Happy New Years!” on a LCD. Although I have been running into many issues over the time I have been programming it. The RGB LED is working but it’s just not following the command of keeping one of the lights on a for only a short amount of time(particularly the green light.). Not only is that happening but I’m unsure on how to change the piezo buzzers tempo and Melody without causing any effect to the RGB LED. So far this is the best working code I have for it.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,5,4,3,2);

const int RED_PIN = 6;

const int GREEN_PIN = 7;

const int BLUE_PIN = 8;

int DISPLAY_TIME = 10;

const int buzzerPin = 9;

const int songLength = 30;

char notes = “cdfda ag cdfdg gf”; // a space represents a rest

int beats = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};

int tempo = 150;

void setup()
{
pinMode(buzzerPin, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);

lcd.begin(16, 2);

lcd.clear();

lcd.print(“Merry Christmas &”);
lcd.setCursor(0,1);
lcd.print("& Happy New Year");
}

void loop()
{
// We’ve written a custom function called mainColors() that steps
// through all eight of these colors. We’re only “calling” the
// function here (telling it to run). The actual function code
// is further down in the sketch.

int i, duration;

for (i = 0; i < songLength; i++) // step through the song arrays
{
duration = beats * tempo; // length of note/rest in ms
_ if (notes == ’ ') // is this a rest?_
* {*
* delay(duration); // then pause for a moment*
* }*
* else // otherwise, play the note*
* {*
_ tone(buzzerPin, frequency(notes*), duration);
delay(duration); // wait for tone to finish*
* }
delay(tempo / 10); // brief pause between notes*_

mainColors();
}
int frequency(char note)
{
// This function takes a note character (a-g), and returns the
// corresponding frequency in Hz for the tone() function.
int i;
const int numNotes = 30; // number of notes we’re storing
// The following arrays hold the note characters and their
// corresponding frequencies. The last “C” note is uppercase
// to separate it from the first lowercase “c”. If you want to
// add more notes, you’ll need to use unique characters.

// For the “char” (character) type, we put single characters
// in single quotes.
char names[] = { ‘d’, ‘g’, ‘g’, ‘a’, ‘g’, ‘F’, ‘e’, ‘e’, ‘e’, ‘a’, ‘a’, ‘b’, ‘a’, ‘g’, ‘F’, ‘d’, ‘d’, ‘b’, ‘b’, ‘c’, ‘b’, ‘a’, ‘g’, ‘e’, ‘d’, ‘d’, ‘e’, ‘a’, ‘F’, ‘g’};
int frequencies[] = {294, 392, 392, 440, 392, 370, 330, 330, 330, 440, 440, 492, 440, 392, 370, 294, 294, 494, 494, 262, 494, 440, 392, 330, 294, 294, 330, 440, 370, 392};
// Now we’ll search through the letters in the array, and if
// we find it, we’ll return the frequency for that note.
for (i = 0; i < numNotes; i++) // Step through the notes
{
_ if (names == note)
{

return(frequencies*);
}
}
return(0);
}

void mainColors()
{
// Off (all LEDs off):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
//wait 1 second*

delay(1000);
// Red (turn just the red LED on):
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
//wait 1 seconds
delay(1000);
// Green (turn just the green LED on):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
//wait 1 second
delay(1000);
}_

We do not know what changes that the forum software has made to your improperly posted code (besides italics removing array indexes). Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

If you want the program to be responsive you will need to get rid of those delay() calls. The program stops everything during a delay(). It is called blocking.
Non-blocking timing tutorials:
Several things at a time.
Beginner’s guide to millis().
Blink without delay().

I see you have not included the ability to write to your display on your PC. Look at some of the IDE examples and almost all of them have that ability.

Then you can Serial.print() some to the values you are assuming are correct and actually verify that -yes- that one is correct. Especially useful on compares that switch something in your logic.

Paul

#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
const int RED_PIN = 6;
const int GREEN_PIN = 7;
const int BLUE_PIN = 8;
int DISPLAY_TIME = 10;
const int buzzerPin = 9;
const int songLength = 18;
char notes = "cdfda ag cdfdg gf ";
int beats = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
int tempo = 150;
void setup()

{
pinMode(buzzerPin, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.print(“Merry Christmas&”);
lcd.setCursor(0,1);
lcd.print(“Happy New Years!”);
}

void loop()
{
mainColors();
int i, duration;
for (i = 0; i < songLength; i++)
{
duration = beats * tempo; _
_ if (notes *== ’ ') *

* { *
* delay(duration); *
* }*
* else *
* { *
_ tone(buzzerPin, frequency(notes*), duration);
delay(duration);
}
delay(tempo/10);
}
}
int frequency(char note)
{

int i;
const int numNotes = 8;
char names = { ‘d’, ‘g’, ‘g’, ‘a’, ‘g’, ‘F’, ‘e’, ‘e’, ‘e’, ‘a’, ‘a’, ‘b’, ‘a’, ‘g’, ‘F’, ‘d’, ‘d’, ‘b’, ‘b’, ‘c’, ‘b’, ‘a’, ‘g’, ‘e’, ‘d’, ‘d’, ‘e’, ‘a’, ‘F’, ‘g’ };
int frequencies = {294, 392, 392, 440, 392, 370, 330, 330, 330, 440, 440, 492, 440, 392, 370, 294, 294, 494, 494, 262, 494, 440, 392, 330, 294, 294, 330, 440, 370, 392};
for (i = 0; i < numNotes; i++)
{ *
if (names *== note)
{
return(frequencies);
}
}
return(0);
} *

*void mainColors()
{
delay(200); _

digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
_ delay(200); _
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
_ delay(200);
} *_

Please read reply #1 again.