Create a program so that when pressing the button, the LEDs will turn on intermittently following the speed of the sound made by the buzzer, simulating the alarm of an emergency vehicle.
DIFFICULTY LEVEL: Beginner.
DURATION OF THE EXERCISE: 20 min.
MATERIALS:
- 1 Green LED, 1 Red LED, 1 Button and 1 Buzzer from the Code&Drive kit. The Code&Drive will have to be built according to the instructions manual
- Computer
The Code&Drive will have to be built according to the instructions manual.
What is a buzzer?
A buzzer is an electroacoustic transducer that makes a continuous or intermittent sound, usually treble. It is used as a signaling mechanism o warning and can be applied to multiple systems, such as cars and household appliances, including wake up alarms.
What is a button?
A button is a switch mechanism that when pressed it sends a signal.
CONNECTIONS:
- Connect the light sensors to the analog pins A0 and A1.
- Connect the LEDs to the digital pins 9 and 10.
PROGRAMMING CODE
You can do this project using the Arduino, Bitbloq and other visual programming software by blocks compatible. Below you will find the necessary code.
Arduino Code
- Download and install the Arduino IDE program. It is available for Windows, Mac OS and Linux.
- Open the Arduino program and copy the following program in it:
int PinSpeedMotorA = 5, PinSpeedMotorB = 6; // PIN DIGITAL PARA LA VELOCIDAD DE LOS MOTORES int PinBoton = 11, ValueBoton = 0; // PIN DIGITAL DEL BOTÓN Y VALOR DEL BOTÓN int PinLED1 = 9, PinLED2 = 10; // PIN DIGITAL LED1 Y LED2 int PinBuzzer = 8; // PIN DIGITAL DEL ZUMBADOR void setup() { // put your setup code here, to run once: // CONFIGURACIÓN DE LOS PINES DIGITALES pinMode(PinSpeedMotorA, OUTPUT); pinMode(PinSpeedMotorB, OUTPUT); pinMode(PinBoton, INPUT); pinMode(PinLED1, OUTPUT); pinMode(PinLED2, OUTPUT); pinMode(PinBuzzer, OUTPUT); // VELOCIDAD DE LOS MOTORES A 0 analogWrite(PinSpeedMotorA, 0); analogWrite(PinSpeedMotorB, 0); } void loop() { // put your main code here, to run repeatedly: ValueBoton = digitalRead( PinBoton); // LECTURA DE ESTADO DEL BOTÓN if ( ValueBoton == LOW) // SI EL BOTÓN ESTÁ PULSADO { digitalWrite(PinLED1,HIGH); // LED1 = ON digitalWrite(PinLED2,LOW); // LED2 = OFF tone(PinBuzzer, 600); // TONO DEL ZUMBADOR delay(350); // TIEMPO DE EJECUCIÓN digitalWrite(PinLED1,LOW); // LED1 = OFF digitalWrite(PinLED2,HIGH); // LED2 = ON tone(PinBuzzer, 400); // TONO DEL ZUMBADOR delay(350); // TIEMPO DE ESPERA } else // SI EL BOTÓN NO ESTÁ PULSADO { noTone(PinBuzzer); // ZUMBADOR APAGADO digitalWrite(PinLED1,LOW); // LED1 = OFF digitalWrite(PinLED2,LOW); // LED2 = OFF } }
- Configure and upload the code, following the indications on the Code&Drive First Steps guide.
- Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.
Code for the visual programming software by blocks compatible
- Download and install the program.
- Open the software and copy the following code. Use the following image as a guide:
- Configure and upload the code, following the indications on the Code&Drive First Steps guide.
- Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.
Bitbloq code
- Download Bitbloq and install the Web2board app.
- Open the software and copy the following code:
- Hardware
- Software
- Hardware
- Configure and upload the code, following the indications on the Code&Drive First Steps guide.
- Check that the BTL/USB switch on the Build&Code 4in1 board is set to USB, to upload the code correctly.
RESULT OF THE EXERCISE
When pressing the button, both LEDs will light intermittently. The buzzer will make a sound and change its note every time a LED turns on.
Altogether, when you press the button, the LEDs and the buzzer will simulate the alarm of an emergency vehicle.