브레이크 밟기에 따른 제동등 밝기조절 #3
<밟는 조건에 따른 코드>
int trig = 2;
int echo = 3;
float duration,distance,FirstDistance=0;
float SecondDistance=0;
float speed=0;
void setup(){
Serial.begin(19200);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(5,OUTPUT);
pinMode(4,OUTPUT);
pinMode(8,OUTPUT);
}
void loop() {
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
duration=pulseIn(echo, HIGH);
FirstDistance=((float)(340*duration)/10000)/2;
delay(100);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
duration=pulseIn(echo, HIGH);
SecondDistance=((float)(340*duration)/10000)/2;
delay(100);
speed=(SecondDistance - FirstDistance/1.0);
Serial.print("Speed in cm/s : ");
Serial.println(speed);
Serial.println(analogRead(A0));
Serial.print("distance : ");
Serial.println(FirstDistance);
int sensorval = analogRead(A0);
if (sensorval < 500){
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
}
if ( sensorval >= 500 ){
digitalWrite(5,LOW);
digitalWrite(4,HIGH);
}
if (FirstDistance <= 4 || FirstDistance > 15){
digitalWrite(5,LOW);
digitalWrite(4,LOW);
}
if ( (sensorval >= 930) && (( (speed < -2 ) && (speed >= -15 )) || (speed < -15) )){
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
delay(100);
digitalWrite(5,HIGH);
digitalWrite(4,HIGH);
delay(100);
digitalWrite(5,LOW);
digitalWrite(4,LOW);
digitalWrite(8,HIGH);
delay(500);
digitalWrite(8,LOW);
delay(500);
digitalWrite(8,HIGH);
delay(500);
digitalWrite(8,LOW);
delay(500);
digitalWrite(8,HIGH);
delay(500);
digitalWrite(8,LOW);
delay(500);
digitalWrite(8,HIGH);
delay(500);
digitalWrite(8,LOW);
delay(500);
digitalWrite(8,HIGH);
delay(500);
digitalWrite(8,LOW);
delay(500);
digitalWrite(8,HIGH);
delay(500);
digitalWrite(8,LOW);
delay(500);
digitalWrite(8,HIGH);
delay(500);
digitalWrite(8,LOW);
delay(500);
digitalWrite(8,HIGH);
delay(500);
digitalWrite(8,LOW);
delay(500);
}
}