Components and supplies
Arduino UNO
Resistor 1k ohm
Male/Male Jumper Wires
5 mm LED: Red
Solderless Breadboard Half Size
Micro - Push Button
5 mm LED: Green
NPN Transistor (S8050)
5 mm LED: Yellow
Passive Buzzer
USB Cable for Arduino UNO
Tools and machines
Mastech MS8217 Autorange Digital Multimeter
Apps and platforms
Arduino IDE
Project description
Code
LED_combination_experiment.ino
arduino
1/*********************************************************** 2File name:LED_combination_experiment.ino 3Description: With three buttons, three lamps and one buzzer, 4press the different buttons to make the corresponding lamp light 5up, along with the buzzer sounds at different frequencies. 6Made by: ARSH RAJ 7Date: 4/08/2022 8***********************************************************/ 9#define LED1 3 //definition led_1 I/O number is 3 10#define KEY1 2 //definition key_1 I/O number is 2 11 12#define LED2 5 //definition led_2 I/O number is 5 13#define KEY2 4 //definition key_2 I/O number is 4 14 15#define LED3 10 16#define KEY3 9 17 18#define beepPin 8 19 20int KEY_NUM1 = 0; //key_1 value (The key value stores the variable, not equal to 1 indicates that the key is pressed) 21int KEY_NUM2 = 0; //key_2 value 22int KEY_NUM3 = 0; 23 24void setup() 25{ 26 pinMode(LED1,OUTPUT); //definition led_1 I/O is OUTPUT 27 pinMode(KEY1,INPUT_PULLUP); //definition led_1 I/O is INPUT_PULLUP 28 pinMode(LED2,OUTPUT); 29 pinMode(KEY2,INPUT_PULLUP); 30 pinMode(LED3,OUTPUT); 31 pinMode(KEY3,INPUT_PULLUP); 32 33 Serial.begin(9600); 34} 35 36void loop() 37{ 38 ScanKey1();//Key scan program, when the button is pressed, the subroutine will modify the value of KEY_NUM 39 ScanKey2(); 40 ScanKey3(); 41} 42 43//Button 1 Scanner 44void ScanKey1() 45{ 46 KEY_NUM1 = 0; //Clear variable 47 if(digitalRead(KEY1) == LOW) //There is a key press 48 { 49 beep1(); 50 delay(20); //Delayed debounce 51 if(digitalRead(KEY1) == LOW) //There is a key press 52 { 53 KEY_NUM1 = 1; //Variable set to 1 54 while(digitalRead(KEY1) == LOW); //Wait for the keys to release hands 55 } 56 Serial.println(digitalRead(LED1));//Serial output current LED status 57 } 58 59 if(KEY_NUM1 == 1) //Is the button pressed 60 { 61 digitalWrite(LED1,!digitalRead(LED1)); //LED status flip 62 } 63 64} 65 66//Button 2 scanner 67void ScanKey2() 68{ 69 KEY_NUM2 = 0; 70 if(digitalRead(KEY2) == LOW) 71 { 72 beep2(); 73 delay(20); 74 if(digitalRead(KEY2) == LOW) 75 { 76 KEY_NUM2 = 1; 77 while(digitalRead(KEY2) == LOW); 78 } 79 Serial.println(digitalRead(LED2)); 80 } 81 82 if(KEY_NUM2 == 1) 83 { 84 digitalWrite(LED2, !digitalRead(LED2)); 85 } 86} 87 88//Button 3 scanner 89void ScanKey3() 90{ 91 KEY_NUM3 = 0; 92 if(digitalRead(KEY3) == LOW) 93 { 94 beep3(); 95 delay(20); 96 if(digitalRead(KEY3) == LOW) 97 { 98 KEY_NUM3 = 1; 99 while(digitalRead(KEY3) == LOW); 100 } 101 Serial.println(digitalRead(LED3)); 102 } 103 104 if(KEY_NUM3 == 1) 105 { 106 digitalWrite(LED3, !digitalRead(LED3)); 107 } 108} 109 110void beep1()//Set buzzer frequency 111{ 112 tone (beepPin,300); 113 delay(200); 114 noTone(beepPin); 115 } 116 void beep2() 117{ 118 tone (beepPin,900); 119 delay(200); 120 noTone(beepPin); 121 } 122 void beep3() 123{ 124 tone (beepPin,1400); 125 delay(200); 126 noTone(beepPin); 127 } 128
LED_combination_experiment.ino
arduino
1/*********************************************************** 2File name:LED_combination_experiment.ino 3Description: 4 With three buttons, three lamps and one buzzer, 5press the different buttons 6 to make the corresponding lamp light 7up, along with the buzzer sounds at different 8 frequencies. 9Made by: ARSH RAJ 10Date: 4/08/2022 11***********************************************************/ 12#define 13 LED1 3 //definition led_1 I/O number is 3 14#define KEY1 2 //definition 15 key_1 I/O number is 2 16 17#define LED2 5 //definition led_2 I/O number is 18 5 19#define KEY2 4 //definition key_2 I/O number is 4 20 21#define LED3 22 10 23#define KEY3 9 24 25#define beepPin 8 26 27int KEY_NUM1 = 0; //key_1 28 value (The key value stores the variable, not equal to 1 indicates that the key 29 is pressed) 30int KEY_NUM2 = 0; //key_2 value 31int KEY_NUM3 = 0; 32 33void 34 setup() 35{ 36 pinMode(LED1,OUTPUT); //definition led_1 I/O is OUTPUT 37 38 pinMode(KEY1,INPUT_PULLUP); //definition led_1 I/O is INPUT_PULLUP 39 40 pinMode(LED2,OUTPUT); 41 pinMode(KEY2,INPUT_PULLUP); 42 pinMode(LED3,OUTPUT); 43 44 pinMode(KEY3,INPUT_PULLUP); 45 46 Serial.begin(9600); 47} 48 49void 50 loop() 51{ 52 ScanKey1();//Key scan program, when the button is pressed, the 53 subroutine will modify the value of KEY_NUM 54 ScanKey2(); 55 ScanKey3(); 56} 57 58//Button 59 1 Scanner 60void ScanKey1() 61{ 62 KEY_NUM1 = 0; //Clear 63 variable 64 if(digitalRead(KEY1) == LOW) //There is a key press 65 { 66 67 beep1(); 68 delay(20); //Delayed debounce 69 if(digitalRead(KEY1) 70 == LOW) //There is a key press 71 { 72 KEY_NUM1 = 1; //Variable 73 set to 1 74 while(digitalRead(KEY1) == LOW); //Wait for the keys to release 75 hands 76 } 77 Serial.println(digitalRead(LED1));//Serial output current 78 LED status 79 } 80 81 if(KEY_NUM1 == 1) //Is the button pressed 82 83 { 84 digitalWrite(LED1,!digitalRead(LED1)); //LED status flip 85 } 86 87 88} 89 90//Button 2 scanner 91void ScanKey2() 92{ 93 KEY_NUM2 94 = 0; 95 if(digitalRead(KEY2) == LOW) 96 { 97 beep2(); 98 99 delay(20); 100 if(digitalRead(KEY2) == LOW) 101 { 102 KEY_NUM2 103 = 1; 104 while(digitalRead(KEY2) == LOW); 105 } 106 Serial.println(digitalRead(LED2)); 107 108 } 109 110 if(KEY_NUM2 == 1) 111 { 112 digitalWrite(LED2, !digitalRead(LED2)); 113 114 } 115} 116 117//Button 3 scanner 118void ScanKey3() 119{ 120 KEY_NUM3 121 = 0; 122 if(digitalRead(KEY3) == LOW) 123 { 124 beep3(); 125 126 delay(20); 127 if(digitalRead(KEY3) == LOW) 128 { 129 KEY_NUM3 130 = 1; 131 while(digitalRead(KEY3) == LOW); 132 } 133 Serial.println(digitalRead(LED3)); 134 135 } 136 137 if(KEY_NUM3 == 1) 138 { 139 digitalWrite(LED3, !digitalRead(LED3)); 140 141 } 142} 143 144void beep1()//Set buzzer frequency 145{ 146 tone (beepPin,300); 147 148 delay(200); 149 noTone(beepPin); 150 } 151 void beep2() 152{ 153 tone (beepPin,900); 154 155 delay(200); 156 noTone(beepPin); 157 } 158 void beep3() 159{ 160 tone (beepPin,1400); 161 162 delay(200); 163 noTone(beepPin); 164 } 165
Downloadable files
Circuit diagram :-
Circuit diagram :-
Comments
Only logged in users can leave comments