element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Legacy Personal Blogs App Inventor, Bluetooth and Arduino Part 3; Android Slider controls LED brightness
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: neilk
  • Date Created: 5 Sep 2014 6:19 PM Date Created
  • Views 7014 views
  • Likes 1 like
  • Comments 28 comments
  • android
  • bluetooth
  • hc06
  • mit
  • appinventor
  • arduino
Related
Recommended

App Inventor, Bluetooth and Arduino Part 3; Android Slider controls LED brightness

neilk
neilk
5 Sep 2014

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


image

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:


image


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:



image


However there are 2 serious problems with this simple approach:


  1. The thumbPosition property is a floating point number which is sometimes displayed to several decimal places - an integer value will be fine!


  1. 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:


image


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:





image

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:



image


The connection Blocks are here:



image


And, of course, the disconnect Block:



image


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)



image


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.

  • Sign in to reply

Top Comments

  • Former Member
    Former Member over 10 years ago +1
    Hi Neil, I try to rebuild the app from your example. Unfortunately I can't see the pictures anymore... I'm getting below 404 error when clicking them. 404. That’s an error. The requested URL/llQW0xW0a8SnIMsVzpuFV0gd9Za6P2Ubij8cIulQR6CtqIb62gOmlvKiBJKdmS8UPBrSHrG388u5_0x9Gx93XmozusYAUO9gN4XFveNipn5ZiuNrqI6COjPooxh_6nrmzQwas…
  • alacosta
    alacosta over 9 years ago +1
    Hi Neil, pleased to say all systems are working well ...thank you for your guidance. Have a mystery .. you will see from the attached serial read, the output is converting the Decimal Value to the ascii…
  • alacosta
    alacosta over 9 years ago in reply to neilk +1
    Hi Neil, Again thank you for your quick response ... as I said was not an issue to the project but interesting to know what is happening. Regards John
  • neilk
    neilk over 2 years ago in reply to sebulox

    Glad you fixed it. Nice to know something this old still has value

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • sebulox
    sebulox over 2 years ago in reply to sebulox

    It's me again, fixed the issue 1) by moving to pin D5. Seems that A3 and D2 are not PWM supported. Now working. Great tuto, thanks.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • sebulox
    sebulox over 2 years ago

    Hi, 1) Tried the MIT inventor and arduino scripts as suggested. Android app is working fine, Arduino serial correctly receives numbers (each number separated by a "0"), but the led would not turn on. I connected the LED long leg to A3 (then also tried D2 and D13) with a 330 ohm resistor in between, and the short led to GND. I tried the blink script and LED is working ok. Any reasons for that?

    2) I wonder whether we could use a dimmer to define led blinking frequency ? I tried to replace the "analogWrite" with "digitalWrite(ledPin, HIGH);delay(value): digitalWrite(ledPin, HIGH); delay(value)"; but LED blinks only once and then stay off.

    Thank you for your support; in general I'm looking for options to send numeric values to control arduino via android, for other applications (e.g. water pump timer, etc.).

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 9 years ago in reply to alexis_cy

    So I presume that is why you can't get it to work ??

     

    Please mark my answer either helpful, or correct. Thanks

     

    Neil

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • alexis_cy
    alexis_cy over 9 years ago in reply to neilk

    You are correct BLE support is not available in App inventor

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube