//
int MOT1A = 3; //
int MOT1B = 2; //
int SpeedPin_A = 7; //
//
int MOT2A = 5; //
int MOT2B = 4; //
int SpeedPin_B = 6; //
int distance = 0;
void setup() {
Serial.begin(9600);
pinMode(MOT1A, OUTPUT);
pinMode(MOT1B, OUTPUT);
pinMode(MOT2A, OUTPUT);
pinMode(MOT2B, OUTPUT);
pinMode(SpeedPin_A, OUTPUT);
pinMode(SpeedPin_B, OUTPUT);
}
void loop() {
int volt = map(analogRead(A0), 0, 1023, 0, 5000);
distance = (27.61 / (volt - 0.1696)) * 1000;
Serial.print(distance);
Serial.println(" cm");
Serial.println(" ");
if (distance > 20) {
motor_up();
delay(3000);
motor_stop();
} else {
motor_down();
delay(3000);
motor_stop();
}
}
void motor_stop()
{
digitalWrite(MOT1A, LOW);
digitalWrite(MOT1B, LOW);
analogWrite(SpeedPin_A, 130);
digitalWrite(MOT2A, LOW);
digitalWrite(MOT2B, LOW);
analogWrite(SpeedPin_B, 130);
}
void motor_down()
{
motorA_Reverse();
delay(900);
motorB_Reverse();
delay(400);
}
void motor_up() {
motorA_Rotation();
delay(400);
motorB_Rotation();
delay(900);
}
void motorB_Reverse()
{
digitalWrite(MOT1A, LOW);
digitalWrite(MOT1B, LOW);
analogWrite(SpeedPin_A, 130);
digitalWrite(MOT2A, LOW);
digitalWrite(MOT2B, HIGH);
analogWrite(SpeedPin_B, 130);
}
void motorA_Reverse()
{
digitalWrite(MOT1A, LOW);
digitalWrite(MOT1B, HIGH);
analogWrite(SpeedPin_A, 130);
digitalWrite(MOT2A, LOW);
digitalWrite(MOT2B, LOW);
analogWrite(SpeedPin_B, 130);
}
void motorB_Rotation()
{
digitalWrite(MOT1A, LOW);
digitalWrite(MOT1B, LOW);
analogWrite(SpeedPin_A, 130);
digitalWrite(MOT2A, HIGH);
digitalWrite(MOT2B, LOW);
analogWrite(SpeedPin_B, 130);
}
void motorA_Rotation()
{
digitalWrite(MOT1A, HIGH);
digitalWrite(MOT1B, LOW);
analogWrite(SpeedPin_A, 130);
digitalWrite(MOT2A, LOW);
digitalWrite(MOT2B, LOW);
analogWrite(SpeedPin_B, 130);
}
-----------------------------------------
적외선센서에 물체가 닿았을때 모터 2개가 반대로 상하운동하면서 물체를 올리고 내리는
코딩입니다.
그런데 일정딜레이시간동안 상하운동을 하며 올라갔다가 정지시키고 다시 물체가 사라졌을때
반대로 내려가게끔 하고싶은데
루프에서 정지가 안되다보니 올라갔다가 잠깐 정지하고 다시 올라가버리네요 ㅠㅠ
아예 멈춰버리게 하는 방법있을까요???
|