The forum offers a section for (paid for) help: Jobs and Paid Consultancy - Arduino Forum .
You will have to
But we're here to help.
As it's not quite clear to me what does not work as expected, below the steps that I would take; you might already have done them, in that case skip.
I suggest that you break your code down into pieces.
(1) Get your sensor working with some simple code like below. Do not connect anything but the sensor; power wil be supplied by the USB port of the PC.
#define SENSE A0
void setup()
{
// serial communication for debugging
Serial.begin(115200);
Serial.println(F("starting"));
// configure pins
pinMode(SENSE, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
// led of to start with
digitalWrite(LED_BUILTIN, LOW);
}
void loop()
{
// read the sensor
uint8_t sensorStatus = digitalRead(SENSE);
// for debugging, print
Serial.print(F("Sense input: "));
Serial.println(sensorStatus);
// to prevent serial monitor to be spammed
delay(1000);
}
(2) Once that behaves as expected, you can add the LED on pin 13; below the updated loop() function. Still nothing but the sensor connected.
void loop()
{
// read the sensor
uint8_t sensorStatus = digitalRead(SENSE);
// for debugging, print
Serial.print(F("Sense input: "));
Serial.println(sensorStatus);
// control the led / motor / buzzer
digitalWrite(LED_BUILTIN, sensorStatus);
// to prevent serial monitor to be spammed
delay(1000);
}
As I don't know the sensor, you might have to invert the reading. You can do that e.g. as shown below; I will show another way using a macro in (5).
// read the sensor
uint8_t sensorStatus = !digitalRead(SENSE);
(3) If that behaves as expected, you can add the buzzer and the vibration motor one by one. Each time, test.
(4) Once that behaves as you want / expect, disconnect the USB and power from the battery. Does it still work? If not,
- The pins can only safely drive a load of 20 mA (40 mA amx); you will have to measure the total current with a multimeter. If it exceeds the 20 mA by a lot, you can use two pins; one to drive the buzzer and one to drive the motor.
- Your Arduino can't supply enough current to drive the motor and buzzer; the on-board regulator that converts the battery voltage to 5V is very limited when it comes to current. You will need an external converter to convert your battery voltage to 5V and feed the Arduino, the sensor, motor and buzzer.
Let us know if there is any problem up till now.
(5) Now you can start concentrating on the timing. You're requirement is / seems to be that after 2 seconds with closed eyes, you want the system to start beeping / vibrating. In the first step, your code must detect if the eyes became closed; therefore you have to keep track of the current state of the sensor and the provious state.
// macro to more easily evaluate the sensor status
#define EYES_ARE_CLOSED LOW
void loop()
{
// the previous sensor status
static uint8_t previousSensorStatus;
// read the sensor
uint8_t sensorStatus = digitalRead(SENSE);
// if the sensor status changed
if(sensorStatus != previousSensorStatus)
{
// and the eyes are closed
if(sensorStatus == EYES_ARE_CLOSED)
{
// for debugging, print
Serial.println(F("Eyes became closed"));
}
}
// remember sensor status
previousSensorStatus = sensorStatus;
}
If this goes as expected, you will 'see' only one message when the eyes became closed. Open the eyes again and close them again and you will 'see' the next message. It's a bit difficult to see with your eyes closed
If you see "Eyes became closed" when they were actually opened, you must change the #define to #define EYES_ARE_CLOSED HIGH.
Now you can add the timing.
// macro to more easily evaluate the sensor status
#define EYES_ARE_CLOSED LOW
void loop()
{
// the previous sensor status
static uint8_t previousSensorStatus;
// the times that eyes closed was detected
static uint32_t startTime;
// flag that the timer is started
static bool timerIsStarted = false;
// read the sensor
uint8_t sensorStatus = digitalRead(SENSE);
// if the sensor status changed
if(sensorStatus != previousSensorStatus)
{
// and the eyes are now closed
if(sensorStatus == EYES_ARE_CLOSED)
{
// record the start time
startTime = millis();
timerIsStarted = true;
// for debugging, print
Serial.print(F("Eyes became closed at "));
Serial.println(startTime);
}
}
// remember sensor status
previousSensorStatus = sensorStatus;
// check if the eyes are closed for two or more seconds
if(timerIsStarted == true && millis() - startTime >= 2000)
{
// activate buzzer and motor
digitalWrite(LED_BUILTIN, HIGH);
}
}
The only thing outstanding is to stop the buzzer / vibration sensor when the eyes are opened again.
// macro to more easily evaluate the sensor status
#define EYES_ARE_CLOSED LOW
void loop()
{
// the previous sensor status
static uint8_t previousSensorStatus;
// the times that eyes closed was detected
static uint32_t startTime;
// flag that the timer is started
static bool timerIsStarted = false;
// read the sensor
uint8_t sensorStatus = digitalRead(SENSE);
// if the sensor status changed
if(sensorStatus != previousSensorStatus)
{
// and the eyes are now closed
if(sensorStatus == EYES_ARE_CLOSED)
{
// record the start time
startTime = millis();
timerIsStarted = true;
// for debugging, print
Serial.print(F("Eyes closed at "));
Serial.println(startTime);
}
// eyes are open
else
{
// 'stop' timer
timerIsStarted = false;
// de-activate buzzer and motor
digitalWrite(LED_BUILTIN, LOW);
Serial.print(F("Eyes opened at "));
Serial.println(millis());
}
}
// remember sensor status
previousSensorStatus = sensorStatus;
// check if the eyes are closed for two or more seconds
if(timerIsStarted == true && millis() - startTime >= 2000)
{
// activate buzzer and motor
digitalWrite(LED_BUILTIN, HIGH);
}
}
And that is it. Ask if you have questions or if does not work; in that case, please provide (web) links to the sensor, the buzzer and the vibration motor.
As this no longer is a bootloader issue, I've moved your topic to a more suitable location on the forum.