Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. #define trigPin 3
  2. #define echoPin 4
  3. #define vibrationPin 1
  4. #define potentiometrIn A1
  5. #define buttonPin 0
  6.  
  7. #define minDist 2
  8. #define maxDist 100
  9. const int t = 200;
  10.  
  11. long duration;
  12. long distance;
  13. long t1;
  14. long t2;
  15. long multiplier;
  16. long currentMax;
  17.  
  18. bool isOn = false;
  19. int buttonState = LOW;
  20.  
  21. int currentButtonState;
  22.  
  23. void setup() {
  24.   pinMode(trigPin, OUTPUT);
  25.   pinMode(echoPin, INPUT);
  26.   pinMode(vibrationPin, OUTPUT);
  27.   pinMode(potentiometrIn, INPUT);
  28. }
  29.  
  30.  
  31. void loop() {
  32.  
  33.   currentButtonState = digitalRead(buttonPin);
  34.   if (currentButtonState != buttonState) {
  35.     buttonState = currentButtonState;
  36.     if (buttonState == HIGH) {
  37.       isOn = !isOn;
  38.     }
  39.   }
  40.  
  41.   if (isOn) {
  42.     multiplier = analogRead(potentiometrIn);
  43.     multiplier = constrain(multiplier, 0, 1023);
  44.     currentMax = (maxDist * multiplier) / 1023 + 2*minDist;
  45.    
  46.     digitalWrite(trigPin, LOW);  
  47.     delayMicroseconds(2);
  48.     digitalWrite(trigPin, HIGH);
  49.     delayMicroseconds(10);
  50.     digitalWrite(trigPin, LOW);
  51.     duration = pulseIn(echoPin, HIGH);
  52.     distance = duration / 58;
  53.     distance = constrain(distance, minDist, currentMax);
  54.  
  55.  
  56.    
  57.     t2 = (long(t)*(distance-minDist))/(currentMax-minDist);
  58.     t1 = t - t2;
  59.      
  60.     if (t1 >= 3) {
  61.       digitalWrite(vibrationPin, HIGH);
  62.       delay(t1);
  63.     }
  64.    
  65.     if (t2 >= 3) {
  66.       digitalWrite(vibrationPin, LOW);
  67.       delay(t2);
  68.     }
  69.   } else {
  70.     digitalWrite(vibrationPin, LOW);
  71.   }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement