LED blinking with BEEP

Hello, I have a noobie question. I need to make one blinking LED with beep sound working together ( train traffic sign ) LED blinks two times with two long beeps, then it blinks again two times with two short beeps. It should work for 10 seconds, then pause for 5sec and continue work again. I just started to work with arduino, I made blinking LED but I cant figure out how to make it work with BEEP sound together. Could anyone help me?

int led = 8;

void setup() {
pinMode(led, OUTPUT);

}

void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);

}

The first thing that you'll want to do is pay attention to the blink without delay example. You can not make a beeping noise and blink an LED while using delay().

The second thing to do is define what, exactly, a beeping noise is. The usual way to make sounds with the Arduino is to use the tone() function, but that plays single tones, and your concept of a beep may not be a single tone.

PaulS:
The second thing to do is define what, exactly, a beeping noise is.

And what exactly will be making the noise? Foe example, a speaker is driven differently than an AC piezo transducer, which is driven differently from a DC piezo buzzer, which is driven differently from a sound synthesizer chip.

PaulS:
The first thing that you'll want to do is pay attention to the blink without delay example. You can not make a beeping noise and blink an LED while using delay().

That is true. Also the blink without delay approach is something you should know. But in this case, you can make a LED blink with a tone, if I understand correctly. Because the LED and tone are simultaneous, you can turn the LED on before starting a tone, and turn it off when the tone is complete. That is a single task.

What you can not do, is have the LED blink in any unrelated way, while a tone is playing. That is a multiple task.

I'm thinking in terms of the functionality of the tone() command here.

Mini speaker will do the noise which is connected with one LED. I got this task in school. I made this:

int led = 8;
int peizoPin = 8;

void setup() {
pinMode(led, OUTPUT);

}

void loop() {
digitalWrite(led, HIGH);
tone(peizoPin, 50, 500);
delay(1500);
digitalWrite(led, LOW);
delay(1000);

}

Will LED blink and speaker beep for one second on the same time?

The code will try to blink the LED slower than one second: on for 1.5 seconds, off for 1, with a total period of 2.5 seconds. The piezo will try to make sound the entire time.

I say try, because the code you have now won't work. First off, you set the pinMode of "Raudonas" to OUTPUT, but "Raudonas" is not defined anywhere. This won't compile. Then, you define led and peizoPin to the same pin number, but you never set that pin as an output. Finally, in the code, you set the led output high which should turn it on (assuming the other end of the LED is grounded.) But then you use the tone command on the same pin number: this will make the peizo start to buzz, but will also cause the LED to flash rapidly, which will cause it to be dim. Then, you try to turn this off with a digitalWrite call, noTone() would be more appropriate if you want the buzzer to stop.

I'm thinking you want to have two different pins: one for the LED which you use digitalWrite() to control, and one for the peizo which uses tone() and noTone().

ShapeShifter:
The code will try to blink the LED slower than one second: on for 1.5 seconds, off for 1, with a total period of 2.5 seconds. The piezo will try to make sound the entire time.

I say try, because the code you have now won't work. First off, you set the pinMode of "Raudonas" to OUTPUT, but "Raudonas" is not defined anywhere. This won't compile. Then, you define led and peizoPin to the same pin number, but you never set that pin as an output. Finally, in the code, you set the led output high which should turn it on (assuming the other end of the LED is grounded.) But then you use the tone command on the same pin number: this will make the peizo start to buzz, but will also cause the LED to flash rapidly, which will cause it to be dim. Then, you try to turn this off with a digitalWrite call, noTone() would be more appropriate if you want the buzzer to stop.

I'm thinking you want to have two different pins: one for the LED which you use digitalWrite() to control, and one for the peizo which uses tone() and noTone().

Thanks for your reply. Yes, I corrected it earlier it should be "led", not "Raudonas". For those pins I have a task that I need to write code that will make mini speaker connected with LED blink and beep at one time and I have to use pin 8. Two short and two long beeps with LED

I gues there will be led with speaker in one, is it possible?

Your requirement is that the LED and the speaker must be connected to a single pin? In that case, the code can consider them a single combined device, regardless if they are packaged together or discrete devices. The code just needs to control the single output to make light and noise at the same time, or no light and silence. Don't think of them as two separate devices. Think in terms of the output pin, not what is connected to it (in reality, you could have dozens of devices connected to a single pin, as long as they are all controlled at the same time - think of a string of Christmas lights connected to a switch: the whole string is either on or off, you don't think in terms of individual bulbs.)

How you control that one output pin depends on the device that is making the noise: if it's a speaker that uses the tone() function, then the LED will not reach full brightness because it is turning on and off at the same frequency as the speaker. In this case, you only need tone() and noTone() to control both. With this setup, there is no way to get full brightness from the LED, and sound, at the same time.

If the noisemaker is a buzzer that just takes constant DC voltage, then you use digitalWrite() to turn the output on and off, and the LED will be at full brightness.

I think there will be a buzzer.

int led = 8;

void setup() {
pinMode(led, OUTPUT);

}

void loop() {
// two longs beeps and two led blinks
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
// two short beeps and two led blinks
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);

}

So now LED and buzzer will blink and beep two long and two short times? Further I need to make that this code would work for 10seconds, then pause it for 5seconds and start code again. Says I need to use cycles, what about those cycles?

Yes, I think you've got the idea!

I'm not sure what you mean by "cycles." Do you mean loops? http://www.cplusplus.com/doc/tutorial/control/

I'm guessing that rather than a long list of setting the output, delay, set the output, delay, etc, he wants you to use some sort of loop control statement to repeat a short sequence of statements multiple times?

Yea, you're right.

void loop() {

for (int i=1; i<3; i++){
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay (1000);
}

for (int i=1; i<3; i++){
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay (500);
}

for (int i=1; i<3; i++){
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay (1000);
}

for (int i=1; i<3; i++){
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay (500);
}
for (int i=1; i<3; i++){
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay (1000);
}

delay (5000);
}

I got it right? Led should blink for 10seconds with different time interval ( 2 times with one second interval, 2 times with 0.5 second interval ), then pause for 5seconds and will repeat it again

Add up your times, I think you're trying to get too much done in 10 seconds.

How much time does this take?

   digitalWrite(led, HIGH);   
    delay(1000);             
    digitalWrite(led, LOW);     
    delay (1000);

Now, put it in a loop, how much total time does this take?

 for (int i=1; i<3; i++){
    digitalWrite(led, HIGH);   
    delay(1000);             
    digitalWrite(led, LOW);     
    delay (1000);           
  }

Add up the times for all of the iterations of all of you loops. How long does it take?

Step through the code, what is the resulting pattern?