0
def blue():
    global blue_old
    global blue_new
    while blue_old > blue_new:
        blue_old = blue_old - 0.01
        print blue_old
        os.system("echo 21=" + str(blue_old) +  " > /dev/pi-blaster")    
        time.sleep(0.01)

    while blue_old < blue_new:
        blue_old = blue_old + 0.01
        os.system("echo 21=" + str(blue_old) +  " > /dev/pi-blaster")      
        time.sleep(0.01)

    print "blue done: " + str(blue_old)   

Hi, using threading method in Python I have this code to control intensity of RGB LED strip via RPi. Basically when new value is higher than current, it adds value 0.01 in loop until new and old values are equal, to have a nice brightening effect. If new value is lower than current it does exact opposite, decreasing value in 0.01 steps, but if I want to complete shut it down(value = 0) I get this output:

0.1
0.09
0.08
0.07
0.06
0.05
0.04
0.03
0.02
0.01
-7.52869988574e-16

Why am I getting this strange negative number instead of 0?

You can run this if you want to test:

import os
import time
import threading

blue_old = float(1)
blue_new = float(0)

def blue():
    global blue_old
    global blue_new
    while blue_old > blue_new:
        blue_old = blue_old - 0.01
        print blue_old    
        time.sleep(0.01)

    while blue_old < blue_new:
        blue_old = blue_old + 0.01
        print blue_old   
        time.sleep(0.01)

    print "blue done: " + str(blue_old)

blue = threading.Thread(target=blue).start() 

Thanks.

user3570799
  • 81
  • 1
  • 4

0 Answers0