Advertisement
pippero

Untitled

Apr 15th, 2021
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. const int PIN_BUZZER = 8;
  2. unsigned long TempoInizio = 0;
  3. int range = 99;
  4. bool old = HIGH;
  5. bool puls = LOW;
  6. void setup() {
  7. Serial.begin(9600);
  8. pinMode(PIN_BUZZER, OUTPUT);// buzzer
  9. pinMode(12, INPUT_PULLUP);
  10. }
  11.  
  12. void loop() {
  13. puls = digitalRead(12);
  14.  
  15. //serve per eseguire il comando
  16. //solo 1 volta quando premo
  17. if (digitalRead(12) == LOW && old == HIGH) {
  18. TempoInizio = millis();
  19. range = 0;
  20. old != puls;
  21. }
  22. switch (range) {
  23. case 0: { // your hand is on the sensor
  24. tone(PIN_BUZZER, 100);
  25. if ( millis() - TempoInizio > 500) {
  26. TempoInizio = millis();
  27. range = 1;
  28. }
  29. }
  30. break;
  31. case 1: { // your hand is close to the sensor
  32. tone(PIN_BUZZER, 500);
  33. if ( millis() - TempoInizio > 500) {
  34. TempoInizio = millis();
  35. range = 2;
  36. }
  37. }
  38. break;
  39. case 2: {
  40. tone(PIN_BUZZER, 800);
  41. if ( millis() - TempoInizio > 800) {
  42. TempoInizio = millis();
  43. range = 3;
  44. }
  45. }
  46. break;
  47. case 3: {
  48. tone(PIN_BUZZER, 1700);
  49. if ( millis() - TempoInizio > 1700) {
  50. TempoInizio = 0;
  51. range = 4;
  52. }
  53. }
  54. break;
  55. case 4: { // your hand is nowhere near the sensor
  56. noTone(PIN_BUZZER);
  57. break;
  58. }
  59. }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement