The interface is very simple and composed by 3 buttons to control the servo motor:
1) to make it slowly turn left; 2) to make it slowly turn right; 3) a third one to quickly turn to a user-specified position.
Also a display box is present to show the servo's current position.
The top of the code consist of the declaration of libraries and variables that will be used during the program execution. In particular the objects needed are declared in this way:
AndeeHelper btnTurnLeft; AndeeHelper btnTurnRight; AndeeHelper btnCustomPosition; AndeeHelper displaybox;
#include <SPI.h>
#include <Andee.h>
#include <Servo.h> // You need this for your servos to work
AndeeHelper btnTurnLeft;
AndeeHelper btnTurnRight;
AndeeHelper btnCustomPosition;
AndeeHelper displaybox;
Servo theServo; // Declare a servo
char userPos[4];
int currentPosition; // Stores current position of servo (from 0 to 180)
int newPosition;
int turnResolution = 10; // Servo turns by 20 every time left/right turn button is pressed
const int servoPin = 9; // Servo connected to pin 9
3. SetupIn order to use the Anniken Andee shield it's important to use the method:
Andee.begin()
This instruction initialize the libraries to work in a proper way.
It is very often followed by the method:
Andee.clear()
in order to be sure to start with a blank application.
void setup()
{
Andee.begin(); // Setup communication between Annikken Andee and Arduino
Andee.clear(); // Clear the screen of any previous displays
currentPosition = 0; // Initialise position to 0
theServo.attach(servoPin); // Tell Arduino which pin the servo is connected to
theServo.write(currentPosition); // Set servo to position 0
setInitialData(); // Define object types and their appearance
}
4. setInitialData()This function is used to assign initial values to the objects will be used, such as screen positionig, labels and dimensions.
void setInitialData()
{
btnTurnLeft.setId(0);
btnTurnLeft.setType(BUTTON_IN);
btnTurnLeft.setLocation(0,0,HALF);
btnTurnLeft.setTitle("Turn Left");
btnTurnLeft.requireAck(false); // You need this line to allow for multiple button presses
btnTurnRight.setId(1);
btnTurnRight.setType(BUTTON_IN);
btnTurnRight.setLocation(0,1,HALF);
btnTurnRight.setTitle("Turn Right");
btnTurnRight.requireAck(false); // You need this line to allow for multiple button presses
btnCustomPosition.setId(2);
btnCustomPosition.setType(KEYBOARD_IN); // Sets object as a text input button
btnCustomPosition.setLocation(1,0,FULL);
btnCustomPosition.setTitle("Quickly Go to Custom Position (0 - 180)");
btnCustomPosition.setKeyboardType(ANDEE_NUMERIC_A);
displaybox.setId(3);
displaybox.setType(DATA_OUT); // Sets object as a text input button
displaybox.setLocation(2,0,FULL);
displaybox.setTitle("Current Position");
displaybox.setData(0);
}
5. LoopIn the loop each button of the interface is constantly checked in order to detect actions from the user.
In particular if the CustomPosition button is pressed a dialog box will be showed in order to allow the user to insert the desidered position
void loop()
{
if( btnCustomPosition.isPressed() )
{
memset(userPos, 0x00, 4); // Empty the contents of the string before receiving user input
btnCustomPosition.ack(); // Acknowledge button press or else phone will be left waiting
btnCustomPosition.getKeyboardMessage(userPos); // Display keyboard and store input into userInput
newPosition = atoi(userPos); // Convert string value to integer value
// Tell Arduino x Andee what to do if user keys in ridiculous values
if(newPosition < 0) newPosition = 0;
if(newPosition > 180) newPosition = 180;
currentPosition = newPosition;
// This is how you do a quick turn
theServo.write(currentPosition); // Turn servo to new position
displaybox.setData(currentPosition); // Update new position
}
if( btnTurnLeft.getButtonPressCount() > 0 ) // As long as left button is pressed
{
btnTurnLeft.ack(); // Acknowledge button press or else phone will be left waiting
newPosition = currentPosition - turnResolution; // Set new position
if(newPosition < 0) newPosition = 0; // Set to 0 if new position goes below 0
// This is how you do a slow turn:
for(currentPosition; currentPosition > newPosition; currentPosition--)
{
theServo.write(currentPosition);
displaybox.setData(currentPosition); // Update servo position on screen as it turns
displaybox.update();
delay(15); // You can change the delay value. Larger value means slower turns
// Do not set your delay to 0 when you're doing this. You run the possibility of
// damaging the servo. Or you might just make it become more cranky.
}
}
if( btnTurnRight.getButtonPressCount() > 0 ) // As long as right button is pressed
{
btnTurnRight.ack(); // Acknowledge button press or else phone will be left waiting
newPosition = currentPosition + turnResolution; // Set new position
if(newPosition > 180) newPosition = 180; // Set to 180 if new position goes above 180
for(currentPosition; currentPosition < newPosition; currentPosition++)
{
theServo.write(currentPosition);
displaybox.setData(currentPosition); // Update servo position on screen as it turns
displaybox.update();
delay(15); // You can change the delay value. Larger value means slower turns
}
}
btnTurnLeft.update(); // Always remember to update so that new content will be displayed
btnTurnRight.update();
btnCustomPosition.update();
displaybox.update();
delay(500); // Always leave a short delay for Bluetooth communication
}
6. Full code & CircuitHere the whole code and the simple circuit used to test it.
#include <SPI.h>
#include <Andee.h>
#include <Servo.h> // You need this for your servos to work
AndeeHelper btnTurnLeft;
AndeeHelper btnTurnRight;
AndeeHelper btnCustomPosition;
AndeeHelper displaybox;
Servo theServo; // Declare a servo
char userPos[4];
int currentPosition; // Stores current position of servo (from 0 to 180)
int newPosition;
int turnResolution = 10; // Servo turns by 20 every time left/right turn button is pressed
const int servoPin = 9; // Servo connected to pin 9
void setup()
{
Andee.begin(); // Setup communication between Annikken Andee and Arduino
Andee.clear(); // Clear the screen of any previous displays
currentPosition = 0; // Initialise position to 0
theServo.attach(servoPin); // Tell Arduino which pin the servo is connected to
theServo.write(currentPosition); // Set servo to position 0
setInitialData(); // Define object types and their appearance
}
void setInitialData()
{
btnTurnLeft.setId(0);
btnTurnLeft.setType(BUTTON_IN);
btnTurnLeft.setLocation(0,0,HALF);
btnTurnLeft.setTitle("Turn Left");
btnTurnLeft.requireAck(false); // You need this line to allow for multiple button presses
btnTurnRight.setId(1);
btnTurnRight.setType(BUTTON_IN);
btnTurnRight.setLocation(0,1,HALF);
btnTurnRight.setTitle("Turn Right");
btnTurnRight.requireAck(false); // You need this line to allow for multiple button presses
btnCustomPosition.setId(2);
btnCustomPosition.setType(KEYBOARD_IN); // Sets object as a text input button
btnCustomPosition.setLocation(1,0,FULL);
btnCustomPosition.setTitle("Quickly Go to Custom Position (0 - 180)");
btnCustomPosition.setKeyboardType(ANDEE_NUMERIC_A);
displaybox.setId(3);
displaybox.setType(DATA_OUT); // Sets object as a text input button
displaybox.setLocation(2,0,FULL);
displaybox.setTitle("Current Position");
displaybox.setData(0);
}
void loop()
{
if( btnCustomPosition.isPressed() )
{
memset(userPos, 0x00, 4); // Empty the contents of the string before receiving user input
btnCustomPosition.ack(); // Acknowledge button press or else phone will be left waiting
btnCustomPosition.getKeyboardMessage(userPos); // Display keyboard and store input into userInput
newPosition = atoi(userPos); // Convert string value to integer value
// Tell Arduino x Andee what to do if user keys in ridiculous values
if(newPosition < 0) newPosition = 0;
if(newPosition > 180) newPosition = 180;
currentPosition = newPosition;
// This is how you do a quick turn
theServo.write(currentPosition); // Turn servo to new position
displaybox.setData(currentPosition); // Update new position
}
if( btnTurnLeft.getButtonPressCount() > 0 ) // As long as left button is pressed
{
btnTurnLeft.ack(); // Acknowledge button press or else phone will be left waiting
newPosition = currentPosition - turnResolution; // Set new position
if(newPosition < 0) newPosition = 0; // Set to 0 if new position goes below 0
// This is how you do a slow turn:
for(currentPosition; currentPosition > newPosition; currentPosition--)
{
theServo.write(currentPosition);
displaybox.setData(currentPosition); // Update servo position on screen as it turns
displaybox.update();
delay(15); // You can change the delay value. Larger value means slower turns
// Do not set your delay to 0 when you're doing this. You run the possibility of
// damaging the servo. Or you might just make it become more cranky.
}
}
if( btnTurnRight.getButtonPressCount() > 0 ) // As long as right button is pressed
{
btnTurnRight.ack(); // Acknowledge button press or else phone will be left waiting
newPosition = currentPosition + turnResolution; // Set new position
if(newPosition > 180) newPosition = 180; // Set to 180 if new position goes above 180
for(currentPosition; currentPosition < newPosition; currentPosition++)
{
theServo.write(currentPosition);
displaybox.setData(currentPosition); // Update servo position on screen as it turns
displaybox.update();
delay(15); // You can change the delay value. Larger value means slower turns
}
}
btnTurnLeft.update(); // Always remember to update so that new content will be displayed
btnTurnRight.update();
btnCustomPosition.update();
displaybox.update();
delay(500); // Always leave a short delay for Bluetooth communication
}



_ztBMuBhMHo.jpg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)

Comments