how to let light up LED as input for speaker.

Hi guys, im new here. I need help with my work. I am designing a whack-a-mole game (yes, I know a lot of people have done this project already). There are things that I don't know how to do it. Either I miss it or it is just impossible to do.

Anyway, here is my design. The mole is make of 5 LED. Those 5 LED will blank at any position with random sequence. This also is able to adjust the delaytime of each LED lights up. Then, there will always be 1 LED that blink twice in any position with random sequence too. So, when the LED blink twice, we press the button so it make noise.

Here is the problem, I don't know how to make the LED that blink twice as input for my speaker.
My speaker takes two input, LED and push button. I set up the code, the code compile, but it just wont be able to make the buzzer make some noise when one of the LED blink and push button is being press.

If anyone know how to set LED light up (which is ledPIN(whatever) set it as an input to make the buzzer make sound, please help me I really appreciate it. thank you

here is my code

sketch_nov13a.ino (2.12 KB)

As I read this, you have 5 LEDS blinking randomly, periodically you have one LED blink twice and sound a buzzer. When this happens, you want the player to press the button.

Is this correct?

Yes, you are correct.
So when a LED blink twice, and I press the button, the buzzer will make noise.
I wonder if this is possible.
If not, I want to change the design, probably to let all LED blinks once at random position. When the light still on and I press the button, the buzzer will make noise. If the light is off and I press the button, the buzzer wont make noise.

So, do you have any idea how to set it up? Thank you

This something that the Arduino does easily.

Your program needs to check the switch to see if it is pressed so if it is, simply turn on the output to the buzzer for a short time.

Your loop will need to randomly flash the LEDs, and when you branch off to do the double flash, you need to start looping for whatever time you want to allow, while checking the switch.

In pseudo code:

Double flash the required LED

For count = 1 to 100
Check switch for that LED
If closed
sound buzzer
Exit this loop and return to random flashing
Delay 10msec
Next count

Weedpharma

here is my code. when I run it, it compiles. but its just not making sound. But when I keep pressing it, after the light blink within 10 counts, the buzzer will make sound. so I don't know where did I do wrong

const int led[] = {1, 2, 3, 4, 5};
const int buttonPin = 7;
int count = 0;
int buttonState = 0;
int speakerPin = 13;
int potPIN = 0;
int potValue = 0;
int delayTime = 100;
int n = random (1,10);
int length = 2; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 100;
int rightLight = 1;
int j;
int win;

void randLight()
{
for(int i = 0; i <= 5; i++)
{
j = random(0,5);
if( j == rightLight)
{
potValue = analogRead(potPIN);
potValue = (potValue/3 + 10);
digitalWrite(led[j], HIGH);
delay(potValue);
digitalWrite(led[j], LOW);
delay(potValue);
digitalWrite(led[j], HIGH);
delay(potValue);
digitalWrite(led[j], LOW);
delay(potValue);
//digitalWrite(led[j], HIGH);
//delay(potValue);
//digitalWrite(led[j], LOW);
//delay(potValue);
//digitalWrite(led[j], HIGH);
//delay(potValue);
//digitalWrite(led[j], LOW);
//delay(potValue);
}
else
{
digitalWrite(led[j], HIGH);
delay(3potValue);
digitalWrite(led[j], LOW);
delay(potValue
n);
}
}
}

void playTone(int tone, int duration)
{
if (led[j] == HIGH)
{
win = led[j];
}
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && win )
{
for (long i = 0; i < duration * 1000L; i += tone * 2)
{

digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);

}
}
else
{
//digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration)
{
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++)
{
if (names == note)

  • {*
    _ playTone(tones*, duration);_
    _
    }_
    _
    }_
    _
    }_
    _
    //void turnOFF()_
    _
    //{_
    _
    //buttonState = digitalRead(buttonPin);_
    _
    //if (buttonState == HIGH)_
    _
    //{_
    _
    //digitalWrite(led[j], LOW);_
    _ //delay(potValue5);
    * //}*
    * //else*
    * //{*
    * //}*
    //}
    void setup()
    {
    * pinMode(buttonPin, INPUT);*
    * pinMode(speakerPin, OUTPUT);*
    * for (int i = 0; i < 5; i++)*
    * {*
    pinMode(led*, OUTPUT);
    pinMode(led[j], INPUT);
    }
    }
    void loop()
    {
    randLight();*_

* //turnOFF();*

* for (int i = 0; i < length; i++)*
* {*
_ if ( notes == ' ')
* {_
delay(beats _ tempo); // rest_

* } else {*
playNote(notes, beats * tempo);
* }*

* // pause between notes*
* delay(tempo / 2);*
* }*

}

