OK, so I've built a laser harp that has 12 beams that shine onto photo-resistors, and using the wave shield when the beam is broken it makes a noise. When the code is used on a breadboard with 12 buttons it worked fine. Our problem is when uploaded to the harp the delay between breaking the beam and a noise being played is slow, and when switching beams the transfer time is even slower. So in essence we can barely manage 1/8 notes. Do you have any ideas for why this is so slow. When I uploaded a basic code that turns on a LED when the beam is broken, the speed at which the Arduino could comprehend was definitely fast enough. My only idea is that the code reads "just released" when there is absolutely no light shown onto the sensor so it takes a little bit of time for the sensor to read completely dark. Any ideas? I'm stuck
I trimmed up the code for the preview, the rest is in the attachment
The photo resistor that I'm using is 5k
//12 string Laser Harp
//Analog 0 and A1 have 2 sensors set up
//while pins 6,7,8,9,A2,A3,A4,A5 are digital
int isButtonBeingPushed1 = 0;
int oneShotMode1 = 1; // 1 = oneshot mode on, 0 = oneshot mode off
byte buttonStateRaw1;
int isButtonBeingPushed2 = 0;
int oneShotMode2 = 1; // 1 = oneshot mode on, 0 = oneshot mode off
byte buttonStateRaw2;
#define DEBOUNCE 5 // button debouncer
byte buttons[] = {6, 7, 8, 9, 16, 17, 18, 19};
#define NUMBUTTONS sizeof(buttons)
volatile byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
void setup() {
byte i;
// Make input & enable pull-up resistors on switch pins
for (i=0; i< NUMBUTTONS; i++) {
pinMode(buttons[i], INPUT);
digitalWrite(buttons[i], HIGH);
}
}
void check_switches()
{
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
byte index;
for (index = 0; index < NUMBUTTONS; index++) {
currentstate[index] = digitalRead(buttons[index]); // read the button
if (currentstate[index] == previousstate[index]) {
if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
// just pressed
justpressed[index] = 1;
}
else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
justreleased[index] = 1;
}
pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed
}
previousstate[index] = currentstate[index]; // keep a running tally of the buttons
}
}
void loop() {
// check the button state
buttonStateRaw1 = Wire.read();
delayMicroseconds(3);
if(buttonStateRaw1 == 1 && isButtonBeingPushed1 == 0)
{
isButtonBeingPushed1 = 1;
if(oneShotMode1 == 1)
{
oneShotMode1 = 0;
}
else if(oneShotMode1 == 0)
{
oneShotMode1 = 1;
}
delay(DEBOUNCE);
}
else if(buttonStateRaw1 == 0 )
{
isButtonBeingPushed1 = 0;
}
unsigned long timeSinceButtonPush1;
int analog0;
delay(2);
timeSinceButtonPush1;
if(analog0 > 100 && analog0 < 300) // button1
{
buttonState1 = 1;
playfile("9.WAV");
while (wave.isplaying && analog0 > 100 && analog0 < 300)
analog0 = analogRead(0);
buttonState1 = 0;
}
wave.stop();
if (analog0 > 300 && analog0 < 370) // button 2
{
buttonState1 = 1;
playfile("10.WAV");
while (wave.isplaying && analog0 > 300 && analog0 < 370)
analog0 = analogRead(0);
buttonState1 = 0;
}
wave.stop();
buttonStateRaw2 = Wire.read();
delayMicroseconds(3);
if(buttonStateRaw2 == 1 && isButtonBeingPushed2 == 0)
{
isButtonBeingPushed2 = 1;
if(oneShotMode2 == 1)
{
oneShotMode2 = 0;
}
else if(oneShotMode2 == 0)
{
oneShotMode2 = 1;
}
delay(DEBOUNCE);
}
else if(buttonStateRaw2 == 0 )
{
isButtonBeingPushed2 = 0;
}
unsigned long timeSinceButtonPush2;
int analog1;
delay(2);
timeSinceButtonPush2;
if(analog1 > 150 && analog1 < 340) // button1
{
buttonState2 = 1;
playfile("11.WAV");
while (wave.isplaying && analog1 > 150 && analog1 < 340)
analog1 = analogRead(1);
buttonState2 = 0;
}
wave.stop();
if (analog1 > 340 && analog1 < 395) // button 2
{
buttonState2 = 1;
playfile("12.WAV");
while (wave.isplaying && analog1 > 340 && analog1 < 395)
analog1 = analogRead(1);
buttonState2 = 0;
}
wave.stop();
// Digital Pins
byte i;
if (justreleased[0]) {
justpressed[0] = 0;
playfile("1.WAV");
while (wave.isplaying && pressed[0]) {
//Serial.print(".");
}
wave.stop();
}
if (justreleased[1]) {
justpressed[1] = 0;
playfile("2.WAV");
while (wave.isplaying && pressed[1]) {
//Serial.print(".");
}
wave.stop();
}
if (justreleased[2]) {
justpressed[2] = 0;
playfile("5.WAV");
while (wave.isplaying && pressed[2]) {
}
wave.stop();
}
if (justreleased[3]) {
justpressed[3] = 0;
playfile("6.WAV");
while (wave.isplaying && pressed[3]) {
//Serial.print(".");
}
wave.stop();
}
if (justreleased[4]) {
justpressed[4] = 0;
playfile("3.WAV");
while (wave.isplaying && pressed[4]) {
//Serial.print(".");
}
wave.stop();
}
if (justreleased[5]) {
justpressed[5] = 0;
playfile("4.WAV");
while (wave.isplaying && pressed[5]) {
//Serial.print(".");
}
wave.stop();
}
if (justreleased[6]) {
justpressed[6] = 0;
playfile("7.WAV");
while (wave.isplaying && pressed[6]) {
//Serial.print(".");
}
wave.stop();
}
if (justreleased[7]) {
justpressed[7] = 0;
playfile("8.WAV");
while (wave.isplaying && pressed[7]) {
//Serial.print(".");
}
wave.stop();
}
}
void playcomplete(char *name) {
// call our helper to find and play this name
playfile(name);
while (wave.isplaying) {
}
}
Di8_4An_harp_code.ino (10.1 KB)