This post builds on previous posts, drawing together the use of AppInventor to create an Android app to send commands, via Bluetooth, to an Arduino controlling a stepper motor using the AccelStepper library.
Because the HC06 Bluetooth board is connected to the Arduino serial I/O on Digital pins 0 and 1, it is possible to develop and test the Arduino sketches independently of the Android Apps. The data stream anticipated from the Android app, via Bluetooth, can be simulated by opening the serial monitor, ensuring that "No line ending" is selected in the drop down box in the bottom right-hand corner and then using the keyboard to enter command strings.
Two different approaches were developed to demonstrate the control possibilities:
In the first example, a number of simple, fixed command strings of the form "com1", "com2" etc, were defined, each command string sent by pressing a different button on the Android device. The actual interpretation of the command strings is a function of the Arduino sketch.
In the second example, pressing three buttons, in the correct sequence, constructs a command string containing 3 parameters. The parameters are:
- mode - move/step by a number of steps,OR run continuously
- direction - clockwise or anti-clockwise
- size - representing number of steps OR speed at which to run continuously
EXAMPLE 1 - generating fixed command strings
The screen appearance of the Android app needed to send out the fixed command strings is shown below:
A few observations on the appearance of the Android screen:
- In order to space the buttons neatly, a table arrangement of 4 columns and 5 rows has been used.
- The buttons are placed in columns 2 and 4, rows 1,3 and 5. Blank label separators are used in columns 1 and 3 and rows 2 and 4 to achieve the desired layout.
The code blocks necessary to achieve the Bluetooth connection are similar to previous posts. However, a deliberate test for a successful connection has been added, with an appropriate message if the connection failed.
The code blocks necessary to transmit the fixed command strings are quite simple:
An additional command string, "comZ" is transmitted if the Bluetooth connection is deliberately disconnected.
EXAMPLE 1 - the Arduino Sketch to receive and interpret the fixed command strings.
The Arduino sketch can be broken down into a number of simple steps:
- Initialise the AccelStepper library
- Define Global Variables
- Execute the setup() function, including disabling the motor drive pins
- Within the loop() funtion, the following steps are executed:
- read datastring from the serial interface
- check the data string for validity and extract the command character - "0", "1", etc
- use a switch...case statement to process the command character and set up the stepper motor variables accordingly
- execute either stepper.run() or stepper.runSpeed() depending on the value of the command character
The interpretation of the command strings is as follows:
- com0 = STOP immediately and disable the motor drive pins
- com1 = Move/Step the number of steps defined by a variable in the sketch, CLOCKWISE
- com2 = Move/Step the number of steps defined by a variable in the sketch, ANTI-CLOCKWISE
- com3 = Run at a speed defined by a variable in the sketch, CLOCKWISE
- com4 = Run at a speed defined by a variable in the sketch, ANTI-CLOCKWISE
- comZ = STOP immediately and disable the motor drive pins - Bluetooth has been disconnected by the Android
EXAMPLE 2 - generating variable command strings
The screen appearance of the Android app for this second example is shown here:
A few observations about the design:
- The Disconnect button is not visible on the screen UNTIL the Bluetooth client has been connected - demonstrating an additional feature of AppInventor.
- As in Example 1, the command generating buttons and the STOP button are arranged in a table, containing 5 columns and 7 rows. Again, empty labels are used to space the buttons neatly.
- It is difficult to choose specific values for the speed/number of steps buttons because of the disparity in values - the 28BYJ-48 has a nominal 4096 steps per revolution, so although a speed of 500 is quite fast for this motor, 500 steps doesn't move it much. For this reason, the 3 buttons labelled " >" , ">>" ," >>>" actually transmit the characters "1" , "2" , "3" and their precise meaning is decided in the Arduino sketch.
- The STOP button transmits a valid command string with "0" as the the 3rd parameter.
Here are the code blocks to establish the Bluetooth connection:
Note the additional instruction set disconnectButton.Visible to true This will only be executed if the Bluetooth client connects successfully.
The code blocks defining the flags and variables and to handle the mode buttons (STEP and RUN) are shown next:
The logic for the STEP and RUN buttons is the same, but with dataString initialised appropriately. There is no need to check any flags before setting up dataString. However, just in case the buttons have been pressed out of sequence, there is a need to reset directionFlag.
The code blocks to handle the direction parameter are shown below:
With the direction buttons it is necessary to prevent button presses out of sequence from corrupting the dataString. We need to test both the modeFlag and the directionFlag, to make sure we don't press a direction button a second time.
In this next code block - only one of 3 almost identical blocks is shown here - we see how the size buttons are handled:
As can be seen, the size button is ignored unless both the modeFlag and the directionFlag are set. After the size character has been concatenated onto the dataString, the dataString is sent out to the Bluetooth device and on to the Arduino
EXAMPLE 2 - the Arduino Sketch to receive and interpret the variable command strings.
The Arduino sketch can be broken down into a number of simple steps:
- Initialise the AccelStepper library
- Define Global Variables
- Execute the setup() function, including disabling the motor drive pins
- Within the loop() funtion, the following steps are executed:
- read datastring from the serial interface
- parse the data string, extracting the mode, direction and size parameters and check validity
- process the command parameters and set up the stepper motor variables according to the size parameter
- execute either stepper.run() or stepper.runSpeed() depending on the value of the mode parameter
The size parameter is interpreted so as to show significant changes:
- STEP = 512 steps RUN = speed of 100
- STEP = 1024 steps RUN = speed of 200
- STEP = 4096 steps RUN = speed of 500
Of course these values can be easily changed within the sketch.
One important observation is that the stepper.moveTo(no of steps) and stepper.setSpeed(speed) commands are non-blocking commands. This means:
- Program flow is not halted and movement of the stepper motor depends on repeated execution of the stepper.run() or stepper.runSpeed() commands.
- The current behaviour of the stepper motor will be interrupted if a new command is issued.
The accelStepper library does include blocking commands which will hold up program flow in the sketch until they have completed and can only be interrupted literally - ie by using an eternal interrupt.
The .aia files for the Android apps and the .ino files for the Arduino sketches are attached.