kerelius

arduino

Dec 2nd, 2021 (edited)
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int BuzzersA = 13;
  2. const int BuzzersB = 12;
  3. const int BuzzersC = 11;
  4. const int BuzzersD = 10;
  5.  
  6. const int Sensor = 2;  
  7. const int ResetButton = 4;
  8.  
  9. int currentSensorState = 0; //currentSensorState
  10. int secondCurrentSensorState = 0;   //currentSensorState
  11. int thirdCurrentSensorState = 0;    //currentSensorState
  12. int fourthCurrentSensorState = 0;   //currentSensorState
  13.  
  14. int lastSensorState = 0;    //lastSensorState
  15. int secondLastSensorState = 0;  //lastSensorState
  16. int thirdLastSensorState = 0;   //lastSensorState
  17. int fourthLastSensorState = 0;  //lastSensorState
  18.  
  19. double startVibrating = 0;  // the moment the vibration was started
  20. double endVibrating = 0;    // the moment the vibration was stoped
  21.  
  22. double vibingTime = 0;  // how long the vibration lasted
  23. double idlingTime = 0;  // how long the sensor was idle
  24.  
  25. double vibedTime = 0;
  26. double idledTime = 0;
  27.  
  28. double seconds = 0;
  29. double minutes = 0;
  30.  
  31. void setup()
  32. {
  33.     Serial.begin(9600);
  34.  
  35.     pinMode(BuzzersA, OUTPUT);
  36.     pinMode(BuzzersB, OUTPUT);
  37.     pinMode(BuzzersC, OUTPUT);
  38.     pinMode(BuzzersD, OUTPUT);
  39.  
  40.     pinMode(Sensor, INPUT);
  41.     pinMode(ResetButton, INPUT);
  42. }
  43.  
  44. void loop()
  45. {
  46.     currentSensorState = digitalRead(Sensor);
  47.     secondCurrentSensorState = digitalRead(Sensor);
  48.     thirdCurrentSensorState = digitalRead(Sensor);
  49.     fourthCurrentSensorState = digitalRead(Sensor);
  50.  
  51.     if (digitalRead(ResetButton) == HIGH)
  52.     {
  53.         //delay(300);
  54.         Buzzers_Off();
  55.         Serial.println("Reset ...");
  56.         currentSensorState = 0;
  57.         secondCurrentSensorState = 0;
  58.         thirdCurrentSensorState = 0;
  59.         fourthCurrentSensorState = 0;
  60.        
  61.     }
  62.  
  63.     if (currentSensorState != lastSensorState && secondCurrentSensorState != secondLastSensorState && thirdCurrentSensorState != thirdLastSensorState && fourthCurrentSensorState != fourthLastSensorState)//&& idledTime < 300 && vibedTime < 300)
  64.     {
  65.         updateState();
  66.     }
  67.     else
  68.     {
  69.         updateCounter();    // sensor state not changed. It runs in a loop.
  70.     }
  71.  
  72.     lastSensorState = currentSensorState;
  73.     secondLastSensorState = secondCurrentSensorState;
  74.     thirdLastSensorState = thirdCurrentSensorState;
  75.     fourthLastSensorState = fourthCurrentSensorState;
  76.     delay(400);
  77. }
  78.  
  79. void updateState()
  80. {
  81.     // the vibration has been just started
  82.     if (currentSensorState != HIGH && secondCurrentSensorState != HIGH && thirdCurrentSensorState != HIGH && fourthCurrentSensorState != HIGH)
  83.     {
  84.         endVibrating = millis();
  85.         vibedTime = endVibrating - startVibrating;
  86.         Serial.print("Vibrated Time: ");
  87.         Serial.println(vibedTime/1000);
  88.     }
  89.     else if ((currentSensorState == HIGH && secondCurrentSensorState == HIGH)  ||  (secondCurrentSensorState == HIGH && thirdCurrentSensorState != HIGH) || (thirdCurrentSensorState == HIGH && fourthCurrentSensorState != HIGH))
  90.     {
  91.         startVibrating = millis();
  92.         idledTime = startVibrating - endVibrating;
  93.         Serial.print("Idled Time: ");
  94.         Serial.println(idledTime/1000);
  95.     }
  96.     else {
  97.         return;
  98.     }
  99. }
  100.  
  101. void updateCounter()
  102. {
  103.     // the vibration is still occuring
  104.     if (currentSensorState == HIGH || secondCurrentSensorState == HIGH || thirdCurrentSensorState == HIGH || fourthCurrentSensorState == HIGH)
  105.     {
  106.         vibingTime = millis() - startVibrating;
  107.         Serial.print("Vibrating Time: ");
  108.         seconds = vibingTime / 1000;
  109.         minutes = seconds / 60;
  110.         Serial.println(seconds);
  111.        
  112.         if (seconds >= 7.5)
  113.         {
  114.             BuzzersAB_On();
  115.         }
  116.        
  117.         if (seconds >= 75)
  118.         {
  119.             BuzzersCD_On();
  120.         }
  121.     }
  122.     else
  123.     {
  124.         idlingTime = millis() - endVibrating;
  125.         Serial.print("Idling Time: ");
  126.         Serial.println(idlingTime/1000);
  127.     }
  128. }
  129.  
  130. void BuzzersAB_On()
  131. {
  132.     digitalWrite(BuzzersA, HIGH);
  133.     digitalWrite(BuzzersB, HIGH);
  134.     return;
  135. }
  136.  
  137. void BuzzersCD_On()
  138. {
  139.     digitalWrite(BuzzersC, HIGH);
  140.     digitalWrite(BuzzersD, HIGH);
  141.     return;
  142. }
  143.  
  144. void Buzzers_Off()
  145. {
  146.     digitalWrite(BuzzersA, LOW);
  147.     digitalWrite(BuzzersB, LOW);
  148.     digitalWrite(BuzzersC, LOW);
  149.     digitalWrite(BuzzersD, LOW);
  150.     return;
  151. }
RAW Paste Data