Skip to content

Instantly share code, notes, and snippets.

@pauly7300
Last active January 1, 2021 22:36
Show Gist options
  • Save pauly7300/70cc5291bbb0b3b3891f72943321d46d to your computer and use it in GitHub Desktop.
Save pauly7300/70cc5291bbb0b3b3891f72943321d46d to your computer and use it in GitHub Desktop.
Fade led up and down while pressing button
output:
- platform: ledc
pin: 16
frequency: 40000Hz
bit_depth: 10
id: LED_gpio_16
light:
- platform: monochromatic
name: "printer_led_light"
default_transition_length: .1s
id: printer_led
output: LED_gpio_16
globals:
- id: my_global
type: bool
restore_value: no
initial_value: 'true'
binary_sensor:
- platform: gpio
pin:
number: 22
mode: INPUT_PULLUP
inverted: True
id: button1
name: button1
retain: false
on_press:
then:
- if:
condition:
# lambda to ensure the global variable is returning true initially. Just for debug
lambda: |-
ESP_LOGD("paul", "Global value is: %d", id(my_global));
return id(my_global);
# When above condition evaluates to true - Fade_up function else Fade_down
then:
# Fade_up function
- while:
condition:
binary_sensor.is_on: button1
then:
- light.turn_on:
id: printer_led
brightness: !lambda 'return id(printer_led).get_current_values().get_brightness() + 0.05;'
- delay: 0.05s
- logger.log: "executing up"
- lambda: |-
id(my_global) = (false);
# flips the value of the global variable once the button is released
else:
# Fade_down function
- while:
condition:
binary_sensor.is_on: button1
then:
- light.turn_on:
id: printer_led
brightness: !lambda 'return (id(printer_led).get_current_values().get_brightness() == 0.1) ? 0.1 : id(printer_led).get_current_values().get_brightness() - 0.05;'
# brightness: !lambda 'return id(printer_led).get_current_values().get_brightness() - 0.05;'
- delay: 0.05s
- logger.log: "executing down"
- lambda: |-
id(my_global) = (true);
# flips the value of the global variable once the button is released
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment