I am using an IR sensor, arduino nano and motor, if IR sensor input is above 500(pn the analog 1023 system) for 0.5 second then the motor should turn on for 1 second then turn off. There should be a cooldown of 3 seconds. Within those 3 seconds the motor should not turn on even if the input is above 500 for more 0.5 seconds. I dont know how to generate requirement for input being above 500 for more than 1 second and I am not confident about rest of the code.
const int IR_PIN = A1;
const int MOTOR_PIN = 10;
const int MINI = 500; //minimum input received by sensor to send output in 1023 system
void setup() {
Serial.begin(9600);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(IR_PIN, INPUT);
}
void loop() {
// making the ir input a variable
int input = analogRead(IR_PIN);
// if sensor value mini than "MINI" for 1 second Motor turns on, what code is required to complete the function
if (input > MINI)
digitalWrite(MOTOR_PIN, HIGH);
delay(01s); //motor should be turned on for 1 second
digitalWrite(MOTOR_PIN, LOW); //motor should be turned off after 1 second
delay(3s); //3 seconds cooldown before the system can be activated again
//the motor should be off when input value is less than mini
else if (input < MINI)
digitalWrite(LED_PIN, LOW);
@prik0307
Why do you start second thread about the same problem? Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help.
I am making a touchless sanitiser dispenser using arduino nano, I am very new to arduino coding. I am using ir sensor, nano and water motor. If my ir sensor get threshold of higher than 500(in analog 1023 system) for 500 miliseconds than the motor turns on for 500 miliseconds and turns off. There will be a 3 second or 3000 milisecond cooldown between each pump. This is the code I have written here and there is an error in the else if command on line 28. Can somebody help
const int IR_PIN = A1;
const int MOTOR_PIN = 10;
const int MINI = 500; //minimum input received by sensor to send output in 1023 system
void setup()
{
Serial.begin(9600);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(IR_PIN, INPUT);
}
void loop()
{
int input = analogRead(IR_PIN); // making the ir input a variable
// if sensor value mini than "MINI" for 1 second Motor turns on
if (input > MINI);
{
delay(500);
if (input > MINI);
{
digitalWrite(MOTOR_PIN, HIGH);
delay(500); //motor should be turned on for 0.5 seconds
digitalWrite(MOTOR_PIN, LOW); //motor should be turned off
delay(2000); // 2 seconds cooldown before the system can be activated again
}
else if(input > MINI); //the motor should be off when input value is less than mini
{
delay(500);
if (input < MINI);
{
digitalWrite(MOTOR_PIN, LOW);
}
}
}
}
Your suggestion worked, ty so much
Just wanted to ask, will my code work for my sanitizer dispenser because I lack the ability to test it currently and I don't trust my code
void loop()
{
int input = analogRead(IR_PIN); // making the ir input a variable
Serial.print("input = ");
Serial.println(input);
// if sensor value mini than "MINI" for 1 second Motor turns on
if (input > MINI)
{
Serial.println("input > MINI - waiting 500mS");
delay(500);