답변 : 거리센서와 네오픽셀 코딩
페이지 정보
작성자 master 작성일18-06-05 17:21 조회10,391회 댓글0건본문
// MCU BASIC: https://www.basic4mcu.com
// DateTime : 2018-06-05 오후 5:19:42
// by Ok-Hyun Park
//
초음파 거리센서 URM37 코딩
//#Editor: Zrh from DFRobot
//#Data: 29.08.2014
//#Product name: ultrasonic scanner
//#Product SKU: SEN0001
//#Version: 1.0
//#Description:
//#The sketch for using the URM37 autonomous mode from DFRobot
//#and writes the values to the serialport
//#Connection:
//#Vcc (Arduino)->Pin 1 VCC(URM V4.0)
//#GND (Arduino)->Pin 2 GND(URM V4.0)
//#Pin 3 (Arduino)->Pin 4 ECHO(URM V4.0)
//#Pin TX1(Arduino)->Pin 8 RXD(URM V4.0)
//#Pin RX0(Arduino)->Pin 9 TXD(URM V4.0)
//#Working Mode: autonomous mode.
int URECHO=3; // PWM Output 0-25000US,Every 50US represent 1cm
unsigned int x=0;
uint8_t EnPwmCmd[4]={0x44,0x02,0xaa,0xf0}; // distance measure command
//
void setup(){
Serial.begin(9600);
AutonomousMode_Setup();
}
//
void loop(){
AutonomousMode();
delay(100);
}
//
void AutonomousMode_Setup(){ // PWM mode setup function
pinMode(URECHO,INPUT); // Sending Enable PWM mode command
for(int i=0;i<4;i++)Serial.write(EnPwmCmd[i]);
}
//
void AutonomousMode(){
unsigned long DistanceMeasured=pulseIn(URECHO,LOW);
if(DistanceMeasured>=45000){ // the reading is invalid.
Serial.print("Invalid");
}
else{
x=DistanceMeasured/50; // every 50us low level stands for 1cm
Serial.print("Distance="); Serial.print(x); Serial.println("cm");
if(x>180){ 흰색ON; 적색OFF; } // 흰색 점등
else { 흰색OFF; 적색ON; } // 빨간색 점등
}
}
흰색과 적색 LED는 핀 설정하고 출력하는 코드를 추가하시고요
// MCU BASIC: https://www.basic4mcu.com
// DateTime : 2018-06-05 오후 5:23:33
// by Ok-Hyun Park
//
초음파 거리센서 URM37 코딩
//#Editor: Zrh from DFRobot
//#Data: 29.08.2014
//#Product name: ultrasonic scanner
//#Product SKU: SEN0001
//#Version: 1.0
//#Description:
//#The sketch for using the URM37 autonomous mode from DFRobot
//#and writes the values to the serialport
//#Connection:
//#Vcc (Arduino)->Pin 1 VCC(URM V4.0)
//#GND (Arduino)->Pin 2 GND(URM V4.0)
//#Pin 3 (Arduino)->Pin 4 ECHO(URM V4.0)
//#Pin TX1(Arduino)->Pin 8 RXD(URM V4.0)
//#Pin RX0(Arduino)->Pin 9 TXD(URM V4.0)
//#Working Mode: autonomous mode.
int URECHO=3; // PWM Output 0-25000US,Every 50US represent 1cm
unsigned int x=0;
uint8_t EnPwmCmd[4]={0x44,0x02,0xaa,0xf0}; // distance measure command
//
void setup(){
Serial.begin(9600);
AutonomousMode_Setup();
}
//
void loop(){
AutonomousMode();
delay(100);
}
//
void AutonomousMode_Setup(){ // PWM mode setup function
pinMode(URECHO,INPUT); // Sending Enable PWM mode command
for(int i=0;i<4;i++)Serial.write(EnPwmCmd[i]);
}
//
int flag=0;
//
void AutonomousMode(){
unsigned long DistanceMeasured=pulseIn(URECHO,LOW);
if(DistanceMeasured>=45000){ // the reading is invalid.
Serial.print("Invalid");
}
else{
x=DistanceMeasured/50; // every 50us low level stands for 1cm
Serial.print("Distance="); Serial.print(x); Serial.println("cm");
if(x>180){ flag=0; 흰색ON; } // 흰색 점등
else{
if(flag==0){ flag=1; 흰색OFF; 적색ON; delay(1000); 적색OFF; } // 빨간색 점등
}
}
}
1초 점등을 1회만 하는 코드는 이렇게 작성하면 됩니다.
180cm 이하에서 1회만 적색1초 점등후 OFF
180cm 이상이 되면 flag 초기화
댓글 : 0
조회수 : 10,391
등록된 댓글이 없습니다.