I'm new to C programming, so I have a rather noob question. The way I understand it, whatever code you enter into the void loop section just keeps repeating itself. Well what if I want it to run once and stop? Say I've got a PIR sensor as input and when it senses movement the arduino fades a LED strip on. Instead of the LEDs turning off and coming back on I want the light to just stay on until the PIR goes back low. Which is set at 1;45 secs.
Check out the strategy employed in the state change example. You have to keep track of what the state of your pir was last time through loop so that you only run code when it is triggered this time but wasn't last time.
The way I understand it, whatever code you enter into the void loop section just keeps repeating itself.
Technically, loop() is called over and over. So, it appears as though the code "keeps repeating itself".
Well what if I want it to run once and stop?
Use an if statement and a static or global variable.
void loop()
{
static bool beenThereDoneThat = false;
if(!beenThereDoneThat)
{
goThereDoIt();
beenThereDoneThat = true;
}
// Do the loopy stuff
}
Which is set at 1;45 secs.
Unless there continues to be movement.
You really don't need a beenThereDoneThat variable or block of code. What you need is to study the state change detection example. You want to diddle with the LEDs when the PIR state changes to "motion detected".