adc_ref = 5 def send_to_server(type, value): try: payload = {"sensors": type, "value": value} print payload r = requests.post(URL, data=payload) print r.text except: print("Server Down") while True: try: # Read sensor value from potentiometer sensor_value = bakebit.analogRead(light_sensor) send_to_server("lux", sensor_value) # Calculate voltage voltage = round((float)(sensor_value) * adc_ref / 1023, 2) # Calculate LED brightess (0 to 255) if sensor_value > lowThreshold: brightness = int(((float)(sensor_value) - lowThreshold) / (highTreshold - lowThreshold) * 255) elif sensor_value > highTreshold: brightness = 255 else: brightness = 0 # Give PWM output to LED
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ''' import time import bakebit # Connect the BakeBit Thumb Joystick to analog port A0 # If you wish to connect two joysticks, use ports A0 and A2 (skip A1) # Uses two pins - one for the X axis and one for the Y axis # This configuration means you are using port A0 xPin = 0 yPin = 1 bakebit.pinMode(xPin, "INPUT") bakebit.pinMode(yPin, "INPUT") while True: try: # Get X/Y coordinates x = bakebit.analogRead(xPin) y = bakebit.analogRead(yPin) print("x =", x, " y =", y) time.sleep(.5) except IOError: print("Error")
# Connect the BakeBit Sound Sensor to analog port A0 # SIG,NC,VCC,GND sound_sensor = 0 # Connect the BakeBit LED to digital port D5 # SIG,NC,VCC,GND led = 5 bakebit.pinMode(sound_sensor,"INPUT") bakebit.pinMode(led,"OUTPUT") # The threshold to turn the led on 400.00 * 5 / 1024 = 1.95v threshold_value = 400 while True: try: # Read the sound level sensor_value = bakebit.analogRead(sound_sensor) # If loud, illuminate LED, otherwise dim if sensor_value > threshold_value: bakebit.digitalWrite(led,1) else: bakebit.digitalWrite(led,0) print("sensor_value = %d" %sensor_value) time.sleep(.2) except IOError: print ("Error")
bakebit.pinMode(led, "OUTPUT") time.sleep(1) # Reference voltage of ADC is 5v adc_ref = 5 # Vcc of the BakeBit interface is normally 5v bakebit_vcc = 5 # Full value of the rotary angle is 300 degrees, as per it's specs (0 to 300) full_angle = 300 while True: try: # Read sensor value from potentiometer sensor_value = bakebit.analogRead(potentiometer) # Calculate voltage voltage = round((float)(sensor_value) * adc_ref / 1023, 2) # Calculate rotation in degrees (0 to 300) degrees = round((voltage * full_angle) / bakebit_vcc, 2) # Calculate LED brightess (0 to 255) from degrees (0 to 300) brightness = int(degrees / full_angle * 255) # Give PWM output to LED bakebit.analogWrite(led, brightness) print( "sensor_value = %d voltage = %.2f degrees = %.1f brightness = %d" %