Difference between revisions of "BakeBit - LED Bar"

From FriendlyELEC WiKi
Jump to: navigation, search
(示例程序(2):Smart Lighting)
 
(6 intermediate revisions by the same user not shown)
Line 31: Line 31:
 
A [[BakeBit - Rotary Angle Sensor]] module is needed in this test case.
 
A [[BakeBit - Rotary Angle Sensor]] module is needed in this test case.
  
=== Hardware Connection ===
+
=== Hardware Setup ===
Connect the LED Bar module to the NEO-Hub at D3 and the rotary angle sensor module to the NEO-Hub at A0:  
+
Connect the LED Bar module to the NanoHat Hub's D3 and the rotary angle sensor module to the NanoHat Hub's A0:  
  
 
::{| class="wikitable"
 
::{| class="wikitable"
Line 132: Line 132:
 
=== Run Code Sample ===
 
=== Run Code Sample ===
 
   
 
   
Before you run the code sample you need to follow the steps in [http://wiki.friendlyarm.com/bakebit bakebit tutorial] to intall the BakeBit package.<br />
+
Before you run the code sample you need to follow the steps in [http://wiki.friendlyarm.com/bakebit bakebit tutorial] to install the BakeBit package.<br />
 
Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_LEDBar_And_RotaryAngleSensor.py" program:
 
Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_LEDBar_And_RotaryAngleSensor.py" program:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
Line 147: Line 147:
 
A [[BakeBit - Light Sensor]] module is needed in this test case. For more details about the light sensor module refer to the [[BakeBit - Light Sensor]] page.
 
A [[BakeBit - Light Sensor]] module is needed in this test case. For more details about the light sensor module refer to the [[BakeBit - Light Sensor]] page.
  
==相关资料==
+
==Resources==
 
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/0/08/07-SCHEMATIC_LED_Bar.pdf BakeBit - LED Bar.pdf])
 
*[Schematic]([http://wiki.friendlyarm.com/wiki/images/0/08/07-SCHEMATIC_LED_Bar.pdf BakeBit - LED Bar.pdf])
*[BakeBit Github项目](https://github.com/friendlyarm/BakeBit)
+
*[BakeBit Github Project Page](https://github.com/friendlyarm/BakeBit)
*[BakeBit Starter Kit手册](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_cn.pdf)
+
*[BakeBit Starter Kit User's manual](http://wiki.friendlyarm.com/bakebit/bakebit_starter_kit_manual_en.pdf)
 +
 
 +
==Update Log==
 +
===December-13-2016===
 +
* Released English Version
 +
 
 +
===Jan-19-2017===
 +
* Renamed "NEO-Hub" to "NanoHat-Hub"
 +
 
 +
===Jan-20-2017===
 +
* Renamed "NanoHat-Hub" to "NanoHat Hub"

Latest revision as of 08:15, 20 January 2017

查看中文

1 Introduction

LED Bar
  • The BakeBit - LED-Bar is a multiple-LED module. It has five LEDs and each LED is controlled by a WS2812 chip.
  • These five LEDs are in series connection.

2 Hardware Spec

  • Standard 2.0mm pitch 4-Pin BakeBit Interface
  • Multi-color R, G, B LED
  • True Color

BakeBit - LED Bar

  • Pin Description:
Pin Description
GND Ground
5V 5V Supply Voltage
NC Not Connected
SIG Signal

3 Code Sample(1): LED Bar

By running this code sample users can adjust the LED bar with the rotary angle sensor module.
A BakeBit - Rotary Angle Sensor module is needed in this test case.

3.1 Hardware Setup

Connect the LED Bar module to the NanoHat Hub's D3 and the rotary angle sensor module to the NanoHat Hub's A0:

LED Bar-1.jpg LED Bar-2.jpg

3.2 Source Code

import time
import bakebit
import random
 
# Connect the BakeBit LED Bar to digital port D3
# DI,DCKI,VCC,GND
ledbar = 3
 
# Connect the BakeBit Rotary Angle Sensor to analog port A0
# SIG,NC,VCC,GND
potentiometer = 0
 
# 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 360 degrees, as per it's specs (0 to 360)
full_angle = 360
 
bakebit.pinMode(potentiometer,"INPUT")
bakebit.pinMode(ledbar,"OUTPUT")
time.sleep(1)
 
# LED Bar methods
# bakebit.bakeBitLedBar_Init(pin, chipset, numOfLED)
# bakebit.bakeBitLedBar_Release(pin)
# bakebit.bakeBitLedBar_Show(pin,colorHigh,colorLow)
 
bakebit.bakeBitLedBar_Init(ledbar, 0, 5)
time.sleep(.5)
 
old_color = 0
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 360)
        degrees = round((voltage * full_angle) / bakebit_vcc, 2)
        print("sensor_value = %d voltage = %.2f degrees = %.1f" % (sensor_value, voltage, degrees))
 
        color16bit = 0
        if degrees > 0:
            color16bit = color16bit | bakebit.Green
        if degrees > 72:
            color16bit = color16bit | (bakebit.Green<<3)
        if degrees > 144:
            color16bit = color16bit | (bakebit.Green << 6)
        if degrees > 216:
            color16bit = color16bit | (bakebit.Yellow << 9)
        if degrees > 288:
            color16bit = color16bit | (bakebit.Red << 12)
        if degrees == 360:
            color16bit = 0
            color16bit = color16bit | bakebit.Red
            color16bit = color16bit | (bakebit.Red << 3)
            color16bit = color16bit | (bakebit.Red << 6)
            color16bit = color16bit | (bakebit.Red << 9)
            color16bit = color16bit | (bakebit.Red << 12)
 
        if color16bit == old_color:
            continue
        old_color = color16bit
 
        lowBits = color16bit & 255
        highBits = (color16bit & (255<<8))>>8
        print("%s %s" % ('{0:08b}'.format(highBits), '{0:08b}'.format(lowBits)))
        bakebit.bakeBitLedBar_Show(ledbar, highBits, lowBits)
        time.sleep(.2)
 
    except KeyboardInterrupt:
        bakebit.bakeBitLedBar_Release(ledbar)
        time.sleep(.2)
        break
    except IOError:
        print ("Error")

Github


3.3 Run Code Sample

Before you run the code sample you need to follow the steps in bakebit tutorial to install the BakeBit package.
Enter the "BakeBit/Software/Python" directory and run the "bakebit_prj_LEDBar_And_RotaryAngleSensor.py" program:

cd ~/BakeBit/Software/Python
sudo python bakebit_prj_LEDBar_And_RotaryAngleSensor.py

3.4 Observation

When you rotate the rotary angle sensor module the LEDs on the LED Bar will be turned on or off.

4 Code Sample(2): Smart Lighting

By running this code sample your system adjusts the LEDs on the LED module according to surrounding lightness. When the surrounding environment becomes darker the LEDs will be brighter. When the surrounding becomes bright enough the LEDs will be completely off.
A BakeBit - Light Sensor module is needed in this test case. For more details about the light sensor module refer to the BakeBit - Light Sensor page.

5 Resources

6 Update Log

6.1 December-13-2016

  • Released English Version

6.2 Jan-19-2017

  • Renamed "NEO-Hub" to "NanoHat-Hub"

6.3 Jan-20-2017

  • Renamed "NanoHat-Hub" to "NanoHat Hub"