Blue tooth controlled Timer

Hello Guys,

I am trying to build an application where i am able to control the software timer via a bluetooth, To test that i created a small sample program. I was going to try two different periods based on if else statement in the loop. But i do not see any out put? What am i doing wrong?


void print_every_second()
{
    static int count = 0;
    Serial.println(count++);
}

Timer myTimer(1000, print_every_second);

void setup()
{
    Serial.begin(9600);
   
}

void loop(){
    
    if(true){
      myTimer.changePeriod(10000);
      myTimer.reset();
      myTimer.start();
        
    }else{
        
      myTimer.changePeriod(1000);
      myTimer.reset();
      myTimer.start();
        
    }
    
    
}

Simply, it is your loop() logic structure.

You are repeatedly resetting and starting your timer so that it never ends which would be when you will trigger the calling of print_every_second() and some output.

I suggest you look again at the reference documentation for software timers.

To fix your example put myTimer.start(); in setup();

Put nothing in loop(); to begin with.

After that you could try testing for myTimer.isActive() and do something else.

2 Likes

Hey,

The reason why i put everything in the loop was because, i wanted to continuously poll the status of the bluetooth , and when it changed i would change the frequency of the timer. My understanding of the setup loop was that it is called only once. Unless i am wrong? If i have my entire logic inside the setup, would i be able to change the timer based on the bluetooth?

There is no “setup loop”, only setup(), which is called once on reset and loop(), which is called endlessly after setup() is run. @armor is correct in his comments and suggestions. Your loop() code does not emulate a bluetooth connection in that it repeats the commands over and over again. In a bluetooth scenario, you would receive a command, parse it and act upon it ONCE by setting a flag that you can test. In your code, if (true) is ALWAYS true so that code will ALWAYS run over and over again, again as @armor pointed out. You could simulate a bluetooth “event” with a Particle.function() to receive a “command” and set a flag which then get acted on in your loop() code ONCE.

1 Like

Thanks @peekay123 and @armor, was able to figure out what you guys were saying.

void print_every_second()
{
    static int count = 0;
    Serial.println(count++);
}

Timer myTimer(1000, print_every_second);
bool flag;

void setup()
{
    Serial.begin(9600);
flag = true;
    
}

void loop(){

    
    if(flag){
      myTimer.changePeriod(10000);
      myTimer.reset();
      myTimer.start();
        flag = false;
    }
    
    
}

I basically want to control the frequency of a sprinkler that i have. So, i can have it turn on at intervals of minutes or hours. For that purpose, is using timers a good approach? Or is there something better that i can use?

@jrjack, just one note. You don’t need to call myTimer.reset() if your are calling myTimer.start():

start()
Starts a stopped timer (a newly created timer is stopped). If start() is called for a running timer, it will be reset.

I would suggest that unless the accuracy of your sprinkler needs to be starting and stopping within a millisecond you don’t need to consider timers. Instead, if the control is a diurnal pattern (daily) you look at local seconds e.g. 0 at 00:00 am and 86399 at 23:59:59. Or you could find a library that does such scheduled on / off.

2 Likes

@peekay123 Hey!

Everything seems to be working fine, however i have realized that if i have set a timer via bluetooth and my connection resets, the state of the bluetooth resets and the timer stops working. How can i avoid this? One idea that i had was to save the status of the bluetooth signal in EEPROM? Does doing that make sense or is there a better approach to do this?

Not sure where the connection between bluetooth and your reset comes from since I can’t see any reference to bluetooth in any of the codes you submitted above.

How/Why would that reset occure?
How is your device powered?
If there is no power loss expected, just using retained variables should be enough.
If you also need to cater for power loss EEPROM is probably the way to go (unless you add another kind of non-volatile storage - e.g. SD, FRAM, …)