// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int motorPin = 11;
const int motorPin2 = 12;
const int motorPin3 = 13;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(motorPin, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print("Distance: ");
Serial.println(distance);
delay(100);
if (distance < 50)
{
digitalWrite(motorPin3, HIGH);
delay(100);
digitalWrite(motorPin, LOW);
delay(1000);
digitalWrite(motorPin, HIGH);
digitalWrite(motorPin2, LOW);
delay(1000);
digitalWrite(motorPin2, HIGH);
}else{
digitalWrite(motorPin, LOW);
digitalWrite(motorPin3, LOW);
}
}
#include <TimeLib.h>
#include <TimeAlarms.h>
AlarmId id;
void setup() {
Alarm.timerRepeat(15, Repeats); // timer for every 15 seconds
}
void loop() {
Alarm.delay(1000);
}
void Repeats() {
Serial.println("15 second timer");
}
void Repeats2() {
Serial.println("2 second timer");
}
void OnceOnly() {
Serial.println("This timer only triggers once, stop the 2 second timer");
// use Alarm.free() to disable a timer and recycle its memory.
Alarm.free(id);
// optional, but safest to "forget" the ID after memory recycled
id = dtINVALID_ALARM_ID;
// you can also use Alarm.disable() to turn the timer off, but keep
// it in memory, to turn back on later with Alarm.enable().
}
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();
}
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
Hi,
Im having problems implementing a timer-function in my code. I want my dc motor to start when the sensor reaches <50, . But I also want it to have it running every 15 min. I tried to put a timer function in the code but I am not managing getting everything together. I don't know how to implement the timer function so that it would effect motorPin3, to run permanently ever 15 min for 1 sec. I know it is also a bit of a cut and past work I've done, but I am trying.