Hi guys,
I am working in a project for my foosball table. I want to play random audio files during the match (usually longer than 1 minute each) in an infinity loop until an IR sensor is broken (a goal has been scored). Once this happens, I want to stop the ongoing audio, trigger another one (the announcer yelling goal) and and change the score on my 7-segment LED. Everything is working fine on its own. My problem is when I put everything to work simultaneously.
My problem:
The way I am coding this, I keep my arduino busy all he time, so it never gets a chance to read the IR input and trigger the "goal" loop. This is my first arduino project (very ambitious, I know) so I am kind of stuck with knowing what to do. I am just looking for some advises to guide me through the easier solution. I am not sure whether I need to work with two arduinos or simple using the right code structure would work just fine. I am using arduino uno and the Adafruit Audio FX Sound Board to play the audio files. This is the code: Thanks a lot!!!!
/*
Adapted from teacher Morgenthaler and student Leo at Swiss school www.oszt.ch
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"
#include <Adafruit_Soundboard.h>
#include "SoftwareSerial.h"
long randNumber;
long randGol;
// Initailizing the 7-segment display
Adafruit_7segment matrix = Adafruit_7segment();
// Defining varialbes for the two goals
int Rot;
int Blau;
// Defining Arduino's board-LED. For controlling purpose only during coding
#define LEDPIN 13
// Defining the two pins the data connections of the breakbeam sensors
#define SENSORPIN 4
#define SENSORPIN2 2
// Defining the pin for the back to zero button
#define SENSORPIN3 7
#define SFX_TX 3
#define SFX_RX 6
// Connect to the RST pin on the Sound Board
#define SFX_RST 10
SoftwareSerial ss = SoftwareSerial(SFX_TX, SFX_RX);
Adafruit_Soundboard sfx = Adafruit_Soundboard(&ss, NULL, SFX_RST);
// Initalizing and defining states for the breakbeam sensors and the back to zero button
int sensorState = 0, lastState=0;
int sensorState2 = 0, lastState2=0;
int sensorState3 = 0, lastState3=0;
char *soundName[] = {
"01 OGG",
"02 OGG",
"03 OGG",
"04 OGG"
};
char *golSound[] = {
"gol1 OGG",
"gol2 OGG"
};
void setup() {
Serial.begin(9600);
// Initializing Arduino's board LED for output
pinMode(LEDPIN, OUTPUT);
// Initializing pin for input from breakbeam sensor
pinMode(SENSORPIN, INPUT);
// Activating the pullup resistor for this pin to make the two sensor states clear for Arduino
digitalWrite(SENSORPIN, HIGH);
// Same for other breakbeam sensor
pinMode(SENSORPIN2, INPUT);
digitalWrite(SENSORPIN2, HIGH);
// Same for back to zero button
pinMode(SENSORPIN3, INPUT);
digitalWrite(SENSORPIN3, HIGH);
// Start value for goals
Rot = 0;
Blau = 0;
//Define address for display
matrix.begin(0x70);
ss.begin(9600);
randomSeed(analogRead(0));
sfx.playTrack("inicio OGG");
}
void loop(){
randNumber = random(5);
sfx.playTrack(soundName[randNumber]);
// Next 3 lines: Read and save states of the 2 sensors and the back to zero button
sensorState = digitalRead(SENSORPIN);
sensorState2 = digitalRead(SENSORPIN2);
sensorState3 = digitalRead(SENSORPIN3);
// The following lines are for displaying the the goal values on the display.
// This if argument prevents from displaying a zero with numbers under 10
if(Rot / 10)
// Display position 0 will show the first position of goal variable Rot
matrix.writeDigitNum(0, (Rot / 10) );
// Display position 1 will show the second position of goal variable Rot
matrix.writeDigitNum(1, Rot % 10 );
// A colon will be displayed between the two varialbes
matrix.drawColon(true);
// This if argument prevents from displaying a zero for other variable
if(Blau / 10)
// Display position 3 will show the first position of goal variable Blau
matrix.writeDigitNum(3, (Blau / 10) );
// Display position 4 will show the second position of goal variable Blau
matrix.writeDigitNum(4, Blau % 10 );
// Display everyting above
matrix.writeDisplay();
// 3 if-else arguments following for the two breakbeam sensors and the back to zero button
// If sensor light beam is broken, system reads status low and turns the control LED on
if (sensorState == LOW) {
digitalWrite(LEDPIN, HIGH);
}
// otherwise no power for the control LED
else {
digitalWrite(LEDPIN, LOW);
}
// If SensorState does not differ from lastState...
// (The "!" reverses the signal state to function correclty.
// Without it, the goal values constantly increase when light beams ar UNbroken)
if (sensorState && !lastState) {
// ...The message Unbroken would be displayed on Arduino's serial monitor
Serial.println("1 Unbroken");
}
// If it differs, "Broken" would be displayed and the goal variable Red is increased.
if (!sensorState && lastState) {
Serial.println("1 Broken");
randGol = random(2);
Rot = Rot + 1;
sfx.playTrack(golSound[randGol]);
}
// Same for other breakbeam sensor
if (sensorState2 && !lastState2) {
Serial.println("2 Unbroken");
}
if (!sensorState2 && lastState2) {
Serial.println("2 Broken");
Blau = Blau + 1;
sfx.playTrack("1001 OGG");
}
// Check status of back to zero button
if (sensorState3 && !lastState3) {
}
// If pressed, the serial display would show "reset" and both goal variables are reset to zero.
if (!sensorState3 && lastState3) {
Serial.println("reset");
Blau = 0;
Rot = 0;
// Clears everything displayed on LED
matrix.clear();
}
// Save the last sensor states
lastState = sensorState;
lastState2 = sensorState2;
lastState3 = sensorState3;
}
// End of programm. Restart void loop