Hi, (I am new to both this forum and the Arudino, please forgive me for any silly mistakes)
I have been looking around for a solution to my project, but seems like there isn t one that exactly fits my need. Hopefully I m not asking something that's already been asked.
Anyways, I am doing a project that involves a toy part (that is connected to a relay), a inferred motion sensor, and 2 LEDs.
Basically, what the toy does, is that when the "Press Me"-button (which is hacked and now connects to a relay) is pressed/triggered, the toy will run for about 12s, and then stop.
On the Arduino side, I am putting (1) Two LEDs, which blink in different intervals; (2) The relay mentioned above; and (3) The Inferred sensor, which is the main trigger to both (1) and (2).
The thing is, because I have to initially set the sensor in to read the environment, I cannot int a timer when Arduino starts up.
So I am wondering, is there a way I could put a timer only in the loop of the LED blinks, so they would blink for only a certain period of time when the motion sensor is triggered, so when the blinking and the toy stops running, motion sensor would come on again, and read the environment again, and do the whole loop again only when there is motion around?
I hope that make sense!!
And here's my code, and apparently my version of Cutout does not work at all.
const int relay = 2; //int const for relay in pin 13
const byte led_1 = 11; //int const for led1 in pin 11
const byte led_2 = 12; //int const for led2 in pin 12
//Time periods of blinks in milliseconds
const unsigned long led_1_interval = 133UL;
const unsigned long led_2_interval = 78UL;
int msensor = A0; //motion sensor in input A0
int mspower = 10; //motion sensor power at pin 10
int Cutout = 15000; //Define cutout time
unsigned long led_1_timer = 0; //timer for each led
unsigned long led_2_timer = 0; //blink without delay
unsigned long startTime = 0;
int led_1_state = LOW;
int led_2_state = LOW;
void setup () {
Serial.begin(9600);
pinMode(relay, OUTPUT);
pinMode(led_1, OUTPUT);
pinMode(led_2, OUTPUT);
pinMode(mspower, OUTPUT);
pinMode(msensor, INPUT);
led_1_timer = millis (); //blink without delay timer
led_2_timer = millis (); //blink without delay timer
}
void loop () {
int startTime = millis ();
digitalWrite (led_1, LOW);
digitalWrite (led_2, LOW);
digitalWrite (mspower, HIGH); //turning on motion sensor
delay(5000); //stabalizing motion sensor
int motion = digitalRead(msensor);
if (motion == HIGH) {
digitalWrite (mspower, LOW);
if ( millis () - led_1_timer >= led_1_interval ) {
if (led_1_state == LOW)
led_1_state = HIGH;
else
led_1_state = LOW;
digitalWrite (led_1, led_1_state);
led_1_timer = millis ();
}
if ( millis () - led_2_timer >= led_2_interval ) {
if (led_2_state == LOW)
led_2_state = HIGH;
else
led_2_state = LOW;
digitalWrite (led_2, led_2_state);
led_2_timer = millis ();
}
digitalWrite (relay, HIGH); //running the toy
delay (78);
digitalWrite (reply, LOW);
//turn loop off after 15s
if(millis()-startTime > Cutout){
digitalWrite (led_1, LOW);
digitalWrite (led_2, LOW);
digitalWrite (relay, LOW);
startTime = millis ();
}
}
}
And this is not working:
int startTime = millis (); //in loop
if(millis()-startTime > Cutout){
digitalWrite (led_1, LOW);
digitalWrite (led_2, LOW);
digitalWrite (relay, LOW);
startTime = millis ();
}