2017年4月3日星期一

App Inventor and Maker (2): Bluetooth Desk LED Light


The app that built with App Inventor can turn LED strips lights on or off, adjust the light color temperature and brightness with your cell phone. It can do these by timer.








This works gained first prize in 2016 Guangdong Student Maker Contest!

Arthor: Huanglan, Liu jianke. They are all grade 9 students.

Mentors: He wei, Xiangqun Chen

User Interface - Components

  • Image 1: to show logo

  • lblSpace1~lblSpace6: to add spaces between components

  • listConnect: Listpicker component to a bluetooth address to connect

  • VerticalControl: VerticalArrangement component to put LED control panel. It includes several horizontal arrangements.

  • bthDisconnect: To disconnect the phone and LED Strips

  • lblNotifier: To show notifier

  • BluetoothClient1: BluetoothClient Component to connent the phone and LED strips

  • Clock1: in each interval time to check if there is a text to send to arduino board.

 
  • VerticalControl:

  • btnOn: Button component to turn the LED on or off

  • HorizontalTemp:  To show color temperature

  • HorizontalSliderTemp: 
SliderTemp: Slider component to change color temerature

  • HorizontalBrightness: To show color brightness

  • HorizontalSliderBright: 
SliderBrightness: Slider component to change color brightnes

  • HorizontalSetTime: 
TimePicker1: TimePicker component to pick time
SpinnerChoose: To select an event to do

  • HorizontalTimer: 
btnStart: Button component to start a timer
btnCancell: Button component to cancell a timer

                  


 Blocks


  • Initialize variables


hour: hour of timer
minute: minute of timer
doWhat: text that sends to arduino board
status: 1: the light is on, 0: the light is off
ifSent: 1: the text has been sent, 0: the text hasn't been sent
  
  • Initialize screen: to initialize the background color of buttons


  •  Before connecting, set listConnect.Elements to BluetoothClient1.AddressAndNames


  • After picking, if the cell phone is connected with arduino board, make the control panel to show, and hide listConnect.












  • When adjust temperature slider, gets the value of thumbPosition and calls BluetoothClient.SendText to send corresponding text.  You should first round thumPosition.

If thumPosition is
1, then send text "A": The light is cold.
2, then send text "B": The light is warm.
3, then send text "C": The light is cold-warm.





















  • When adjust brightness slider, gets the value of thumbPosition and calls BluetoothClient.SendText to send corresponding text. You should first round thumPosition.

If thumbPosition is
1, then send text "a", the brightness is 10% of
2, then send text "b"
3, then send text "c"
4, then send text "d"
5, then send text "e"
6, then send text "f"
7, then send text "g"
8, then send text "h"
9, then send text "i"
10, then send text "j"




























































  • When after choosing an event to do in time, to set value to doWhat variable.
If selection is
Light on, then set the variable doWhat to "A".
Light off, then set the variable doWhat to "B"
Warm light, then set the variable doWhat to "C"
Cold light, then set the variable doWhat to "D"
Cold_Warm light, then set the variable doWhat to "E"
10% brightness, then set the variable doWhat to "a"
20% brightness, then set the variable doWhat to "b"
30% brightness, then set the variable doWhat to "c"
40% brightness, then set the variable doWhat to "d"
50% brightness, then set the variable doWhat to "e"
60% brightness, then set the variable doWhat to "f"
70% brightness, then set the variable doWhat to "g"
80% brightness, then set the variable doWhat to "h"
90% brightness, then set the variable doWhat to "i"
100% brightness, then set the variable doWhat to "j"
















































































  • When after picking time, set global.minute to TimePicker1.Minute, set global.hour to TimePicker1.Hour











  • When btnStart.Click, if time is valid, then set gloabal ifSent to 0, make btnStart and TimePicker1 disable, make btnCancell and Clock1 enable.

















  • When btnCancell.Click, then set  ifSent to 0, set Clock1.TimeEnabled to false.

  • When Clock1.Timer, if current time hits the setting time, the sends variable doWhat to arduino board, sets variable ifSent to 1, sets btnStart.Enabled and TimePicker1.Enabled to true, sets btnCancelled.Enable and Clock1.TimeEnabled to false.



When btnDisconnect.Click, set VerticalControl.Visible adn btnDisconnect.Visible to false, set listConnect.Visible to true, call BluetoothClient1.Disconnect.












Now we have finished all blocks. But there are so many if-then-else blocks and they do similar things!

we may optimize our blocks.  

  • First, we add two variables: 
        timerItems: to store all 15 events: Light on, Light off, ... , 10% brightness, ... , 100% brightness.
        sendText: to store all texts that send to arduino board: A~E, a~j








  • Define some procedures to call:
procedure: setBackgroundColor, to set some buttons BackgroundColor

 procedure setTimerItemList: to set timerItems to a list that has 15 items.



 procedure setSendTextList: to set sendText to a list that has 15 items.
 






















  • Initialize screen:



  • When SpinnerChoose.AfterSelecting:
 We just use one loop and one conditional blocks to do all judgement.











  • When btnOn.Click: 
We use list items to substitute specific texts to update our blocks.




  • When SliderBrightness.PositionChanged:
We use a loop and a conditional block to judge all 10 conditions.



  • When SliderTemp.PositionChanged:
The items have index 3 to 5 in the list sendText are C, D and E.



So you see, the blocks are much more simple and logical.

以下是中文代码

组件设置:

 

定义变量:








屏幕初始化:
























 将蓝牙列表赋值给列表选择框











选择完成后,如果蓝牙成功连接,显示垂直布局(控制面板)及断开按钮,隐藏列表选择框

















当点击开关按钮时,如果当前是开灯状态,将开关文本设置为“关灯”,否则将文本设置为“开灯”
























当改变色温时,发送相应文本: C为白光,D为黄白光,E 为黄光
























 当改变亮度时,获取当前亮度值:
1:亮度10%,2:亮度20%,... ,10:亮度100%









































 当选择完需要定时的任务后,发送相应字符: 15个任务对应15个字符,分别为A~E, a~j




















  

























当设置好定时的时间后,分别将小时和分钟的数值保存起来









当点击“定时”按钮后,开始计时





 当点击“取消”按钮时,取消当前定时的任务,关闭定时器













当计时器开始计时后, 如果到达计时点,则执行相应任务








当点击“断开”按钮时,隐藏LED灯控制面板,隐藏“断开”按钮,显示“连接”按钮,断开蓝牙连接。