// 2011 mlab.taik.fi/paja // Controlling servo motor from N9 QML application over Bluetooth // import Servo library. This library comes with Arduino software by default. // To install a library, unzip the library and copy it to following location. // Arduino.app > Contents > Resources > Java > hardware > libraries. // Restart the Arduino. Yoshould see the library in the Import Library menu. #include // You can access all Servo related features through this library. // Each statement always have to end with ";" (semicolon) symbol. Otherwise compiler will return a error. // declare Servo object into a memory space. Servo myServo; // declare integer variable which you can store numbers between -32,768 and 32,767 int incomingByte = 0; // setup() and loop() are mandatory functions. Setup() only runs once after pressing a reset // button or power is supplied. loop() is excuted consecutively after setup ran. // As both functions don't return any value, datatype is set as void. // The setup function will only run once, after each powerup or reset of the Arduino board. void setup() // functions always have to start with a curly brace and end with a closing curly brace. { // Initiate serial communication between Arduiono and PC // Parameter is for baud rate (speed of transmiting data) 9600 is used often as default . Serial.begin(9600); // Servo motor works only PWM (Pulse-width modulation) pins on Arduiono // Assign a pin which is connected to the servo myServo.attach(9); } // as the name suggests, the loop function runs consecutively. void loop(){ // Check if the Serial port received new data if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); // send value between 0 and 180 to the servo motor if (incomingByte > 4) { myServo.write(incomingByte); } // pause this program for a certain milisecond //delay(100); } }