/***********************************************************
File name:LED_combination_experiment.ino
Description: With three buttons, three lamps and one buzzer,
press the different buttons to make the corresponding lamp light
up, along with the buzzer sounds at different frequencies.
Made by: ARSH RAJ
Date: 4/08/2022
***********************************************************/
#define LED1 3 //definition led_1 I/O number is 3
#define KEY1 2 //definition key_1 I/O number is 2
#define LED2 5 //definition led_2 I/O number is 5
#define KEY2 4 //definition key_2 I/O number is 4
#define LED3 10
#define KEY3 9
#define beepPin 8
int KEY_NUM1 = 0; //key_1 value (The key value stores the variable, not equal to 1 indicates that the key is pressed)
int KEY_NUM2 = 0; //key_2 value
int KEY_NUM3 = 0;
void setup()
{
pinMode(LED1,OUTPUT); //definition led_1 I/O is OUTPUT
pinMode(KEY1,INPUT_PULLUP); //definition led_1 I/O is INPUT_PULLUP
pinMode(LED2,OUTPUT);
pinMode(KEY2,INPUT_PULLUP);
pinMode(LED3,OUTPUT);
pinMode(KEY3,INPUT_PULLUP);
Serial.begin(9600);
}
void loop()
{
ScanKey1();//Key scan program, when the button is pressed, the subroutine will modify the value of KEY_NUM
ScanKey2();
ScanKey3();
}
//Button 1 Scanner
void ScanKey1()
{
KEY_NUM1 = 0; //Clear variable
if(digitalRead(KEY1) == LOW) //There is a key press
{
beep1();
delay(20); //Delayed debounce
if(digitalRead(KEY1) == LOW) //There is a key press
{
KEY_NUM1 = 1; //Variable set to 1
while(digitalRead(KEY1) == LOW); //Wait for the keys to release hands
}
Serial.println(digitalRead(LED1));//Serial output current LED status
}
if(KEY_NUM1 == 1) //Is the button pressed
{
digitalWrite(LED1,!digitalRead(LED1)); //LED status flip
}
}
//Button 2 scanner
void ScanKey2()
{
KEY_NUM2 = 0;
if(digitalRead(KEY2) == LOW)
{
beep2();
delay(20);
if(digitalRead(KEY2) == LOW)
{
KEY_NUM2 = 1;
while(digitalRead(KEY2) == LOW);
}
Serial.println(digitalRead(LED2));
}
if(KEY_NUM2 == 1)
{
digitalWrite(LED2, !digitalRead(LED2));
}
}
//Button 3 scanner
void ScanKey3()
{
KEY_NUM3 = 0;
if(digitalRead(KEY3) == LOW)
{
beep3();
delay(20);
if(digitalRead(KEY3) == LOW)
{
KEY_NUM3 = 1;
while(digitalRead(KEY3) == LOW);
}
Serial.println(digitalRead(LED3));
}
if(KEY_NUM3 == 1)
{
digitalWrite(LED3, !digitalRead(LED3));
}
}
void beep1()//Set buzzer frequency
{
tone (beepPin,300);
delay(200);
noTone(beepPin);
}
void beep2()
{
tone (beepPin,900);
delay(200);
noTone(beepPin);
}
void beep3()
{
tone (beepPin,1400);
delay(200);
noTone(beepPin);
}
Comments