Hi Guys & gels,
I'm doing a simple project, its a parking sensor that gives a warning when to close to a wall. this is being situated out side and i already have a waterproof sensor which is the MY007Y from ebay.
I already have constructed project but i'm stuck on a bit of code that will able to turn the led's off and come back on when something gets in the way of the sensor.
I've searched google and nearly everyone i've looked at includes using a button which i don't want to use. I'm using the attached code on a attiny85 and it works quite well, I just want to be able to turn the led's off after 30seconds or so. I've tried using the wdt and iterrupts for the attiny but the seem to just hang and do nothing.
Can anyone help or put me in the right direction to solve my little issue.
regards bill
You just need to set a timing sequence when the LED's are lit that will turn them off after your chosen interval.
unsigned long startTime = 0;
const long interval = 1000; // 1 second = 1000 millis
bool timeFlag = false;
void setup()
{
}
void loop()
{
if (something you want to Time)
{
timeFlag = true;
startTime = millis(); // Begin timing sequence
// do stuff here when timing sequence starts
}
unsigned long currentTime = millis();
if (timeFlag == true && currentTime - startTime >= interval) // End timing sequence
{
timeFlag = false;
// do stuff here when time is up
}
}
Thanks Hutkikz for your reply and help....I'm a newbie and i wouldn't no where to start putting that in to my code... I would like my program to keep running on the sensor but i just want to turn off the 3 led's when nothings detected or no movement. then when we park on the drive it pics up the car and lights the first led.... sorry for being thick but I'm trying to learn.
Regards Bill
Here I changed the comment's etc. to more directly apply to your situation.
unsigned long startTime = 0;
const long interval = 1000; // 1 second = 1000 millis
bool LedIsOnFlag = false;
void setup()
{
}
void loop()
{
if (condition to turn on LED)
{
LedIsOnFlag = true;
startTime = millis(); // Begin timing sequence by recording the time
// turn your LED on
}
unsigned long currentTime = millis(); // record current time
if (LedIsOnFlag == true && currentTime - startTime >= interval) // End timing sequence by subtracting the start time from the current time
{
LedIsOnFlag = false;
// turn your LED off
}
}
sorry Hutkikz for not getting back in touch, I've not tried it yet because my father inlaw got taken in to hospital, so I've not had chance to look at it properly.