๋ฌธ์ 2-1
- LED, Buzzer ์ฌ์ฉ
- 2์ด ๊ฐ๊ฒฉ์ผ๋ก ๊ต๋๋ก ์ ๋ฉธ
- ์ค์์น๊ฐ ๋๋ฆด ๊ฒฝ์ฐ Buzzer ๋์
- delay() ๋ฅผ ์ฌ์ฉํด์ ์ฝ๋ ์์ฑ
๋ฌธ์ ์ดํด
- ๋ฌธ์ ๊ทธ๋๋ก ์ฝ๋๋ฅผ ์์ฑ
์ฝ๋
int switch_state;
int int_sw = 2;
int LED1 = 22;
int LED2 = 24;
int BUZZER = 8;
int LED1_state = HIGH;
int LED2_state = LOW;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(BUZZER, OUTPUT);
}
void loop() {
flush();
switch_state = digitalRead(2);
if (switch_state == HIGH) {
digitalWrite(BUZZER, HIGH);
} else {
digitalWrite(BUZZER, LOW);
}
}
void flush() {
if (LED1_state == HIGH) {
digitalWrite(LED1, LED1_state);
delay(2000);
} else if (LED1_state == LOW) {
digitalWrite(LED1, LED1_state);
delay(2000);
}
if (LED2_state == HIGH) {
digitalWrite(LED2, LED2_state);
delay(2000);
} else if (LED2_state == LOW) {
digitalWrite(LED2, LED2_state);
delay(2000);
}
LED1_state = !LED1_state;
LED2_state = !LED2_state;
}
๋ฌธ์ 2-2, 2-3
- 2-2
- MsTimer2 ์ธํฐ๋ฝํธ ์ด์ฉ
- 2-3
- TimerOne ์ธํฐ๋ฝํธ ์ด์ฉ
๋ฌธ์ ์ดํด
- ๊ฐ ํ์ด๋จธ ์ธํฐ๋ฝํธ๋ฅผ ์ด์ฉํ์
์ฝ๋
#include <TimerOne.h>
#include <MsTimer2.h>
int switch_state;
int int_sw = 2;
int LED1 = 22;
int LED2 = 24;
int BUZZER = 8;
int LED1_state = HIGH;
int LED2_state = LOW;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(BUZZER, OUTPUT);
MsTimer2::set(2000, flush);
MsTimer2::start();
}
void loop() {
switch_state = digitalRead(2);
if (switch_state == HIGH) {
digitalWrite(BUZZER, HIGH);
} else {
digitalWrite(BUZZER, LOW);
}
}
void flush() {
if (LED1_state == HIGH) {
digitalWrite(LED1, LED1_state);
} else if (LED1_state == LOW) {
digitalWrite(LED1, LED1_state);
}
if (LED2_state == HIGH) {
digitalWrite(LED2, LED2_state);
} else if (LED2_state == LOW) {
digitalWrite(LED2, LED2_state);
}
LED1_state = !LED1_state;
LED2_state = !LED2_state;
}
#include <TimerOne.h>
#include <MsTimer2.h>
#include <LiquidCrystal.h>
// LiquidCrystal lcd(42, 43, 44, 45, 46, 47);
int switch_state;
int int_sw = 2;
int LED1 = 22;
int LED2 = 24;
int BUZZER = 8;
int LED1_state = HIGH;
int LED2_state = LOW;
void setup() {
// put your setup code here, to run once:
// lcd.begin(16, 2);
// lcd.print("hello!");
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(BUZZER, OUTPUT);
Timer1.initialize(2000000); // 2์ด
Timer1.attachInterrupt(flush);
}
void loop() {
// put your main code here, to run repeatedly:
// lcd.setCursor(0, 1);
// lcd.print(millis() / 1000);
switch_state = digitalRead(2);
if (switch_state == HIGH) {
digitalWrite(BUZZER, HIGH);
} else {
digitalWrite(BUZZER, LOW);
}
}
void flush() {
if (LED1_state == HIGH) {
digitalWrite(LED1, LED1_state);
} else if (LED1_state == LOW) {
digitalWrite(LED1, LED1_state);
}
if (LED2_state == HIGH) {
digitalWrite(LED2, LED2_state);
} else if (LED2_state == LOW) {
digitalWrite(LED2, LED2_state);
}
LED1_state = !LED1_state;
LED2_state = !LED2_state;
}
๋ฌธ์ 3
- ๋๋ฐ์ด์ค ํ๋ก ์ค๊ณ
- Falling 1๋ฒ :
- ์ด์ํ ์ผ์ LCD์ ๊ฑฐ๋ฆฌ ๋์คํ๋ ์ด
- ํ์ด๋จธ ์ธํฐ๋ฝํธ ์ฌ์ฉ 2ํ์ ๋์คํ๋ ์ด, 1์ด๋ง๋ค LED ์ ๋ฉธ
- Falling 3๋ฒ :
- ์ค์๊ฐ LCD์ ๊ฑฐ๋ฆฌ ๋์คํ๋ ์ด
- 20์ผํฐ๋ฏธํฐ ๋ฏธ๋ง LED ๊ณ์ HIGH
- 20์ผํฐ๋ฏธํฐ ์ด๊ณผ ํ์ด๋จธ ์ธํฐ๋ฝํธ ์ฌ์ฉ 1์ด๋ง๋ค ์ ๋ฉธ
- Rising 5๋ฒ :
- ์ด์ํ ์ผ์ LCD์ ๊ฑฐ๋ฆฌ ๋์คํ๋ ์ด
- 20์ผํฐ๋ฏธํฐ ๋ฏธ๋ง ํ์ด๋จธ ์ธํฐ๋ฝํธ ์ฌ์ฉ 1์ด๋ง๋ค LED ์ ๋ฉธ
- ๊ทธ ์ด์ธ LED HIGH, LCD 2ํ์ ๊ฒฝ๊ณ ๋ฌธ์ ์ถ๋ ฅ
๋ฌธ์ ์ดํด
- ํ๋์ ํ์ผ๋ก ํด๋ง๊ณผ ๋ผ์ด์ง ์ฃ์ง๋ฅผ ๋์์ ๊ฒ์ถํ ์ ์๋ค
- ํด๋ง์ด ๋๋๋ฉด ์ธํฐ๋ฝํธ๋ฅผ ์ด๊ธฐํ ํ๋ ์ ๋ฐ์.
- ํ์ด๋จธ ์ธํฐ๋ฝํธ๋ ๋ง์ง๋ง์ ์ธํ
๋ ํจ์๋ง ์คํํ๋ค.
- stop()์ ํธ์ถํ ๊ฒ.
์ฝ๋
#include <TimerOne.h>
#include <MsTimer2.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(42, 43, 44, 45, 46, 47);
int switch_state;
int LED = 22;
int TRIGGER = 12;
int ECHO = 13;
int LED_state = HIGH;
int LCD_state = HIGH;
const byte interruptPin = 2;
volatile int fallingCount = 0;
volatile int risingCount = 0;
volatile long cm; // ๊ฑฐ๋ฆฌ๋ฅผ ์ ์ญ์ ์ผ๋ก ์ฌ์ฉํ๊ธฐ
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(TRIGGER, OUTPUT);
noInterrupts();
interrupts();
attachInterrupt(digitalPinToInterrupt(interruptPin), fallingAct, FALLING); // Falling ๋ถํฐ ์งํ
lcd.begin(16, 2);
}
void loop() { // ํญ์ ์ด์ํ ๊ฑฐ๋ฆฌ๋ lcd์ ๋์คํ๋ ์ด
// put your main code here, to run repeatedly:
cm = getDistanceOfCm();
lcd.setCursor(0, 0);
lcd.print(cm);
}
// ์ฒซ๋ฒ์งธ Falling edge ์ ๋์ ํจ์
void countOneAct() {
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
flushLed();
}
// lcd ์ ๋ฉธ
void countFiveAct() {
if (LCD_state == HIGH) {
lcd.setCursor(0, 1);
lcd.print("Warning!!");
} else if (LCD_state == LOW) {
lcd.clear();
}
LCD_state = !LCD_state;
}
// led ์ ๋ฉธ
void flushLed() {
digitalWrite(LED, LED_state);
LED_state = !LED_state;
}
// ์ด์ํ์ผ์ ์๋ ๋ฐ cm ๋ณํ ํจ์
long getDistanceOfCm() {
digitalWrite(TRIGGER, LOW); // ์ถ๋ ฅ ๊นจ๋ํ!
delayMicroseconds(2);
digitalWrite(TRIGGER, HIGH); // ์ถ๋ ฅ ์์
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW); // ์ถ๋ ฅ ์ข
๋ฃ
long duration = pulseIn(ECHO, HIGH); // ์์ฝ๊ฐ ํธ๋ฆฌ๊ฑฐ๊ฐ HIGH ์์ ์๊ฐ์ ์ ์ฅ
return (duration / 2) / 29.1; // cm๋ก ๋ณํํ์ฌ ๋ฐํ
}
//Falling edge Interrupt Service Routine
void fallingAct() {
fallingCount++; // Falling count 1 ์ฆ๊ฐ
if (fallingCount == 1) {
MsTimer2::set(1000, countOneAct);
MsTimer2::start();
}
if (fallingCount == 3) {
lcd.clear();
if (cm < 20) {
MsTimer2::stop();
digitalWrite(LED, HIGH);
} else {
MsTimer2::set(1000, flushLed);
MsTimer2::start();
}
}
if (fallingCount > 3) {
MsTimer2::stop();
noInterrupts(); // Rising Edge๋ฅผ ๊ฒ์ถํด์ผ ํ๋ฏ๋ก ์ธํฐ๋ฝํธ ์ข
๋ฃ
interrupts();
attachInterrupt(digitalPinToInterrupt(interruptPin), risingAct, RISING);
}
}
void risingAct() {
risingCount++;
if (risingCount == 5) {
lcd.clear();
if (cm < 20) {
MsTimer2::stop();
MsTimer2::set(1000, flushLed);
MsTimer2::start();
} else {
MsTimer2::stop();
digitalWrite(LED, HIGH);
MsTimer2::set(2000, countFiveAct);
MsTimer2::start();
}
}
}
๋ฐ์ํ
'๐ป Microprocessor > ์บก์คํค ์ฑ๋ฆฐ์ง' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[6์ฃผ์ฐจ] (Led Matrix) Capston Challenge (0) | 2022.10.13 |
---|---|
[2์ฃผ์ฐจ] (External Interrupt) Caston Challenge 4 (2) | 2022.10.05 |
[2์ฃผ์ฐจ] Capston challenge 1 (0) | 2022.09.12 |