Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int trigPin = 12;
- int echoPin = 11;
- long timeperiod, cm;
- const int analogOutPin = 9;
- int brightness;
- void setup()
- {
- Serial.begin(9600);
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- }
- void loop(){
- if (timeperiod > 50)
- analogWrite(analogOutPin,0);
- }
- {
- if (timeperiod 10 < 50){
- for(brightness = 0; brightness < 256; brightness++){
- analogWrite(led,brightness);
- delay(10);}
- }}
- if (timeperiod < 10)
- analogWrite(analogOutPin,0);
- {
- digitalWrite(trigPin, LOW);
- delayMicroseconds(20);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(0);
- digitalWrite(trigPin, LOW);
- timeperiod = pulseIn(echoPin, HIGH);
- cm = microsecondsToCentimeters(timeperiod);
- Serial.print("dist=");
- Serial.print(cm);
- Serial.println();
- delay(10);
- }
- long microsecondsToInches(long microseconds)
- {
- return microseconds / 74 /2;
- }
- long microsecondsToCentimeters(long microseconds)
- {
- return microseconds / 29/ 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement