Hi everybody,
is there a place somewhere on Arduino.cc with functionality-examples?
Of course there is the reference that explains the syntax how to use single functions and words of the programming-language.
For beginner it would be very useful if there would be a collection of what I call "functionalities"
I was inspired to this question an example-code posted by horace for a certain "functionality" that I want to use right here to explain what I mean. It is an example about non-blocking timing for "switching off" something after a certain amount of time. First as a very concrete examples using IO-pins then in a more generalised way to make newbees see how they can adapt this functionality to their own needs.
Switch on something depending on a condition becoming true.
If condition changes back to state unset keep switch ON for a certain amount of time then switch OFF
this example uses an IO-input named "SWITCH" to switch on and delayed switching off an IO-pin named "FAN"
loop() {
static unsigned long MillisSnapShot = 0;
const unsigned long WaitingTime = 5000;
if(digitalRead(SWITCH)) // switch pressed ?
{
MillisSnapShot = millis(); // start timing
digitalWrite(FAN, HIGH); // fan ON
}
else if((millis() - MillisSnapShot) > WaitingTime) // delay elapsed?
{
digitalWrite(FAN, LOW); // fan OFF
}
// do other things at high speed in "parallel" to the timing
} // end of loop()
written as code-template in a more general way where variable "flag" can be any kind of boolean expression.
As soon as the boolean expression becomes true the action "switch ON" will be executed
As a sideeffect if the boolean expression stays true for some time the timer-variable MillisSnapShot gets updated all the time
which has the effect that switching off will always happen only after boolean-expression becomes false
AND the additional waiting-time os over
example with numbers:
WaitingTime 20 seconds
10:00:00 boolean expression becomes true => switch ON
10:00:01 boolean expression becomes false => WatingTime starts
10:00:21 switching off
WaitingTime 20 seconds
10:00:00 boolean expression becomes true => switch ON
10:00:01 boolean expression still true
10:00:02 boolean expression still true
10:37:40 boolean expression still true
......
11:45:05 boolean expression becomes false => WatingTime starts
11:45:25 switching off
examples for boolean expressions that replace the variable "flag"
( digitalRead(MyIO_Pin == HIGH) )
( digitalRead(MyIO_Pin == LOW) )
( AnalogRead(MyIO_Pin > 100) )
( AnalogRead(MyIO_Pin < 200) )
( MyBooleanVariabe == true)
( MyBooleanVariabe == false)
loop() {
static unsigned long MillisSnapShot = 0;
const unsigned long WaitingTime = 5000;
if(flag)
{
MillisSnapShot = millis(); // start timing
// switch "ON"
}
else if((millis() - MillisSnapShot) > WaitingTime) // delay elapsed?
{
// switch "OFF"
}
// do other things = execute code at high speed in "parallel" to the timing
} // end of loop()
So my question is:
is there a place where such code-snippets are collected so newbees can look-up these code-snippets for questions like How do I ......
best regards Stefan