[App Inventor and Arduino] Lesson 2: LED fading(PWM)

Continuing from last [App Inventor and Arduino: Lesson 1: LED Blink]. This topic is going to introduce how to use App Inventor's slider component to control Arduino's LED fading.

Below are all lessons of how to interact with your Arduino with App Inventor, just try it~

SOURCE CODE PLEASE CLICK ME filename: SlideControlLED.aia 

YouTube 影片


Review:

App Inventor and Arduino: Lesson 0:Bluetooth
App Inventor and Arduino: Lesson 1: LED Blink

Part List:

1. Android phone (1.6 above are all fine).

2. Arduino and Arduino compatible board (With Arduino MEGA2560, but Arduino UNO is completely OK)

3. Bluetooth module (With JY-MCU04 or JY-MCU05)

4. Breadboard, x1

5. 220 ohm resistor, x1

6. LED, x1

Hardware connection:

First, please connect the Bluetooth module to your Arduino board, since we are using SoftwareSerilal library, we are NOT using RX/TX of Arduino anymore (digital pin #0, #1)please connect module's RX pin to Arduino's digital pin #11, and module's TX pin to Arduino's digital pin #10. 

And for LED, please connect a LED to Arduino digital pin #9 or other pins supporting PWM. (D3, 5, 6, 9, 10, 11 on Arduino Uno).





App Inventor Designer:

Quite easy interface with one of one ListPicker and two Button components. And one Bluetooth client components for Bluetooth communication. Please check the description below:

1. BTList (LIstPicker): To show all the paired devices. The app will connect to the device that user selected.

2. Slider1 (Slider): Analog control component, which will determine the brightness of LED. 

3. Clock1 (Clock): Send our Bluetooth message regularly according to slider component's thumb position, which is disabled when you first start the app. 

4. Disconnect (Button): Disconnect from Arduino.

 

How to play:

First you must pair the Bluetooth module with your Android phone. After you connect all the hardware, you can see that the Bluetooth module's red LED is flashing, means ready for connecting. Then please open your Android phone's Bluetooth setting page (which should under Settings... , differs on each phone), click the name of your module (something like HC-05). Please enter 1234 (or 0000) when there is a prompt asking the paring key. Done~

There is only BTList which is enabled when you first open the app. Please click BTList, it will show all paired Bluetooth devices on your phone. 

Please click the name of your module, it should take 1~2 seconds to connect and back the main screen. You can see drag the slider from left to right, you should see LED is fading. Please click the [Disconnect] button when you don't want to play anymore, this will disconnect the connection of your phone and bluetooth module and the module's LED will flashing again.


螢幕快照 2014-04-04 上午12.28.35 
App Inventor Blocks:

STEP1: Within Screen1.Initialize event, we set that only BTList is enabled, all other components are disabled. It enforce the user must to select the device they want to use, otherwise sending out Bluetooth message without connecting will cause error on your screen (annoying...).

STEP2: In the ListPicker1.BeforePicking event, we set the BTList.Elements toBluetoothClient1.AddressAndNames, therefore we can put paired devices pist into this ListPicker.  

STEP3: In the ListPicker1.AfterPicking event, 
we first use an if statement to check whether the connection is established (BluetoothClient1.Connect -> BTList.Selection). If yes, then flip other components to be enabled, BTList to be disabled. 

螢幕快照 2014-04-04 上午12.29.00
STEP4: 
In Clock.Timer event, we are sending out the current thumb position of the Slider, by using BluetoothClient.SendText command very 0.01 second(10 ms) 

ffff

At the last, press the Disconnect button will disconnect the connection of your phone and bluetooth module and flip all visible components to their initial settings.  


Arduino sketch:

Arduino code is quite straightforward as well. First import necessary libraries: SoftwareSerial and Wire, and define digital pins 11, 10 as RX, TX pins.

In setup(), we set the communication speed of Serial Monitor (USB, 9600) and Arduino (Bluetooth, 57600). Then set Arduino's digital pin #13 to output since we have a LED on it.

There are much thing to do in loop(), first we use if ((insize=(I2CBT.available()))>0 ) to check whether there is an incoming mesage. If yes, then use Serial.print(cmmd[i] = char(I2CBT.read())); to show what Arduino get on Serial Monitor (note: char(I2CBT.read())) is to transfer what Arduino read into char format). 

Please note that the insize variable may differ according to what number Arduino is receiving from App Inventor. If insize is 4, then we have to do some math to combine two byte cmmd[0] and cmmd[1] to get the actual number sending from App Inventor. If it is 3, then just use cmmd[0] directly. 

Finally, we use variable a to control LED's brightness using analogWrite(9, map(a, 0, 80, 0, 255);. Enjoy~

if(insize==4){ a = (cmmd[0]-48)*10; a = a+(cmmd[1]-48); } if(insize==3){ a=(cmmd[0]-48); }


Complete Arduino sketch

#include <SoftwareSerial.h> #include <Wire.h>//import necessary libraries: SoftwareSerial, Wire SoftwareSerial I2CBT(10,11);//define PIN11 and PIN10 as RX and TX byte cmmd[20]; int insize, a; void setup() { Serial.begin(9600); //set USB baudrate: 9600 I2CBT.begin(57600); 
//BT baudrate: 57600(Warning!! baudrate of each BT module may differ, //please check the data sheet first!!
pinMode(9, OUTPUT); //set pin 9 as output, where we connect a LED
//or other pins supporting PWM (#3,5,6,9,10,11 on Arduino Uno) } void loop() { while(1){ //read BT incoming message
if ((insize=(I2CBT.available()))>0){ Serial.print("input size = "); Serial.println(insize); for (int i=0; i<insize; i++){ Serial.print(cmmd[i]=char(I2CBT.read())); Serial.print(" "); } } if(insize==4){ a = (cmmd[0]-48)*10; a = a+(cmmd[1]-48); } if(insize==3){ a=(cmmd[0]-48); } Serial.println(a); analogWrite(9,map(a,0,80,0,255)); //control LED with variable a } //while }
Comments