Micro Dot pHat - Brightness Control

Hi Guys - How can I adjust the brightness of the matrix, I’ve looked at the fading text example and I can’t get my code to work?

from microdotphat import set_brightness, write_string, scroll, show, clear, set_decimal

  if BRIGHTNESS < MAX_BRIGHT:
    BRIGHTNESS = int(BRIGHTNESS + 10)
    set_brightness(BRIGHTNESS)
    message = "Br Up"
    clear()
    show_matrix(message)
    time.sleep(1)

even when I set a low (=5) brightness at the begining of my code it never changes at all ? The value is increasing as you would expect but has no effect - am I setting the value correctly?

What you’re doing looks right, although the uppercase format is generally used to denote constants in Python and not variables that changed.

Could you post your complete code?

Here we go, I’ve had another play and still can’t get it to adjust the brightness, for the life of me I can’t see what I’m doing wrong, basically I have a up/down buttons to adjust the brightness in this example - I know when it hits min/max it errors currently but this should impact the brightness setting ?:

  #!/usr/bin/env python
  import RPi.GPIO as GPIO #interacting with the GPIO  
  import time as time # keeping track of time
  from microdotphat import set_brightness, write_string, scroll, show, clear
  import threading
  import Queue 
  from threading import Thread  

 #========== set up statics ==========================================================
  global sw1_prev
  global sw2_next
  sw1_prev = False
  sw2_next = False
 #---------- GPIO SETUP---------------------------------------------------------------
 #Define GPIO Numbering Schema
 GPIO.setmode(GPIO.BCM)
 #Define individual GPIO Pins
 PIN_SW1_PREV = 20
 PIN_SW2_NEXT = 21
 #------------------------------------------------------------------------------------
def prev_callback(channel):
global sw1_prev
sw1_prev = True
def next_callback(channel):
global sw2_next
sw2_next = True
  #-------init Matrix------------------------------------------------------------------
  global max_bright
  global min_bright
  global cur_bright
  max_bright = 125
  min_bright = 5
  cur_bright = 100
  set_brightness(cur_bright)
  #------------------------------------------------------------------------------------
  GPIO.setup(PIN_SW1_PREV, GPIO.IN, pull_up_down=GPIO.PUD_UP)    # set 20 as input
  GPIO.setup(PIN_SW2_NEXT, GPIO.IN, pull_up_down=GPIO.PUD_UP)   # set 21 as input
  GPIO.add_event_detect(PIN_SW1_PREV, GPIO.FALLING, callback=prev_callback, bouncetime=300)
  GPIO.add_event_detect(PIN_SW2_NEXT, GPIO.FALLING, callback=next_callback, bouncetime=300)
  #===================================================================================
  #do stuff endlessly until ctrl+c etc
  #======
 def main():
  while True:
global sw1_prev
global sw2_next
global max_bright
global min_bright
global cur_bright
press = read_switches()

  # PREV button pressed
if(press == 1):
  print "Down"
  message = "Down"
  if cur_bright > min_bright:
    cur_bright = cur_bright -10
    set_brightness(cur_bright)
    clear()
    write_string(message, kerning = False)
    show()
    time.sleep(1)
    sw1_prev = False
  else:
    message = "Min!"
    write_string(message, kerning = False)
    clear()
    show()
    time.sleep(1)
    sw1_prev = False

  # NEXT button pressed
if(press == 2):
  print "Up"
  message = "Up"
  if cur_bright < max_bright:
    cur_bright = cur_bright +10
    set_brightness(cur_bright)
    clear()
    write_string(message, kerning = False)
    show()
    time.sleep(1)
    sw2_next = False
  else:
    message = "Max!"
    write_string(message, kerning = False)
    clear()
    show()
    time.sleep(1)
    sw2_next = False

  # BOTH buttons pressed together
if(press == 3):
  print "TBD"
  sw2_next = False
  delay_milliseconds(100)

  #==================================================================================
  def read_switches():
  # Debounce switches and look for two-button combo
  global sw1_prev
  global sw2_next
  delay_milliseconds(1)		
  if(sw1_prev and sw2_next):
    return 3
  if(sw2_next):
    return 2
   if(sw1_prev):
    return 1
return 0

def delay_milliseconds(milliseconds):
  seconds = milliseconds / float(1000)	# divide milliseconds by 1000 for seconds
  time.sleep(seconds)

  #=====
  if __name__ == "__main__":
    main()

… and in addition at a basic level the following code doesn’t set the brightness ?

   #!/usr/bin/python

   from microdotphat import set_brightness, write_string, scroll, show, clear, set_decimal
   from time import sleep

   # I would have thought a value of 1 would be very dim?
   set_brightness(1)

   while True:
    global message
    message = "Hello"
    clear()
    write_string(message, kerning=False)
    show()
    sleep(0.05)

If I add a value of say 500 it doesn’t error, so is it actually setting the value in my code ? Thanks for looking ! :)

Bump - Anyone got any ideas where I’m going wrong ?

I’m pretty sure brightness is intended to be a float between 0 and 1. I had a look at the lib and the boundaries are not enforced but the brightness parameter is multiplied by 127.

Anyhow, I’ll talk to @gadgetoid tomorrow but in the meantime it will work if you pass a value of 0.1 to dim the display, and I don’t suspect the next version of the lib will break your code if you assume 0 < brightness < 1

1 Like

yep that works - thanks lots, updated code that works:

 #!/usr/bin/python

  from microdotphat import set_brightness, write_string, scroll, show, clear, set_decimal
  from time import sleep

  # Value from 0.1 -> 1.0
  set_brightness(0.1)

  while True:
  global message
  message = "Hello"
  clear()
  write_string(message, kerning=False)
  show()
  sleep(0.05)