Advertisement
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 LastSensorState = 0; //LastSensorState
- int startVibrating = 0; // the moment the vibration was started
- int endVibrating = 0; // the moment the vibration was stoped
- int VibTime = 0; // how long the vibration lasted
- int idleTime = 0; // how long the sensor was idle
- int seconds = 0;
- int 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);
- if (CurrentSensorState != LastSensorState)
- {
- updateState();
- }
- else
- {
- updateCounter(); // sensor state not changed. It runs in a loop.
- }
- LastSensorState = CurrentSensorState;
- }
- void updateState()
- {
- // the vibration has been just started
- if (CurrentSensorState == HIGH)
- {
- startVibrating = millis();
- idleTime = startVibrating - endVibrating;
- //Serial.print("Idled Time: ");
- //Serial.println(idleTime);
- }
- else
- {
- endVibrating = millis();
- VibTime = endVibrating - startVibrating;
- //Serial.print("Vibrated Time: ");
- //Serial.println(VibTime);
- }
- }
- void updateCounter()
- {
- // the vibration is still occuring
- if (CurrentSensorState == HIGH)
- {
- VibTime = millis() - startVibrating;
- Serial.print("Vibrating Time: ");
- seconds = VibTime / 1000;
- minutes = seconds / 60;
- //Serial.println(VibTime / 100);
- if (seconds >= 30)
- {
- BuzzersAB_On();
- }
- if (minutes >= 5)
- {
- BuzzersCD_On();
- }
- }
- else
- {
- idleTime = millis() - endVibrating;
- if (digitalRead(ResetButton) == HIGH)
- {
- Buzzers_Off();
- Serial.println("Reset ...");
- }
- delay(3000);
- //Serial.print("Idling Time: ");
- //Serial.println(idleTime);
- }
- }
- void BuzzersAB_On()
- {
- //tone(BuzzersAB, 500);
- digitalWrite(BuzzersA, HIGH);
- digitalWrite(BuzzersB, HIGH);
- return;
- }
- void BuzzersCD_On()
- {
- //tone(BuzzersCD, 500);
- digitalWrite(BuzzersC, HIGH);
- digitalWrite(BuzzersD, HIGH);
- return;
- }
- void Buzzers_Off()
- {
- digitalWrite(BuzzersA, LOW);
- digitalWrite(BuzzersB, LOW);
- digitalWrite(BuzzersC, LOW);
- digitalWrite(BuzzersD, LOW);
- return;
- }
- /*
- bool isReset(void){
- return (digitalRead(ResetButton) == HIGH) ? true : false;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement