Checking for Button Presses and Changing the Blinking Rate, both using Non-blocking Code

Hey everyone, I’m new to the Photon and of course, unfamiliar with many of its functionalites.

As the above tittle suggests, I’m trying to create a code for detecting button presses and changing the blinking rate without using any blocking code, but the program I came up with is not working. I’m hoping if you guys could spot any logical errors in my code. I suspect that is the problem, and a simple one too, it’s just I can’t find it out myself.

Here is my code:

int led = D7;
int button = D3; // This is the input button (use an internal pull-down resistor)

int LedFreqState = 0;   // set to 0 for 0.50 Hz, set to 1 for 0.25 Hz
int prevButton = LOW;   // state variable to store if button was HIGH or LOW last time


bool LED_state = FALSE;

unsigned long int timeToToggleLED = 0;


void setup() {
     pinMode(led, OUTPUT);
     digitalWrite(led, LOW);
      Serial.begin(9600);               // Use Serial port for debugging

}

void loop() {
    
    unsigned long int currentTime = millis(); 
    
    int curButton = digitalRead(button);
    
    
     if(curButton == HIGH && prevButton == LOW){
        // a transition happened -- the user has just pressed the button
        Serial.println("Button press detected.");  //Debugging printout
        
        // change the state of the LED blinking rate
        LedFreqState++;
        if(LedFreqState > 2){  
            LedFreqState = 0;
            
        }
        
        prevButton = curButton;
        
       
    Serial.println("LedFreqState is: ");
    Serial.println(LedFreqState);
     }
    
    if(LedFreqState == 0)
    {
        if(currentTime > timeToToggleLED){  
        // Time to Toggle!
        LED_state = !LED_state;
        digitalWrite(led, LED_state);

        // Calculate and store the next time to toggle the LED
        timeToToggleLED += 1000;
        }

    }
    
    else if(LedFreqState == 1)
    {
        if(currentTime > timeToToggleLED){  
        // Time to Toggle!
        LED_state = !LED_state;
        digitalWrite(led, LED_state);

        // Calculate and store the next time to toggle the LED
        timeToToggleLED += 2000;
        }
    
    }
    
    else if (LedFreqState == 2)
    {
        if(currentTime > timeToToggleLED){  
        // Time to Toggle!
        LED_state = !LED_state;
        digitalWrite(led, LED_state);

        // Calculate and store the next time to toggle the LED
        timeToToggleLED += 500;
        }
    
    }
    
     
}

I’m trying to create 3 different blinking rates, which are 0.25Hz, 0.5Hz and 1Hz.

Can anyone help me out here?

Thanks so much!

You should set the pin mode of button in the setup process so the Photon would know that you want D3 to be an input. So in your setup() function, you should do something like:

pinMode(button, INPUT_PULLDOWN);

I re-wrote your code, quick and dirty. I don’t see the value in writing your own timer when you can use the built in timer functions. Take a look and modify it as you see fit; it compiles, but i won’t promise that it runs 100% correctly.

int led = D7;
int button = D3; // This is the input button (use an internal pull-down resistor)


uint8_t blinkInc = 0; //The incrementer for blinking
#define numFreqs    3
unsigned int LedFreq[numFreqs] = {250, 500, 1000}; //The various frequencies  (in ms)
unsigned int blinkRate = LedFreq[0]; //set the first rate to the first entry
bool ledOn = 0; //

void lightBlink();
Timer blinkTimer(blinkRate, lightBlink);

void setup() {
     pinMode(led, OUTPUT);
     pinMode(button,INPUT);
     digitalWrite(led, LOW);
     Serial.begin(9600);               // Use Serial port for debugging

}

void loop() {
    
  if(digitalRead(button) == HIGH){ //If the button is pressed
        delay(50); //bullshit debounce delay
        blinkInc++;
        blinkRate = LedFreq[(blinkInc % numFreqs)];
        blinkTimer.changePeriod(blinkRate/2);
    }    
     
}

void lightBlink(){
  ledOn = !ledOn; //toggle the LED
  digitalWrite(led,ledOn);  
}

This is not very helpful for people trying to diagnose your problem. You should state what actually does happen with your current code, compared to what you want to happen.