728x90
반응형
Pull - Up
const int Btn = 7;
void setup()
{
Serial.begin(9600);
pinMode(Btn, INPUT);
}
void loop()
{
int value = digitalRead(Btn);
Serial.println(value);
/*
Button Pull -> 1 (ON)
Button Push -> 0 (OFF)
*/
}
Pull - Down
const int Btn = 7;
void setup()
{
Serial.begin(9600);
pinMode(Btn, INPUT);
}
void loop()
{
int value = digitalRead(Btn);
Serial.println(value);
/*
Button Pull -> 0 (OFF)
Button Push -> 1 (ON)
*/
}
<Pull-down>
-down 방식으로 스위치를 1개 구성하고, LED를 1개 구성하여서 스위치가 눌려져 있을 때 LED 불이 켜지고,
스위치가 열려 있을 때 LED 불이 꺼지도록 회로 및 프로그래밍을 작성하여라
이때, 스위치와 LED의 신호는 독립적이다.
1. 표 구성
2. 회로 구성
3. 코드 작성
const int LED = 3;
const int Btn = 7;
int Btn_state = 0;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(Btn, INPUT);
}
void loop()
{
int value = digitalRead(Btn);
if (value == HIGH)
{
if (Btn_state = 0)
{
Btn_state = 1;
}
}
else if (Btn_state == 1)
{
Btn_state = 0;
}
if (Btn_state == 0)
{
digitalWrite(LED, LOW);
}
else if (Btn_state == 1)
{
digitalWrite(LED, HIGH);
}
}
const int P_LED = 3;
const int B_LED[5] = {8, 9, 10, 11, 12};
const int Btn = 7;
int Btn_state = 0;
void setup()
{
Serial.begin(9600);
pinMode(P_LED, OUTPUT);
for (int i = 0; i < 5; i++)
{
pinMode(B_LED[i], OUTPUT);
}
pinMode(Btn, INPUT);
}
void loop()
{
/* 가변저항 */
int val = analogRead(A0) / 4;
analogWrite(P_LED, val);
int value = digitalRead(Btn);
Btn_state += value;
delay(100);
Serial.println(val);
/* LED 버튼 */
for (int i = 0; i < 5; i++)
{
digitalWrite(B_LED[i], LOW);
}
digitalWrite(B_LED[Btn_state], HIGH);
if(Btn_state == 4)
{
Btn_state = 0;
}
}
const int P_LED = 3;
const int B_LED[5] = {8, 9, 10, 11, 12};
const int Btn = 7;
int Btn_state = 0;
void setup()
{
Serial.begin(9600);
pinMode(P_LED, OUTPUT);
for (int i = 0; i < 5; i++)
{
pinMode(B_LED[i], OUTPUT);
}
pinMode(Btn, INPUT);
}
void loop()
{
/* 가변저항 */
int val = analogRead(A0) / 4;
analogWrite(P_LED, val);
int value = digitalRead(Btn);
Btn_state += value;
delay(100);
Serial.println(val);
for (int i = 0; i < 5; i++)
{
digitalWrite(B_LED[i], LOW);
}
/* LED 버튼 */
digitalWrite(B_LED[Btn_state], HIGH);
if(Btn_state == 4)
{
Btn_state = 0;
}
/*
switch(Btn_state)
{
case 0:
digitalWrite(B_LED[Btn_state], HIGH);
break;
case 1:
digitalWrite(B_LED[Btn_state], HIGH);
break;
case 2:
digitalWrite(B_LED[Btn_state], HIGH);
break;
case 3:
digitalWrite(B_LED[Btn_state], HIGH);
break;
case 4:
digitalWrite(B_LED[Btn_state], HIGH);
break;
}
if (Btn_state == 4)
{
Btn_state = 0;
}
*/
}
728x90
반응형
'Firmware & Embedded > Components' 카테고리의 다른 글
LED Control (Button, Potentiometer) (0) | 2022.11.22 |
---|---|
Interrupt control (0) | 2022.11.21 |
BlueTooth Module(HC-05, 06) (0) | 2022.11.17 |
DC Motor (0) | 2022.11.16 |
L9110S (Motor Driver Module) (0) | 2022.11.16 |