Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int BuzzersA = 13;
- const int BuzzersB = 12;
- const int BuzzersC = 11;
- const int BuzzersD = 10;
- const int Sensor = 2;
- const int ResetButton = 4;
- int currentSensorState = 0; //currentSensorState
- int secondCurrentSensorState = 0; //currentSensorState
- int thirdCurrentSensorState = 0; //currentSensorState
- int fourthCurrentSensorState = 0; //currentSensorState
- int lastSensorState = 0; //lastSensorState
- int secondLastSensorState = 0; //lastSensorState
- int thirdLastSensorState = 0; //lastSensorState
- int fourthLastSensorState = 0; //lastSensorState
- double startVibrating = 0; // the moment the vibration was started
- double endVibrating = 0; // the moment the vibration was stoped
- double vibingTime = 0; // how long the vibration lasted
- double idlingTime = 0; // how long the sensor was idle
- double vibedTime = 0;
- double idledTime = 0;
- double seconds = 0;
- double minutes = 0;
- void setup()
- {
- Serial.begin(9600);
- pinMode(BuzzersA, OUTPUT);
- pinMode(BuzzersB, OUTPUT);
- pinMode(BuzzersC, OUTPUT);
- pinMode(BuzzersD, OUTPUT);
- pinMode(Sensor, INPUT);
- pinMode(ResetButton, INPUT);
- }
- void loop()
- {
- currentSensorState = digitalRead(Sensor);
- secondCurrentSensorState = digitalRead(Sensor);
- thirdCurrentSensorState = digitalRead(Sensor);
- fourthCurrentSensorState = digitalRead(Sensor);
- if (digitalRead(ResetButton) == HIGH)
- {
- //delay(300);
- Buzzers_Off();
- Serial.println("Reset ...");
- currentSensorState = 0;
- secondCurrentSensorState = 0;
- thirdCurrentSensorState = 0;
- fourthCurrentSensorState = 0;
- }
- if (currentSensorState != lastSensorState && secondCurrentSensorState != secondLastSensorState && thirdCurrentSensorState != thirdLastSensorState && fourthCurrentSensorState != fourthLastSensorState)//&& idledTime < 300 && vibedTime < 300)
- {
- updateState();
- }
- else
- {
- updateCounter(); // sensor state not changed. It runs in a loop.
- }
- lastSensorState = currentSensorState;
- secondLastSensorState = secondCurrentSensorState;
- thirdLastSensorState = thirdCurrentSensorState;
- fourthLastSensorState = fourthCurrentSensorState;
- delay(400);
- }
- void updateState()
- {
- // the vibration has been just started
- if (currentSensorState != HIGH && secondCurrentSensorState != HIGH && thirdCurrentSensorState != HIGH && fourthCurrentSensorState != HIGH)
- {
- endVibrating = millis();
- vibedTime = endVibrating - startVibrating;
- Serial.print("Vibrated Time: ");
- Serial.println(vibedTime/1000);
- }
- else if ((currentSensorState == HIGH && secondCurrentSensorState == HIGH) || (secondCurrentSensorState == HIGH && thirdCurrentSensorState != HIGH) || (thirdCurrentSensorState == HIGH && fourthCurrentSensorState != HIGH))
- {
- startVibrating = millis();
- idledTime = startVibrating - endVibrating;
- Serial.print("Idled Time: ");
- Serial.println(idledTime/1000);
- }
- else {
- return;
- }
- }
- void updateCounter()
- {
- // the vibration is still occuring
- if (currentSensorState == HIGH || secondCurrentSensorState == HIGH || thirdCurrentSensorState == HIGH || fourthCurrentSensorState == HIGH)
- {
- vibingTime = millis() - startVibrating;
- Serial.print("Vibrating Time: ");
- seconds = vibingTime / 1000;
- minutes = seconds / 60;
- Serial.println(seconds);
- if (seconds >= 7.5)
- {
- BuzzersAB_On();
- }
- if (seconds >= 75)
- {
- BuzzersCD_On();
- }
- }
- else
- {
- idlingTime = millis() - endVibrating;
- Serial.print("Idling Time: ");
- Serial.println(idlingTime/1000);
- }
- }
- void BuzzersAB_On()
- {
- digitalWrite(BuzzersA, HIGH);
- digitalWrite(BuzzersB, HIGH);
- return;
- }
- void BuzzersCD_On()
- {
- digitalWrite(BuzzersC, HIGH);
- digitalWrite(BuzzersD, HIGH);
- return;
- }
- void Buzzers_Off()
- {
- digitalWrite(BuzzersA, LOW);
- digitalWrite(BuzzersB, LOW);
- digitalWrite(BuzzersC, LOW);
- digitalWrite(BuzzersD, LOW);
- return;
- }
Add Comment
Please, Sign In to add comment