Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #define LED_PIN 3
  2. #define SENSOR_PIN A0
  3.  
  4. const int threshold = 600;
  5.  
  6. float currentValue;
  7. float targetValue;
  8. float easingAmount;
  9. int sensorValue;
  10.  
  11. int lastTime;
  12. bool ledState = false;
  13.  
  14. void setup() {
  15. pinMode(LED_PIN, OUTPUT);
  16.  
  17. currentValue = 0;
  18. targetValue = 255;
  19. easingAmount = .125;
  20.  
  21. Serial.begin(9600);
  22. lastTime = millis();
  23. }
  24.  
  25. void loop() {
  26. //reading analog sensor value
  27. sensorValue = analogRead(SENSOR_PIN);
  28. Serial.print("Sensor Value: ");
  29. Serial.println(sensorValue);
  30.  
  31. if (sensorValue > threshold) {
  32.  
  33. currentValue += easingAmount * ( targetValue - currentValue );
  34.  
  35. Serial.print("currentValue: ");
  36. Serial.print(currentValue);
  37. Serial.print("\t targetValue: ");
  38. Serial.println(targetValue);
  39.  
  40. int cv = round(currentValue * 10) / 10;
  41.  
  42. if (cv == 255) {
  43. targetValue = 0;
  44. } else if (cv == 0) {
  45. targetValue = 255;
  46. }
  47.  
  48. analogWrite(LED_PIN, (int) currentValue);
  49.  
  50. delay(50);
  51.  
  52. } else {
  53. ledState = !ledState;
  54.  
  55. Serial.print("LED State: ");
  56. Serial.println(ledState);
  57.  
  58. digitalWrite(LED_PIN, ledState);
  59. delay(250);
  60. }
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement