kerelius

arduino 2

Dec 1st, 2021 (edited)
722
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 LastSensorState = 0;    //LastSensorState
  11.  
  12. int startVibrating = 0; // the moment the vibration was started
  13. int endVibrating = 0;   // the moment the vibration was stoped
  14. int VibTime = 0;    // how long the vibration lasted
  15. int idleTime = 0;   // how long the sensor was idle
  16.  
  17. int seconds = 0;
  18. int minutes = 0;
  19.  
  20. void setup()
  21. {
  22.     pinMode(BuzzersA, OUTPUT);
  23.     pinMode(BuzzersB, OUTPUT);
  24.     pinMode(BuzzersC, OUTPUT);
  25.     pinMode(BuzzersD, OUTPUT);
  26.  
  27.     pinMode(Sensor, INPUT);
  28.     pinMode(ResetButton, INPUT);
  29.  
  30.     Serial.begin(9600);
  31. }
  32.  
  33. void loop()
  34. {
  35.     Serial.println(digitalRead(Sensor));
  36.     if (digitalRead(ResetButton) == HIGH)
  37.     {
  38.         delay(3000);
  39.         Buzzers_Off();
  40.         Serial.println("Reset ...");
  41.        
  42.     }
  43.  
  44.  
  45.     CurrentSensorState = digitalRead(Sensor);
  46.  
  47.     if (CurrentSensorState != LastSensorState)
  48.     {
  49.         updateState();
  50.     }
  51.     else
  52.     {
  53.         updateCounter();    // sensor state not changed. It runs in a loop.
  54.     }
  55.  
  56.     LastSensorState = CurrentSensorState;
  57.     delay(100);
  58. }
  59.  
  60. void updateState()
  61. {
  62.     // the vibration has been just started
  63.     if (CurrentSensorState == HIGH)
  64.     {
  65.         startVibrating = millis();
  66.         idleTime = startVibrating - endVibrating;
  67.         //Serial.print("Idled Time: ");
  68.         //Serial.println(idleTime);
  69.     }
  70.     else
  71.     {
  72.         endVibrating = millis();
  73.         VibTime = endVibrating - startVibrating;
  74.         //Serial.print("Vibrated Time: ");
  75.         //Serial.println(VibTime);
  76.     }
  77. }
  78.  
  79. void updateCounter()
  80. {
  81.     // the vibration is still occuring
  82.     if (CurrentSensorState == HIGH)
  83.     {
  84.         VibTime = millis() - startVibrating;
  85.         //Serial.print("Vibrating Time: ");
  86.         seconds = VibTime / 1000;
  87.         minutes = seconds / 60;
  88.         //Serial.println(seconds);
  89.        
  90.         if (seconds >= 30)
  91.         {
  92.             BuzzersAB_On();
  93.         }
  94.        
  95.         if (minutes >= 5)
  96.         {
  97.             BuzzersCD_On();
  98.         }
  99.     }
  100.     else
  101.     {
  102.         idleTime = millis() - endVibrating;
  103. /*          if (digitalRead(ResetButton) == HIGH)
  104.         {
  105.             Buzzers_Off();
  106.             Serial.println("Reset ...");
  107.         }
  108.         delay(3000); */
  109.  
  110.         //Serial.print("Idling Time: ");
  111.         //Serial.println(idleTime);
  112.     }
  113. }
  114.  
  115. void BuzzersAB_On()
  116. {
  117.     //tone(BuzzersAB, 500);
  118.     digitalWrite(BuzzersA, HIGH);
  119.     digitalWrite(BuzzersB, HIGH);
  120.     return;
  121. }
  122.  
  123. void BuzzersCD_On()
  124. {
  125.     //tone(BuzzersCD, 500);
  126.     digitalWrite(BuzzersC, HIGH);
  127.     digitalWrite(BuzzersD, HIGH);
  128.     return;
  129. }
  130.  
  131. void Buzzers_Off()
  132. {
  133.     digitalWrite(BuzzersA, LOW);
  134.     digitalWrite(BuzzersB, LOW);
  135.     digitalWrite(BuzzersC, LOW);
  136.     digitalWrite(BuzzersD, LOW);
  137.     return;
  138. }
  139.  
  140. /*
  141. bool isReset(void){
  142.     return (digitalRead(ResetButton) == HIGH) ? true : false;
  143. }
  144. */
RAW Paste Data