G'day all. I am very basic at programming and I would like some direction. The jest of what I am trying to do is play random audio files from sd card by pushes from 4 separate buttons. Each button representing a period in time of music, Classic, Baroque, Contemporary, Baroque. Button 1 pushed it will play a random file from sd card, push it again and another random file is played. Button 2, 3, 4 same but for other periods of music. Playing the files is not the issue, I believe it is the switch commands I am trying to use, or lack of understanding of brackets?? As of now when I start the arduino (reset) it will loop through the songs on the sd card, starting with baroque, alternating to classic then baroque then classic etc.... I have only written code for 1 and 2 buttons so far so it would be easy to troubleshoot. If I comment out the whole button 2 and just keep button 1 in it works great, but not when I uncomment the button 2 stuff. ANy help would be greatly appreciated.
#include <SoftwareSerial.h>
#include <SerialLCD.h>
#include <SD.h>
#include <TMRpcm.h>
#include <SPI.h>
#define SD_ChipSelectPin 10
#define DEBOUNCE 5 // button debouncer
TMRpcm tmrpcm; // create an object for use in this sketch
unsigned long time = 0;
SerialLCD slcd(7,8); // assign LCD pins
const int buttonPin1 = 1; // the number of the Arduino pin that button is connected
const int buttonPin2 = 2;
//const int buttonPin3 = 3;
//const int buttonPin4 = 4;
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
//int buttonState3 = 0;
//int buttonState4 = 0;
void setup(){
tmrpcm.speakerPin = 9; // Pin that is output for speaker
pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
//pinMode(buttonPin3, INPUT);
//pinMode(buttonPin4, INPUT);
digitalWrite(buttonPin1, LOW);
digitalWrite(buttonPin2,LOW);
slcd.begin(); //initialize LCD
if (!SD.begin(SD_ChipSelectPin)) { // see if the card is present and can be initialized
slcd.print("CHECK CARD");
delay(1000);
slcd.clear();
return; // don't do anything more if not
}
else{
slcd.print("CARD OK");
delay(1000);
slcd.clear();
delay(1000);
tmrpcm.play("Pacman.wav"); //the sound file "music" will play each time the arduino powers up, or is reset
slcd.print("WELCOME PLAYERS");
delay(4000);
slcd.clear();
slcd.setCursor(0,0);
slcd.print("SELECT PERIOD");
slcd.setCursor(0,1);
slcd.print("1, 2, 3, 4");
}
}
void loop(){
buttonState1 = digitalRead(buttonPin1); // read the state of the pushbutton value
buttonState2 = digitalRead(buttonPin2);
if (buttonState1 == HIGH) {
slcd.clear();
slcd.setCursor(0,0);
slcd.print("Baroque");
switch (random(1,8)) {
case 1:
tmrpcm.play("1.WAV");
delay(8000);
slcd.setCursor(0,1);
slcd.print("1.WAV");
break;
case 2:
tmrpcm.play("2.WAV");
delay(6000);
slcd.setCursor(0,1);
slcd.print("2.WAV");
break;
case 3:
tmrpcm.play("3.WAV");
delay(11000);
slcd.setCursor(0,1);
slcd.print("3.WAV");
break;
case 4:
tmrpcm.play("4.WAV");
delay(8000);
slcd.setCursor(0,1);
slcd.print("4.WAV");
break;
case 5:
tmrpcm.play("5.WAV");
delay(14000);
slcd.setCursor(0,1);
slcd.print("5.WAV");
break;
case 6:
tmrpcm.play("6.WAV");
delay(15000);
slcd.setCursor(0,1);
slcd.print("6.WAV");
break;
case 7:
tmrpcm.play("7.WAV");
delay(10000);
slcd.setCursor(0,1);
slcd.print("7.WAV");
break;
case 8:
tmrpcm.play("8.WAV");
delay(12000);
slcd.setCursor(0,1);
slcd.print("8.WAV");
break;
buttonState1 = buttonState1;
}
}else if (buttonState2 == HIGH); {
slcd.clear();
slcd.setCursor(0,0);
slcd.print("Classic");
switch (random(9,20)) {
case 9:
tmrpcm.play("9.WAV");
delay(8000);
slcd.setCursor(0,1);
slcd.print("9.WAV");
break;
case 10:
tmrpcm.play("10.WAV");
delay(6000);
slcd.setCursor(0,1);
slcd.print("10.WAV");
break;
case 11:
tmrpcm.play("11.WAV");
delay(11000);
slcd.setCursor(0,1);
slcd.print("11.WAV");
break;
case 12:
tmrpcm.play("12.WAV");
delay(8000);
slcd.setCursor(0,1);
slcd.print("12.WAV");
break;
case 13:
tmrpcm.play("13.WAV");
delay(14000);
slcd.setCursor(0,1);
slcd.print("13.WAV");
break;
case 14:
tmrpcm.play("14.WAV");
delay(15000);
slcd.setCursor(0,1);
slcd.print("14.WAV");
break;
case 15:
tmrpcm.play("15.WAV");
delay(10000);
slcd.setCursor(0,1);
slcd.print("15.WAV");
break;
case 16:
tmrpcm.play("16.WAV");
delay(12000);
slcd.setCursor(0,1);
slcd.print("16.WAV");
break;
case 17:
tmrpcm.play("17.WAV");
delay(14000);
slcd.setCursor(0,1);
slcd.print("17.WAV");
break;
case 18:
tmrpcm.play("18.WAV");
delay(15000);
slcd.setCursor(0,1);
slcd.print("18.WAV");
break;
case 19:
tmrpcm.play("19.WAV");
delay(10000);
slcd.setCursor(0,1);
slcd.print("19.WAV");
break;
case 20:
tmrpcm.play("20.WAV");
delay(12000);
slcd.setCursor(0,1);
slcd.print("20.WAV");
break;
buttonState2 = buttonState2;
}
}
}