As I am not proficient at C, I will leave it to others to a analyze the code.

What I suggest is that you simplify the code to simply turn on a test LED rather than trying to play a tune.
Once you are happy that the test LED lights at the time you want, then you know that the program works. Then write your tone function called from where you have the test LED turned on.

You need to break down your program to smaller parts and test each before moving on to the next. In this case, prove the LED flashing and switch reads work. Then right another sketch with only one switch and use that to play a tune. Once you have it working, combine to two.

Weedpharma

Look carefully at your code on this page. You'll notice that, at some point, it starts printing in italics (slanty writing). I'll wager a pound to a dollop of cow pat that it's because you included [i] in there.

Now edit your post and place your code between

[code]and [/code]

tags so that we can see it properly.

sorry for the messy code. I was in hurry so I just upload everything without delete the unwanted parts
here is my code

const int led[] = {1, 2, 3, 4, 5};
const int buttonPin = 7;
int potValue = 0;
int potPIN = 0;
int n = random(1, 10);
int buttonState = 0;
int speakerPin = 13;
int rightLight = 1;
int rightLight2 = 3;
int length = 2; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 100;
int win;
int j;
int k;

void randLight () //loops the lights
{

for(int i = 0; i<=5; i++)
{
j = random(0,5);
k = random(0,5);
if(j== rightLight || k== rightLight2) //This is the light that you are trying to target
{
//win = 1; //when this is one, and you press the button, you win
potValue = analogRead(potPIN);
potValue = (potValue/3 + 10);
digitalWrite(led[j,k], HIGH);
delay(potValue);
digitalWrite(led[j,k], LOW);
delay(potValue);
digitalWrite(led[j,k], HIGH);
delay(potValue);
digitalWrite(led[j,k], LOW);
//win = 0; //when this is zero, and you press the button, you lose
//delay(potValuen);
if(led[j,k] == HIGH)
{
win == 1;
}
}
else { //all other lights than the target light
digitalWrite(led[j,k], HIGH);
delay(3
potValue);
digitalWrite(led[j,k], LOW);
delay(potValue*n);
}
}
}

void playTone(int tone, int duration)
{
win = led[j,k];
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH && win == 1)
{
for (long i = 0; i < duration * 1000L; i += tone * 2)
{
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
} else
{
//digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}

void playNote(char note, int duration)
{
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++)
{
if (names == note)

  • {*
    _ playTone(tones*, duration);_
    _
    }_
    _
    }_
    _
    }_
    void setup()
    _
    {_
    _
    pinMode(buttonPin, INPUT);//set push button as input*_
    * for (int i = 0; i < 5; i++)//condition loop*
    * {*
    _ pinMode (led*, OUTPUT);//set led light as output*
    * }
    pinMode(speakerPin, OUTPUT);//set speaker at output*
    }
    void loop()
    {
    * randLight();*_

* for (int i = 0; i < length; i++)*
* {*
_ if ( notes == ' ')
* {_
delay(beats _ tempo); // rest_

* } else {*
playNote(notes, beats * tempo);
* }*

* //pause between notes*
* delay(tempo / 2);*
* }*

}

If you look at other posts you will code in its own little window. KenF has told you how to do this.

At the front of your code put . At he end of your code put . This puts your code into a scrollable window and makes it a lot easier for others.

Weedpharma

You still didn't include

[code]and [/code]

around your code.

If you did it would look like this

void setup()
{
//look I can even include that pesky [i] without issue
 for (int i = 0; i < 8; i++)
  {
    if (names[i] == note)
    {
      playTone(tones[i], duration);
    }
  }
}

Since you haven't included the code tags, we can only guess where the
** ** [i]** **
should actually be.

Ken, why did your code/code in the text not do what mine did???

Weedpharma

weedpharma:
Ken, why did your code/code in the text not do what mine did???

Weedpharma

You have to use [nobbc] and [/nobbc] around the

[code] and [/code]

A little trick I picked up from Mr. Gammon :wink: