I am making a function with multiple results and in this function a need a timer.
I can't get it to work so I stipt my function to just a blinker. Even this I can't get it to work
Can someone help me?
With regards,
Edwin
******************************************************************************************
#define PinLed 6
bool xBlinkOn;
void Test();
void setup() {
pinMode(PinLed, OUTPUT);
}
void loop() {
Test(millis(), &xBlinkOn);
digitalWrite(PinLed, xBlinkOn);
}
void Test(unsigned long time , bool* xOutput){
unsigned long timeBlink;
bool xInit;
if (xInit== 0)
{
timeBlink = time;
xInit = 1;
}
if ( time - timeBlink >= 500)
{
*xOutput = !*xOutput;
timeBlink = time;
}
} //Test
LarryD
March 15, 2024, 7:39pm
2
You should initialize local variables.
bool xInit;
if (xInit== 0)
LarryD
March 15, 2024, 7:43pm
3
Do you understand how this TIMER snippet works ?
//********************************************** h e a r t b e a t T I M E R
//is it time to toggle the heartbeatLED ?
if (millis() - heartbeatTime >= 500ul)
{
//restart this TIMER
heartbeatTime = millis();
//toggle LED
digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
}
gcjr
March 15, 2024, 7:48pm
5
won't this turn the LED off (or on) immediately after it is turned on (or off)
try this
const byte PinLed = LED_BUILTIN;
bool state;
unsigned long msec0;
void loop ()
{
unsigned long msec = millis ();
if (msec - msec0 >= 500) {
msec0 = msec;
state = !state;
digitalWrite (PinLed, state);
}
}
void setup ()
{
pinMode (PinLed, OUTPUT);
}
rainman:
#define PinLed 6
bool xBlinkOn;
void Test();
void setup() {
pinMode(PinLed, OUTPUT);
}
void loop() {
Test(millis(), &xBlinkOn);
digitalWrite(PinLed, xBlinkOn);
}
void Test(unsigned long time , bool* xOutput){
unsigned long timeBlink;
bool xInit;
if (xInit== 0)
{
timeBlink = time;
xInit = 1;
}
if ( time - timeBlink >= 500)
{
*xOutput = !*xOutput;
timeBlink = time;
}
} //Test
Use 'static' variable to initialize and preserve its value through separate function calls.
#define PinLed 21
bool xBlinkOn;
void Test();
void setup() {
Serial.begin (115200);
pinMode(PinLed, OUTPUT | INPUT);
xBlinkOn = digitalRead(PinLed); // or 0 or 1
}
void loop() {
Test(millis(), &xBlinkOn);
digitalWrite(PinLed, xBlinkOn);
}
void Test(unsigned long time , bool* xOutput){
static unsigned long lastTimeBlink = 0;
if ( time - lastTimeBlink >= 500)
{
*xOutput = !*xOutput;
Serial.println (*xOutput);
lastTimeBlink = time;
}
} //Test
bojan_jurca:
Use 'static' variable
Yes. But @rainman has used a pointer to bool in the parameter list for the function, as if she intends to use the function one day for multiple blinking <whatevers >, which won't work because
void Test(unsigned long time , bool* xOutput){
static unsigned long lastTimeBlink = 0;
There is but one place to store the time of the last blink.
Or maybe that's what is desired, seems odd.
A solution would be to add a parameter for an unsigned long call by reference variable that would play a roll unique to the bool being time toggled.
a7
Of course. I just wanted to simplify the code a little. There is absolutely no reason why xIniti could not be static as well.
#define PinLed 21
bool xBlinkOn;
void Test();
void setup() {
Serial.begin (115200);
pinMode(PinLed, OUTPUT | INPUT);
xBlinkOn = digitalRead(PinLed); // or 0 or 1
}
void loop() {
Test(millis(), &xBlinkOn);
digitalWrite(PinLed, xBlinkOn);
}
void Test(unsigned long time , bool* xOutput){
static unsigned long lastTimeBlink = 0;
static bool xInit = false;
if (xInit == false)
{
lastTimeBlink = time;
xInit = true;
}
if ( time - lastTimeBlink >= 500)
{
*xOutput = !*xOutput;
Serial.println (*xOutput);
lastTimeBlink = time;
}
} //Test
I'm not quite sure if this is what your question was.
Here you don't need to say false , static variables get initialised.
But this
bool xInit;
if (xInit== 0)
{
is very asking for trouble. It invokes undefined behaviour, which goes way beyond what you might think, that the variable would necessarily have one, or the other, value.
How I know - learned the hard way
Please enjoy a war story and have a laugh on me.
The error in the code below kept me busier for longer than I will ever say.
I have solved the problem. Many of you will solve it just by reading it, others will find the compiler warning useful.
TBC I have the compiler warnings cranked up to the max. For some reason, I got no warning from the full code. Only late in the game of boiling this down for your consideration did the compiler start warning me. Of how I had gone wrong all by myself.
Th…
a7
system
Closed
September 13, 2024, 12:28pm
10
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.