Anti sleep alarm for drivers


Arduino code:

#define Relay 13
#define buzzer A0
static const int sensorPin = 10;                    // sensor input pin 
int SensorStatePrevious = LOW;                      // previousstate of the sensor

unsigned long minSensorDuration = 3000; // Time we wait before  the sensor active as long 
unsigned long minSensorDuration2 = 6000;
unsigned long SensorLongMillis;                // Time in ms when the sensor was active
bool SensorStateLongTime = false;                  // True if it is a long active

const int intervalSensor = 50;                      // Time between two readings sensor state
unsigned long previousSensorMillis;                 // Timestamp of the latest reading

unsigned long SensorOutDuration;                  // Time the sensor is active in ms

//// GENERAL ////

unsigned long currentMillis;          // Variabele to store the number of milleseconds since the Arduino has started

void setup() {
  Serial.begin(9600);                 // Initialise the serial monitor

  pinMode(sensorPin, INPUT);          // set sensorPin as input
  Serial.println("Press button");
  pinMode(Relay,OUTPUT);
  pinMode(buzzer,OUTPUT);
}

// Function for reading the sensor state
void readSensorState() {

  // If the difference in time between the previous reading is larger than intervalsensor
  if(currentMillis - previousSensorMillis > intervalSensor) {
    
    // Read the digital value of the sensor (LOW/HIGH)
    int SensorState = digitalRead(sensorPin);    

    // If the button has been active AND
    // If the sensor wasn't activated before AND
    // IF there was not already a measurement running to determine how long the sensor has been activated
    if (SensorState == LOW && SensorStatePrevious == HIGH && !SensorStateLongTime) {
     SensorLongMillis = currentMillis;
       SensorStatePrevious = LOW;
      
      Serial.println("Button pressed");
    }

    // Calculate how long the sensor has been activated
   SensorOutDuration = currentMillis - SensorLongMillis;

    // If the button is active AND
    // If there is no measurement running to determine how long the sensor is active AND
    // If the time the sensor has been activated is larger or equal to the time needed for a long active
    if (SensorState == LOW && !SensorStateLongTime && SensorOutDuration >= minSensorDuration) {
      SensorStateLongTime = true;
      digitalWrite(Relay,HIGH);
      Serial.println("Button long pressed");
    }
    if (SensorState == LOW && SensorStateLongTime && SensorOutDuration >= minSensorDuration2) {
     SensorStateLongTime = true;
      digitalWrite(buzzer,HIGH);
      delay(1000);
      Serial.println("Button long pressed");
    }
      
    // If the sensor is released AND
    // If the sensor was activated before
    if (SensorState == HIGH && SensorStatePrevious == LOW) {
      SensorStatePrevious = HIGH;
      SensorStateLongTime = false;
      digitalWrite(Relay,LOW);
      digitalWrite(buzzer,LOW);
      Serial.println("Button released");

  
    }
    
    // store the current timestamp in previousSensorMillis
   previousSensorMillis = currentMillis;

  }

}

void loop() {

  currentMillis = millis();    // store the current time
  readSensorState();           // read the sensor state
  
}

Post a Comment

27 Comments

  1. There is a error showoin the code

    A function definition is not allowed before token.

    ReplyDelete
    Replies
    1. please tell me what is is the error in the code?

      Delete
    2. sir can u plzzz predict and solve the errors in this

      Delete
  2. Please give solution I have to make a project

    ReplyDelete
  3. Replies
    1. The code is wrong please correct it I have to submit my project before 15 august

      Delete
  4. can you please provide report for this project

    ReplyDelete
  5. Code is not working ... There are so many errors... Please try to resend it

    ReplyDelete
  6. are vai aapka project ka jo circuit hai woo galat hai

    ReplyDelete
  7. code is not working.... ple sent solution

    ReplyDelete
  8. Sir can we do this exp first on online simulator if yes on which simulator we can be performed this exp

    ReplyDelete
  9. Missing FQBN error ho raha hay .Please help

    ReplyDelete
    Replies
    1. this error will occur if no board is selected

      Delete
  10. Can you provide me readymade 9466061256

    ReplyDelete
  11. Hai sir, could you send me a programmed nano for this project.. Also I need your contact number for some technical help.. Thanks

    ReplyDelete
  12. bro plz tell how to upload that code in arduino nano

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. Please send the proper code once again and can u pls explain the same

    ReplyDelete
  16. Sir code is not wrking sir, there is a error is shown in thse

    ReplyDelete
  17. Sketch uses 2556 bytes (8%) of program storage space. Maximum is 30720 bytes.
    Global variables use 271 bytes (13%) of dynamic memory, leaving 1777 bytes for local variables. Maximum is 2048 bytes.
    avrdude: ser_open(): can't open device "\\.\COM7": Access is denied.


    Failed uploading: uploading error: exit status 1

    ReplyDelete
  18. Hi @Raman, I am also seeing this issue while uploading code in Adruino Nano , Can you suggest me how to fix it ?

    ReplyDelete