App Inventor, Bluetooth and Arduino - Part 3
I’ve already done a lot of work controlling Arduino connected devices over Ethernet, including using a slider from Google Visualisations to control the brightness of an LED, and using three sliders to control a Tri-Colour LED.
Both of these exercises were fairly nightmarish, involving a lot of javascript, which I didn’t fully understand, along with the HTTP commands GET and PUT, which I also don’t fully understand! (Anyone interested in this work, let me know and I will post something).
I decided to try my hand at building a couple of similar Android Apps and corresponding Arduino sketches to mirror what I had already done with Ethernet.
I was successful! What really amazed me was how incredibly simple both the Apps and the Arduino sketches turned out to be, compared with the Ethernet/javascript versions.
Here I detail the simple LED brightness solution.
Development of the basic Slider App
Above is a screenshot of a simple Android device display, containing a Slider and a Label to hold the current value of the slider, in a Horizontal arrangement to put them side by side.
The default Slider settings are Minimum 10.0, Maximum 50.0 with an initial slider setting of 30.0.
For use in controlling an LED, try setting Minimum 0, Maximum 255 and initial setting 100.
We should also make the Slider wider - I set it to 250 Pixels wide:
It’s very helpful if you connect your Android device to App Inventor at this stage so that you can see how the App behaves and how it needs to be developed.
The first objective is to get Label1 to display the current value of Slider1, as it is moved:
However there are 2 serious problems with this simple approach:
- The thumbPosition property is a floating point number which is sometimes displayed to several decimal places - an integer value will be fine!
- As Slider1 is moved, updates occur very rapidly; this has the potential to overflow the Arduino input buffer.
What we need is a code Block which will set a Flag and capture the current value of thumbPosition :
when Slider1.PositionChanged
{
set myFlag = TRUE;
set myValue = Slider1.thumbPosition;
}
This should be followed by a second code Block which will be executed each time a Timer or Clock ticks:
when Clock1.Timer
{
do
{
if (myFlag)
{
set Label1.Text = myValue;
set myFlag = FALSE;
}
}
}
We will also need to declare 2 Global Variables to pass the Flag and the value of thumbPosition between the code Blocks.:
Below are the variable declarations and the changes to the Slider code Block to use the variables. Note I have used the round fuction to convert the floating point value of thumbPosition to integer:
Now to incorporate the Clock - which by default, will have a cycle time of 1000 mS.
Switching to the Design screen, we drag a Clock across to the display and drop it. The Clock is a non-visible component and positions itself below the display.
Returning to the Blocks screen, we can now create the code Block related to the Clock:
One final problem remains: the left hand edge of the Slider is hard up against the left hand edge of the screen, making it impossible to adjust the Slider to its minimum value. There is, as yet, no provision in App Inventor to position a component away from the left hand edge.
The easiest solution to this problem is to add a dummy Label to the left of the Slider, make it 20 pixels wide and remove its text so that it is invisible. This moves the Slider 20 pixels to the right.
Connecting to the Arduino via the Bluetooth Client
This is the same as in the previous postings:
We need a ListPicker, a Label to display the connection status and a Button to disconnect from Bluetooth
The new screen layout becomes:
The connection Blocks are here:
And, of course, the disconnect Block:
Sending the data to the Arduino via the Bluetooth Client
This is simply a matter of adding a component to the Clock Block to send out the value of myValue as a 1 Byte number (maximum unsigned value 255)
The Arduino sketch to work with this App is:
int ledPin = 3; // Needs to be a pin supporting PWM
int value = -1;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
while (Serial.available()) // Read while there is data in the buffer
{
value = Serial.read();
if (value >= 0) / /If we have meaningful data
{
Serial.println(value); // Debug - double check it make sense?
analogWrite(ledPin,value); // set PWM for LED brightness
value = -1;
}
}
}
Note: a lot has been written about LED brightness needing to be controlled in a non-linear manner, in order for the eye to perceive brightness changes linearly. It is not the purpose of this post to address that issue!
Further Note: instead of controlling the brightness of an LED, with a suitable Arduino sketch, this App could control the position of a Servo.
Top Comments