In: Electrical Engineering
Using Arduino:
Emulate dots and dashes telegraph transmission using a button and two LEDs. For instance, if a button is held longer than 500ms, then the red LED will turn on to represent a dash "-". and a short press will turn on the green LED to represent a dot ".". The problem can be approached as a state machine where the states could include "wait_on_press", "delay_500ms", and "read_btn_val".
First we define pins where the red LED, green LED and buttons are present. This is done in the void setup part of the code. Following this we write the actual code in the void loop. In order to use least external components the internal pull up resistor is used on the button pin. This means that whenever the button is pressed the pin will transition from HIGH to LOW. The code is given below. For easy readability the snapshot of the code in Arduino IDE is also attached.
#define red_led_pin 12
#define green_led_pin 11
#define button_pin 10
void setup() {
// put your setup code here, to run once:
pinMode(red_led_pin,OUTPUT);
pinMode(green_led_pin,OUTPUT);
pinMode(button_pin,INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(button_pin) == LOW) // If button is pressed
{
delay(500); // wait for 0.5 second
if(digitalRead(button_pin) == LOW) // Check if its still pressed
digitalWrite(red_led_pin,HIGH); // If yes then turn on red LED
else
digitalWrite(green_led_pin,HIGH);// Else turn on green LED
delay(250); // Wait for 0.25 seconds so that
// LEDs donot turn off quickly
digitalWrite(red_led_pin,LOW); // Turn off the red LED
digitalWrite(green_led_pin,LOW); // Turn of the green LED
}
}
Hope it helps. Fo any questions or doubt drop in a comment and I'll definitely get back to you. All the best.