초음파센서로 리니어 레일을 움직이는 코드 입니다. 지금 15.0으로 고정돼있는 target값을 시리얼 모니터로 3~30으로 바꿀 수 있게 하려면 어떻게 해야 할까요?
// defines pins numbers
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;
int trig = 10;
int echo = 9;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
}
void loop() {
float target = 15.0;
digitalWrite(trig, LOW);
digitalWrite(echo, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
unsigned long duration = pulseIn(echo, HIGH);
float distance = duration / 29.0 / 2.0;
Serial.print(distance);
Serial.println("cm");
float setdis = distance - target;
Serial.print(setdis);
if(setdis > 2){
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 12000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(60);
digitalWrite(stepPin,LOW);
delayMicroseconds(60);
if(setdis < 1 && setdis > -1){
break;
}
}
}else if(setdis < -2){
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 12000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(60);
digitalWrite(stepPin,LOW);
delayMicroseconds(60);
if(setdis < 1 && setdis > -1){
break;
}
}
}else{
}
delay(1000); // One second delay
}
|
이전글 |
마스크 착용 유무를 확인 예제 문의
|
2021-10-04 |