I have interfaced a dc motor and IR sensor to my nano. The DC motor is supposed to run only if the IR sensor detects an obstacle. Now, I'm trying to get the motor to turn off if continuously obstructed for 3 seconds, and exit the interrupt (in case of the first code with interrupt), and again wait for the sensor to go low again (falling edge). I have implemented this using millis(), but the code doesnt seem to be working consistently, as the timing seems to have gone for a toss(random behavior; sometimes it keeps running continuously and sometimes it stops). Can anyone on this forum please review it and suggest me a correction. Also, would it be possible to use a LOW trigger instead of falling edge to achieve this ?
#include <LowPower.h>
int out1 = 5;
int IRSensor = 3;
int LED = 6;
int Switch = 2;
volatile int statusSensor=1;
volatile int switchSensor=1;
unsigned long delayStart = 0;
boolean motorRunning = false;
static unsigned long motorStartMillis;
static unsigned long motorRunMillis = 3000;
static unsigned long motordiff;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(out1,OUTPUT);
//pinMode(out1,OUTPUT);
pinMode (IRSensor, INPUT); // sensor pin INPUT
pinMode (LED, OUTPUT); // Led pin OUTPUT
pinMode(Switch, INPUT_PULLUP);
attachInterrupt(1,IRinterrupt,FALLING);
//attachInterrupt(1,Switchinterrupt,FALLING);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println ("zloop");
//Interrupts();
LowPower.powerDown(SLEEP_FOREVER , ADC_OFF, BOD_OFF);
//Serial.println ("loop 1");
}
void IRinterrupt()
{
// put your main code here, to run repeatedly:
switchSensor = digitalRead (Switch);
//Serial.println (switchSensor);
statusSensor = digitalRead (IRSensor);
//Serial.println (statusSensor);
Serial.println ("ir interupt");
if (statusSensor == 0 and switchSensor == 0){
Serial.println ("ir low");
digitalWrite(LED, LOW);
digitalWrite(out1,HIGH);
if (motorRunning == false){
motorStartMillis = millis();
Serial.println (motorStartMillis);}
motorRunning = true;
}
else if (statusSensor == 0 and switchSensor == 1) {
Serial.println ("ir high");
//digitalWrite(out1,LOW);
digitalWrite(LED, HIGH);
analogWrite(out1,0);
}
else if (statusSensor == 1 and switchSensor == 0) {
Serial.println ("ir high");
//digitalWrite(out1,LOW);
digitalWrite(LED, LOW);
analogWrite(out1,0);
}
else {
Serial.println ("ir high");
//digitalWrite(out1,LOW);
digitalWrite(LED, HIGH);
analogWrite(out1,0);
}
motordiff=millis()-motorStartMillis;
Serial.println(motordiff);
if (motorRunning == true and (motordiff >= motorRunMillis)){
digitalWrite(out1,LOW);
Serial.println ("motor timing");
motorRunning = false;
//noInterrupts();
}
}
I have written another piece of code without interrupts. How do I achieve the same timing part for this too ?
int out1 = 5; //motor
int IRSensor = 3; // connect ir sensor to arduino pin 6
int LED = 6; // conect Led to arduino pin 9
int Switch = 2;
int statusSensor=1;
int switchSensor=1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(out1,OUTPUT);
//pinMode(out1,OUTPUT);
pinMode (IRSensor, INPUT); // sensor pin INPUT
pinMode (LED, OUTPUT); // Led pin OUTPUT
pinMode(Switch, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
switchSensor = digitalRead (Switch);
//Serial.println (switchSensor);
statusSensor = digitalRead (IRSensor);
//Serial.println (statusSensor);
delayStart = millis();
//Serial.println ("ir interupt");
if (statusSensor == 0 and switchSensor == 0){
Serial.println ("ir low");
analogWrite(out1,120);
digitalWrite(LED, LOW);
}
else {
Serial.println ("ir high");
analogWrite(out1,0);
}
}