Advertisement
kerelius

Arduino

Dec 1st, 2021
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  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.     Serial.begin(9600);
  23.  
  24.     pinMode(BuzzersA, OUTPUT);
  25.     pinMode(BuzzersB, OUTPUT);
  26.     pinMode(BuzzersC, OUTPUT);
  27.     pinMode(BuzzersD, OUTPUT);
  28.  
  29.     pinMode(Sensor, INPUT);
  30.     pinMode(ResetButton, INPUT);
  31. }
  32.  
  33. void loop()
  34. {
  35.     CurrentSensorState = digitalRead(Sensor);
  36.  
  37.     if (CurrentSensorState != LastSensorState)
  38.     {
  39.         updateState();
  40.     }
  41.     else
  42.     {
  43.         updateCounter();    // sensor state not changed. It runs in a loop.
  44.     }
  45.  
  46.     LastSensorState = CurrentSensorState;
  47. }
  48.  
  49. void updateState()
  50. {
  51.     // the vibration has been just started
  52.     if (CurrentSensorState == HIGH)
  53.     {
  54.         startVibrating = millis();
  55.         idleTime = startVibrating - endVibrating;
  56.         //Serial.print("Idled Time: ");
  57.         //Serial.println(idleTime);
  58.     }
  59.     else
  60.     {
  61.         endVibrating = millis();
  62.         VibTime = endVibrating - startVibrating;
  63.         //Serial.print("Vibrated Time: ");
  64.         //Serial.println(VibTime);
  65.     }
  66. }
  67.  
  68. void updateCounter()
  69. {
  70.     // the vibration is still occuring
  71.     if (CurrentSensorState == HIGH)
  72.     {
  73.         VibTime = millis() - startVibrating;
  74.         Serial.print("Vibrating Time: ");
  75.         seconds = VibTime / 1000;
  76.         minutes = seconds / 60;
  77.         //Serial.println(VibTime / 100);
  78.        
  79.         if (seconds >= 30)
  80.         {
  81.             BuzzersAB_On();
  82.         }
  83.        
  84.         if (minutes >= 5)
  85.         {
  86.             BuzzersCD_On();
  87.         }
  88.     }
  89.     else
  90.     {
  91.         idleTime = millis() - endVibrating;
  92.         if (digitalRead(ResetButton) == HIGH)
  93.         {
  94.             Buzzers_Off();
  95.             Serial.println("Reset ...");
  96.         }
  97.         delay(3000);
  98.  
  99.         //Serial.print("Idling Time: ");
  100.         //Serial.println(idleTime);
  101.     }
  102. }
  103.  
  104. void BuzzersAB_On()
  105. {
  106.     //tone(BuzzersAB, 500);
  107.     digitalWrite(BuzzersA, HIGH);
  108.     digitalWrite(BuzzersB, HIGH);
  109.     return;
  110. }
  111.  
  112. void BuzzersCD_On()
  113. {
  114.     //tone(BuzzersCD, 500);
  115.     digitalWrite(BuzzersC, HIGH);
  116.     digitalWrite(BuzzersD, HIGH);
  117.     return;
  118. }
  119.  
  120. void Buzzers_Off()
  121. {
  122.     digitalWrite(BuzzersA, LOW);
  123.     digitalWrite(BuzzersB, LOW);
  124.     digitalWrite(BuzzersC, LOW);
  125.     digitalWrite(BuzzersD, LOW);
  126.     return;
  127. }
  128.  
  129. /*
  130. bool isReset(void){
  131.     return (digitalRead(ResetButton) == HIGH) ? true : false;
  132. }
  133. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement