int fmq=2;
int kg=3;
int led=4;
int tp=5;
int ep=6;
int RG=A0;
int RGvol=0;
int cm=0;
int ledvol=0;
float cread(int t, int e) {
digitalWrite(tp, LOW);
delayMicroseconds(2);
digitalWrite(tp, HIGH);
delayMicroseconds(10);
digitalWrite(tp, LOW);
float distance = pulseIn(ep, HIGH, 30000);
distance = distance / 58.0;
return distance;
}
void setup() {
pinMode(fmq,OUTPUT);
digitalWrite(fmq,LOW);
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
pinMode(tp,OUTPUT);
digitalWrite(tp,LOW);
pinMode(ep,INPUT);
digitalWrite(ep,LOW);
pinMode(kg,INPUT);
digitalWrite(kg,HIGH);
Serial.begin(9600);
}
void loop() {
cm=cread(tp,ep);
if(cm<76){
RGvol=analogRead(RG);
if(RGvol>200){
RGvol=200;
}
if(RGvol<0);
RGvol=0;
ledvol=255-RGvol;
digitalWrite(fmq,HIGH);
delay(1000);
digitalWrite(fmq,LOW);
analogWrite(led,ledvol);
delay(1000);
digitalWrite(led,LOW);
if(digitalRead(kg)==LOW){
while(cm<80){
cm=80;
digitalWrite(led,LOW);
digitalWrite(fmq,LOW);
}
}else{
digitalWrite(led,LOW);
digitalWrite(fmq,LOW);
}
}
digitalWrite(fmq,LOW);
//Serial.println(cm);
} |