hey!
I'm having some problems getting my project to work, I've gotten someone to write some code for me since i'm pretty bad at it.
but now when I upload it it never gets any further than "doPlayAudio1".
and I dont actually hear audio 1 being played
I think it might be a voltage problem?
I included the way I have everything wired now, its a bit of a mess so it also has the text with what stuff is on which pin.
#include "SPI.h"
#include "SD.h"
//#include <pcmRF.h> // kan volgens mij weg, als het zonder werkt
//#include <pcmConfig.h> // kan volgens mij weg, als het zonder werkt
#include "TMRpcm.h"
#define SD_ChipSelectPin 10 // ChipSelect op pin 10
TMRpcm music; // TMRpcm heet vanaf nu 'music'
int button1 = 7; // pin 7 connected to button 1
int button2 = 6; // pin 6 connected to button 2
int button3 = 5; // pin 5 connected to button 3
int button4 = 4; // pin 4 connected to button 4
int button5 = 3; // pin 3 connected to button 5
int ldr = A0; // pin A0 connected to ldr
long nobody = 10000; // 10 seconds
// define application states
enum AppStates
{
Nothing = 0,
PlayAudio1 = 1,
PlayAudio2 = 2,
PlayAudio3 = 3,
PlayAudio4 = 4,
PlayAudio5 = 5,
PlayAudio6 = 6,
PlayAudio7 = 7,
PlayAudio8 = 8,
PlayAudio9 = 9,
PlayAudio10 = 10
};
// define behaviors
void doNothing()
{
Serial.println("doNothing");
}
void doPlayAudio1()
{
Serial.println("doPlayAudio1");
{music.play("1.wav");} //Play song 1
}
void doPlayAudio2()
{
Serial.println("doPlayAudio2");
{music.play("2.wav");} //Play song 2
}
void doPlayAudio3()
{
Serial.println("doPlayAudio3");
{music.play("3.wav");} //Play song 3
}
void doPlayAudio4()
{
Serial.println("doPlayAudio4");
{music.play("4.wav");} //Play song 4
}
void doPlayAudio5()
{
Serial.println("doPlayAudio5");
{music.play("5.wav");} //Play song 5
}
void doPlayAudio6()
{
Serial.println("doPlayAudio6");
{music.play("6.wav");} //Play song 6
}
void doPlayAudio7()
{
Serial.println("doPlayAudio7");
{music.play("7.wav");} //Play song 7
}
void doPlayAudio8()
{
Serial.println("doPlayAudio8");
{music.play("8.wav");} //Play song 8
}
void doPlayAudio9()
{
Serial.println("doPlayAudio9");
{music.play("9.wav");} //Play song 9
}
void doPlayAudio10()
{
Serial.println("doPlayAudio10");
{music.play("10.wav");} //Play song 10
}
// logic
int state = Nothing;
struct Movement
{
int button;
int from;
int to;
};
struct Behavior
{
int state;
void (*func)();
};
const int maxMovements = 64;
Movement movements[maxMovements];
int numMovements = 0;
const int maxBehaviors = 32;
Behavior behaviors[maxBehaviors];
int numBehaviors = 0;
void addMovement(int f, int b, int t)
{
movements[numMovements].button = b;
movements[numMovements].from = f;
movements[numMovements].to = t;
numMovements++;
}
void addBehavior(int s, void (*f)())
{
behaviors[numBehaviors].state = s;
behaviors[numBehaviors].func = f;
numBehaviors++;
}
void setup() {
// setup, basically some preparations
pinMode(button1, INPUT); // button 1-5 and ldr are inputs
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(button5, INPUT);
pinMode(ldr, INPUT);
music.speakerPin = 9; // audio output on pin 7
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
music.setVolume(7); // 0 tot 7, stel volume in
music.quality(1); //Set 1 for 2x oversampling Set 0 for normal
addMovement(Nothing, button1, PlayAudio2);
addMovement(Nothing, button2, PlayAudio3);
addMovement(PlayAudio2, -1, PlayAudio7);
addMovement(PlayAudio3, -1, PlayAudio7);
addMovement(PlayAudio7, -1, PlayAudio1);
addMovement(Nothing, button3, PlayAudio4);
addMovement(PlayAudio4, -1, PlayAudio8);
addMovement(PlayAudio8, -1, PlayAudio10);
addMovement(PlayAudio10, -1, PlayAudio1);
addMovement(Nothing, button4, PlayAudio5);
addMovement(PlayAudio5, -1, PlayAudio8);
addMovement(Nothing, button5, PlayAudio6);
addMovement(PlayAudio6, -1, PlayAudio9);
addMovement(PlayAudio9, -1, PlayAudio1);
addMovement(PlayAudio1, button1, PlayAudio2);
addMovement(PlayAudio1, button2, PlayAudio3);
addMovement(PlayAudio1, button3, PlayAudio4);
addMovement(PlayAudio1, button4, PlayAudio5);
addMovement(PlayAudio1, button5, PlayAudio6);
addBehavior(Nothing, &doNothing);
addBehavior(PlayAudio1, &doPlayAudio1);
addBehavior(PlayAudio2, &doPlayAudio2);
addBehavior(PlayAudio3, &doPlayAudio3);
addBehavior(PlayAudio4, &doPlayAudio4);
addBehavior(PlayAudio5, &doPlayAudio5);
addBehavior(PlayAudio6, &doPlayAudio6);
addBehavior(PlayAudio7, &doPlayAudio7);
addBehavior(PlayAudio8, &doPlayAudio8);
addBehavior(PlayAudio9, &doPlayAudio9);
addBehavior(PlayAudio10, &doPlayAudio10);
}
int buttonPressed = 0;
long pressedMillis = 0;
void checkButtonPressed(int button)
{
if (digitalRead(button) == HIGH && millis() - pressedMillis > 1000)
{
buttonPressed = button;
pressedMillis = millis();
}
}
void goToState(int s)
{
state = s;
for (int i = 0 ; i < numBehaviors ; i++)
if (behaviors[i].state == state && behaviors[i].func != 0)
(*behaviors[i].func)();
}
void loop() {
// put your main code here, to run repeatedly:
buttonPressed = 0;
checkButtonPressed(button1);
checkButtonPressed(button2);
checkButtonPressed(button3);
checkButtonPressed(button4);
checkButtonPressed(button5);
int lightThreshold = 800;
int lightValue = analogRead(ldr);
if (buttonPressed != 0 && lightValue >= lightThreshold)
{
for (int i = 0 ; i < numMovements ; i++)
if ((movements[i].button == -1 || movements[i].button == buttonPressed) && movements[i].from == state)
{
goToState(movements[i].to);
break;
}
}
else if (state != PlayAudio1 && (millis() - pressedMillis > nobody || lightValue < lightThreshold))
goToState(PlayAudio1);
}