BASIC4MCU | 질문게시판 | 답변 : 아두이노 인터럽트
페이지 정보
작성자 hello 작성일2019-11-21 10:33 조회5,367회 댓글0건본문
제가 반복동작을 하는 코딩인줄 알고 계속 질문을 드렷네요 ~ㅠㅠ 유익한 답변 항사 감사드립니다~
처음에 초음파센서에 대한 반응을 반복동작을로 변경하고 싶은데 while문으로 변경해야 할까요~??
지금상황은 rfid카드를 태그하고 딱한번만 동작하고 있습니다~!
#include <SPI.h>
#include <MFRC522.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>const int RST_PIN = 9;
const int SS_PIN = 10;MFRC522 mfrc522(SS_PIN, RST_PIN); // MFRC522 인스턴스
SoftwareSerial BTSerial(0,1); //블루투스 설정 BTSerial(Tx, Rx)
byte cardkeyByte[4] = {0xE9, 0x85, 0x59, 0x98}; //card UID
boolean state = false; //초음파 센서 작동 상태값Servo servo;
const int SERVO_PIN = 8;
const int TONEPIN = 5;int LEDB= 7;
int LEDR =6;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int TRIG_PIN = 3;
const int ECHO_PIN = 2;bool status=0;
void setup() {
Serial.begin(9600);
BTSerial.begin(9600); //블루투스 통신 시작
while (!Serial);
SPI.begin(); //SPI 시작
mfrc522.PCD_Init(); //MFRC522 card 초기화
Serial.println(F("Warning: this example clears your mifare UID, use with care!"));pinMode(TRIG_PIN,OUTPUT); //초음파센서핀(TRIG)
pinMode(ECHO_PIN,INPUT); //초음파센서핀(ECHO)pinMode(LEDR, OUTPUT); // 3번핀을 출력으로 설정
pinMode(LEDB, OUTPUT); // 4번핀을 출력으로 설정servo.attach(SERVO_PIN);
servo.write(0);// I2C LCD를 초기화 합니다..
lcd.init();
// I2C LCD의 백라이트를 켜줍니다.
lcd.backlight();
delay(50);
}void loop() {
noTone(TONEPIN);
if(mfrc522.PICC_IsNewCardPresent()){ // 새 카드 확인if(mfrc522.PICC_ReadCardSerial()){ // 카드 읽기
if(!memcmp(mfrc522.uid.uidByte,cardkeyByte,4)){ // UID 일치
tone(TONEPIN,523,1000/8); delay(1000/4*1.30); noTone(TONEPIN); //카드 확인 메세지
status^=1;
if(status){
digitalWrite(TRIG_PIN,HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN,LOW);
float duration=pulseIn(ECHO_PIN,HIGH);
float distance=duration/57.5;
//
if(distance<10){
digitalWrite(LEDR,LOW); digitalWrite(LEDB,HIGH);
lcd.setCursor(4,0); // 0번째 줄 0번째 셀부터 입력하게 합니다.
lcd.print("Warning!"); // 문구를 출력합니다.
lcd.setCursor(0,1); // 0번째 줄 0번째 셀부터 입력하게 합니다.
lcd.print(" please call 112"); // 문구를 출력합니다.
Serial.print("Close: "); Serial.println(distance);
noTone(TONEPIN); tone(TONEPIN,3700); if(myDelay(325))return;
noTone(TONEPIN); tone(TONEPIN,3900); if(myDelay(325))return;
noTone(TONEPIN); tone(TONEPIN,3700); if(myDelay(325))return;
noTone(TONEPIN); tone(TONEPIN,3900); if(myDelay(325))return;
noTone(TONEPIN); servo.write(0);
}
else{
digitalWrite(LEDB,HIGH); digitalWrite(LEDR,LOW);
lcd.init();
lcd.setCursor(4,0); // 0번째 줄 0번째 셀부터 입력하게 합니다.
lcd.print("Safety"); // 문구를 출력합니다.
lcd.setCursor(0,1); // 0번째 줄 0번째 셀부터 입력하게 합니다.
lcd.print("Have a good time"); // 문구를 출력합니다.Serial.println("Open"); servo.write(180);
}
}
}
delay(500);
}
}
}
//
int myDelay(int d){
for(int i=0;i<d;i++){
if(mfrc522.PICC_IsNewCardPresent())return 1;
delay(1);
}
return 0;
}
댓글 0
조회수 5,367등록된 댓글이 없습니다.