Hey everyone, i am new to arduino and i am working on a project that can get user inputs from LCD keypad shield and by using those inputs, generates a random melody. I have programmed the LCD menu and tested it. I have also programmed the random melody maker and tested it. They work just fine but when i try to use them at the same time LCD works but groove speaker doesn't. I have tried to debug it but couln't find the problem. Here is my code:
#define SPEAKER 16
#include <LiquidCrystal.h>
int lasttempo=0,tempo=0,lastnumber,notenumber;
int notes[]={440,493,523,587,659,698,784,880,987};
int randnotes[20];
LiquidCrystal lcd(8,9,4,5,6,7);
void setup()
{
lcd.begin(16,2);
lcd.setCursor(0,0);
pinMode(SPEAKER,OUTPUT);
pinMode(A1,INPUT);
randomSeed(analogRead(1));
}
void tempodefiner();
void numberofnotes();
void loop()
{
tempodefiner();
numberofnotes();
for (int i=0;i<notenumber;i++)
{
int generatednote=random(9);
randnotes[i]=notes[generatednote];
}
for (int i=0;i<notenumber;i++)
{
int converttempo=((60/tempo)/2);
tone(SPEAKER,randnotes[i]);
delay(converttempo);
noTone(SPEAKER);
delay(converttempo);
}
}
void tempodefiner()
{
int tempo_state=analogRead(0);
while(!(600<tempo_state && tempo_state<800)){
tempo_state=analogRead(0);
if (60<tempo_state &&tempo_state<200)
{
while(60<tempo_state &&tempo_state<200)
{
tempo_state=analogRead(0);
}
lasttempo=tempo;
tempo+=10;
}
else if(200<tempo_state &&tempo_state<400)
{
while(200<tempo_state &&tempo_state<400)
{
tempo_state=analogRead(0);
}
lasttempo=tempo;
tempo -=10;
}
else if(800<tempo_state &&tempo_state<1024)
{
lasttempo=tempo;
}
if(lasttempo!=tempo)
{
lcd.clear();
}
lcd.print("Tempo: ");
lcd.print(tempo);
lcd.print("bpm");
lcd.setCursor(0,1);
lcd.println("Select to choose");
}
lcd.clear();
lcd.print("Successfull");
delay(1000);
lcd.clear();
}
void numberofnotes()
{
int number_state=analogRead(0);
while(!(600<number_state && number_state<800)){
number_state=analogRead(0);
if (60<number_state &&number_state<200)
{
while(60<number_state &&number_state<200)
{
number_state=analogRead(0);
}
lastnumber=notenumber;
notenumber +=1;
}
else if (200<number_state &&number_state<400)
{
while (200<number_state &&number_state<400)
{
number_state=analogRead(0);
}
lastnumber=notenumber;
notenumber -=1;
}
else if(800<number_state &&number_state<1024)
{
lastnumber=notenumber;
}
if(lastnumber!=notenumber)
{
lcd.clear();
}
lcd.print("Note Number: ");
lcd.print(notenumber);
lcd.setCursor(0,1);
lcd.println("Select to choose");
}
lcd.clear();
lcd.print("Successfull");
delay(1000);
lcd.clear();
}
Thanks for any suggestions.