Nick Rogness
Published © MIT

Chewing Gum Nightlight

Kids colorful nightlight made from a chewing gum container and a circuit playground express

BeginnerWork in progress2 hours440
Chewing Gum Nightlight

Things used in this project

Hardware components

Circuit Playground Express
Adafruit Circuit Playground Express
×1
Ice Breakers Cube Gum dispenser
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
Adafruit POTENTIOMETER KNOB - SOFT TOUCH T18 - WHITE
×1
Pushbutton Switch, Momentary
Pushbutton Switch, Momentary
×1

Story

Read more

Code

main.py

MicroPython
CircuitPython (Adafruit's derivative of Micropython)
import time
import board
import neopixel
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction, Pull

last_br = 0

colors = {'RED': (0xff,0,0),
        'PINK': (0xff, 0x14, 0x93),
        'PURPLE': (0x4b, 0x00, 0x82),
        'BLUE': (0, 0, 0xff),
        'YELLOW': (0xff, 0xff, 0),
        'GREEN': (0, 0xff, 0),
        '1_WHITE': (0xff,0xff,0xff)
        }
        

def getPot(pin):
    return (pin.value * 3.3) / 65536
    
def getBrightness():
    
    global last_br
    
    br = round(getPot(pot)/3.3,3)
    br_tol = .01
    if (br-br_tol) <= last_br <= (br+br_tol):
        br = last_br
    else:
        last_br = br
        
    if br > 1:
        br = 1
    if br < .01:
        br = 0
        
    #print(br)
    return br

pot = AnalogIn(board.A1)
button = DigitalInOut(board.A3)
button.direction=Direction.INPUT
button.pull = Pull.DOWN

colors_list = sorted(list(colors))
colors_list_len = len(colors_list)
current_color_idx = 0

pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0)
pixels.fill((0,0,0))


def do_rainbow_down():
   
    counter = 0
    r = 0
    g = 0
    b = 255
    cstep = 5
    for i in range(0,3):

        for j in range(0,255,cstep):
            pixels.brightness=getBrightness()
            if button.value is True:
                return False
                
            if i == 0:
                g = j
                if j != 0:
                    b -= cstep
                
            elif i == 1:
                r = j
                if j != 0:
                    g -= cstep
                            
            elif i == 2:
                if j != 0:
                    r -= cstep
                    
            pixels.fill((r,g,b))
            pixels.show()
            
    return True
    

def do_rainbow_up():
   
    counter = 0
    r = 0
    g = 0
    b = 0
    cstep = 5
    for i in range(0,3):

        for j in range(0,255,cstep):
            pixels.brightness=getBrightness()
            if button.value is True:
                return False
                
            if i == 0:
                r = j
                
            elif i == 1:
                g = j
                if j != 0:
                    r -= cstep
                
            elif i == 2:
                b = j
                if j != 0:
                    g -= cstep
                            
             
            pixels.fill((r,g,b))
            pixels.show()
            
    return True
    
    
def set_next_color():
    
    global current_color_idx
    
    colors_list_len = len(colors_list)
    if colors_list_len == 0:
        return ((0,0,0))
        
    current_color_idx += 1
    if current_color_idx >= colors_list_len:
        current_color_idx = 0
        
    return colors[colors_list[current_color_idx]]
    

single_color_mode = True
last_color_idx = -1
counter = 100 # 10 seconds
time_delay = .05
loop_counter = 0
rainbow_up = True

while True:
    
    pixels.brightness=getBrightness()
    
    if button.value is True:

        # print('Button Pressed!')
        
        if current_color_idx == colors_list_len-1:
            single_color_mode = False
            print('Entering color loop')
            pixels.fill((0,0,0))
            pixels.show()
            time.sleep(.05)
            pixels.fill((255,255,255))
            pixels.show()
            time.sleep(.05)
            pixels.fill((0,0,0))
            pixels.show()
            time.sleep(.05)
            pixels.fill((255,255,255))
            pixels.show()
            time.sleep(.05)
            
        else:
            if single_color_mode is False:
                single_color_mode = True
                
            set_next_color()    
        
           
    if single_color_mode is False:
        
        if rainbow_up is True:
            did_finish = do_rainbow_up()
            rainbow_up = False
        else:
            did_finish = do_rainbow_down()
            rainbow_up = True
            
        if did_finish is not True:
            set_next_color()
            single_color_mode = True
            
    else:
        rainbow_up = True
                    
            
    if current_color_idx != last_color_idx:
        pixels.fill(colors[colors_list[current_color_idx]])
        pixels.show()
        print(colors_list[current_color_idx],'{:.1f}'.format(pixels.brightness))
        last_color_idx = current_color_idx
        time.sleep(.5)
            
            
    time.sleep(time_delay)

    

Credits

Nick Rogness

Nick Rogness

5 projects • 11 followers

Comments