728x90
반응형
#include <Servo.h>
const int SERVO = 10;
Servo servo;
const int buttonPin = 2;
int servo_state = 30;
bool servo_state_changed = false;
void buttonPressed(){
servo_state = (servo_state == 30)?150:30;
servo_state_changed = true;
}
void setup(){
pinMode(buttonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonPressed, RISING);
servo.attach(SERVO);
servo.write(0);
delay(1000);
}
void loop(){
if(servo_state_changed){
servo_state_changed = false;
servo.write(servo_state);
}
}
// 버튼 누를 때마다 서보의 각도가 90도씩 번갈아 움직임
const int ledPin = 13;
const int buttonPin = 2;
int led_state = LOW;
bool led_state_changed = false;
void buttonPressed(){
led_state = (led_state == LOW) ? HIGH : LOW;
led_state_changed = true;
}
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonPressed, RISING);
}
void loop(){
if (led_state_changed){
led_state_changed = false;
digitalWrite(ledPin, led_state);
}
}
// 버튼을 누르면 켜진 상태가 지속, 다시 누르면 꺼진 상태가 지속
rising edge triggered
falling edge triggered
level triggered (HIGH, LOW)
edge triggered(RISING, FALLING)
const int ACTIVE_BUZZER = 10;
const int buttonPin = 2;
int buzzer_state = LOW;
bool buzzer_state_changed = false;
void buttonPressed(){
buzzer_state = (buzzer_state == LOW)?HIGH : LOW;
buzzer_state_changed = true;
}
void setup(){
pinMode(ACTIVE_BUZZER, OUTPUT);
pinMode(buttonPin, INPUT);
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonPressed, RISING);
}
void loop(){
if (buzzer_state_changed){
buzzer_state_changed = false;
digitalWrite(ACTIVE_BUZZER, buzzer_state);
}
}
728x90
반응형
'Firmware & Embedded > AVR' 카테고리의 다른 글
Serial Communication (0) | 2022.11.03 |
---|---|
iteration arduino (0) | 2022.10.11 |
PWM - Timer Library (0) | 2022.09.22 |
Analogue Read (0) | 2022.09.21 |
Basic Arduino (serial.read) (0) | 2022.09.21 |