Oi, this website is currently on the move to https://turboflip.de - party on there!

Fun with One Button and One LED

We can do a lot of fun programming exercises with just one Button and one LED – plus there are several issues to take away that occur in every day programmers life 🙂 – First off a quick starting point:

LED Helper class

// ----------------------------------
// -------- BASIC LED HELPER --------
// ----------------------------------

// basic lerp function
float return_lerp(float _s, int _target, int _time) {
  _s = _s + (( float(_target) - _s) / float(_time));
  return _s;

}

// --------------
// LED Object
// -------------
class aLED {
  private:
    byte pin;
    long ts = 0;
    int led_mode = 0; //0 manual / 1:sinpulse / 2:fading

  public:
    int brightness = 0; // current brightness
    float fade_target = 0; // where to fade?
    int fade_speed = 4;
    float pulse_speed = .004; // speed of pulse when in pulsemode
    int pulse_amplitude = 125;

    aLED(byte pin) {
      this->pin = pin;
      init();
    }

    void init() {
      pinMode(pin, OUTPUT);
    }

    void operateLED() {
      if (led_mode == 0) {

        // if in manual mode

      } else if (led_mode == 1) {

        // -----------  PULSE MODE
        brightness =  int((sin(millis() * pulse_speed) + 1) * pulse_amplitude * .5);

      } else if (led_mode == 2) {

        // -----------  FADING MODE
        brightness = int(return_lerp(brightness, fade_target, fade_speed));

      }

      analogWrite(pin, brightness);

    }

    void setMode(int _mode) {
      led_mode = _mode;
    }

};

BUTTON Helper class

// --------------
// Digital Button helper class
// -------------
class aBUTT {
  private:
    byte bpin;
    long ts = 0;
    bool previous_state = false;
    bool previous_state_holded = false;

  public:
    bool is_pressed = false;
    bool is_holded = false;

    bool on_pressed = false;
    bool on_released = false;
    bool on_holded = false;

    aBUTT(byte bpin) {
      this->bpin = bpin;
      init();
    }
    void init() {
      pinMode(bpin, INPUT);


    }


    void operateBUTTON() {

      is_pressed = digitalRead(bpin);


      on_pressed = false;
      on_released = false;

      if (previous_state != is_pressed) {

        if (is_pressed) {

          ts = millis();
          on_pressed = true;

        } else {

          on_released = true;
          is_holded = false;
          on_holded = false;
          previous_state_holded = on_holded;
        }

        previous_state = is_pressed;

      }

      on_holded = false;
      if (ts + 1000 < millis() && is_pressed ) {

        is_holded = true;


        if (previous_state_holded != is_holded && is_pressed ) {

          on_holded = true;
          previous_state_holded = is_holded;
          //Serial.println("holdi");
        }
      }
    }
};

Mainloop

// ----------------------------------
// -------- MAIN LOOP  --------
// ----------------------------------
  
 
aLED led1(11);
aBUTT button1(12);
  
void setup() {
   
   Serial.begin(9600);
}
  
void loop() {
   
  led1.operateLED(); //operate specific LED
  button1.operateBUTTON();

     if(button1.is_pressed){

        led1.setMode(1);
        led1.pulse_speed = .01;
      }else{

        led1.setMode(0);
        led1.brightness = 0;
      }


      if(button1.on_pressed){
        Serial.println("BUTTON DOWN");
       }

      if(button1.on_released){
        Serial.println("BUTTON UP");
       }

       if(button1.on_holded){
          Serial.print(".");
           
       }

      
  delay(10);
}

RECORD AND PLAYBACK Button Input

Here we have a more advanced piece of code to record the button input and play it back afterwards with the LED. Get the code over here:

morse_looper_basic