Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #define TRIG_PIN 9
- #define ECHO_PIN 8
- #define SOUND_PIN 12
- #define BUTTON_PIN 2
- #define RED_LIGHT 5
- #define BLUE_LIGHT 6
- int LIGHT_RED_TIME = 500;
- int LIGHT_BLUE_TIME = 500;
- long long start;
- long long lastToggle;
- long long lampTimer;
- bool active;
- LiquidCrystal lcd(11, 10, 7, 4, 3, 1);
- void setup(){
- Serial.begin(115200);
- pinMode(ECHO_PIN, INPUT);
- pinMode(TRIG_PIN, OUTPUT);
- pinMode(SOUND_PIN, OUTPUT);
- pinMode(BUTTON_PIN, INPUT);
- pinMode(RED_LIGHT, OUTPUT);
- pinMode(BLUE_LIGHT, OUTPUT);
- lastToggle = millis();
- active = false;
- lcd.begin(16,2);
- }
- void skriv(){
- lcd.print("Ringer polis");
- lcd.clear();
- }
- float getDistance(){
- digitalWrite(TRIG_PIN, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIG_PIN, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIG_PIN, LOW);
- double duration = pulseIn(ECHO_PIN, HIGH);
- return(0.0343*duration/2);
- }
- bool isButtonPressed(){
- if (digitalRead(BUTTON_PIN)){
- if (millis() - lastToggle > 250){
- lastToggle = millis();
- digitalWrite(RED_LIGHT, LOW);
- digitalWrite(BLUE_LIGHT, LOW);
- return(true);
- }
- }
- return(false);
- }
- void alarm(){
- lampTimer = millis();
- bool redOn = false;
- while (!isButtonPressed()){
- noTone(SOUND_PIN);
- if (millis() - lampTimer > LIGHT_BLUE_TIME && redOn == false){
- redOn = true;
- digitalWrite(RED_LIGHT, HIGH);
- digitalWrite(BLUE_LIGHT, LOW);
- lampTimer = millis();
- LIGHT_RED_TIME = getDistance() * 10;
- }
- if (millis() - lampTimer > LIGHT_RED_TIME && redOn == true){
- redOn = false;
- digitalWrite(RED_LIGHT, LOW);
- digitalWrite(BLUE_LIGHT, HIGH);
- lampTimer = millis();
- LIGHT_BLUE_TIME = getDistance() * 10;
- }
- skriv();
- }
- }
- void loop(){
- if (getDistance() < 50 && active == false){
- tone(SOUND_PIN, 400);
- start = millis();
- active = true;
- }
- while (active){
- if (millis() - start < 5000){
- if (isButtonPressed()){
- active = false;
- noTone(SOUND_PIN);
- delay(3000);
- break;
- }
- }
- if (millis() - start > 5000){
- active = false;
- alarm();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement