Friday, December 12, 2014

Basic Resistor Sensor Reading on Raspberry Pi

This is my implementation of the adafruit tutorial Basic Resistor Sensor Reading on Raspberry Pi B+

My  python program implement a sort of calibration of the light censor defined in a clean way,
#!/usr/bin/env python
import RPi.GPIO as GPIO, time, os
def RCtime (RCpin):
    reading = 0
    GPIO.setup(RCpin, GPIO.OUT)
    GPIO.output(RCpin, GPIO.LOW)
    GPIO.setup(RCpin, GPIO.IN)
    # This takes about 1 millisecond per loop cycle
    while (GPIO.input(RCpin) == GPIO.LOW):
        reading += 1
    return reading

class LightRange(object):

    def __init__(self, start, type):
        self.Start = start
        self.Type  = type

    def InRange(self, val):
        return val <= self.Start

    def __str__(self):
        return "[%d]%s" % (self.Start, self.Type)

    def __repr__(self):
        return self.__str__()        

class LightDetector(object):
    def __init__(self):
        self._ranges = []
        # Value must be add from small to large
        self._ranges.append(LightRange(   50,  "A lot of light"))
        self._ranges.append(LightRange(  500,  "Some light"))
        self._ranges.append(LightRange( 3500,  "Very little light"))
        self._ranges.append(LightRange(100000, "No light"))

    def GetLightType(self, val):
        for r in self._ranges:
            if r.InRange(val):
                return "%s - %d" % (r.Type, val)
        return "Unknown value:%d" % (val)

def demo2():
    ld = LightDetector()
    while True:                                     
        lightType = ld.GetLightType(RCtime(18))
        print lightType


No comments:

Post a Comment