With this example you will display the angle of a servo into a LCD 2x16.
It uses the Servo, servo.h
, library to control the servo and the Liquidcrystal, liquidcrystal.h
, library to display the angle on the LCD.
This example is already in biicode. You can create your project and open the block or copy the code into a .cpp file:
$ bii init my_arduino_project
$ cd my_arduino_project
$ bii open examples/servolcd
Check the code inside your block’s folder:
lcd_and_servo.cpp
#include <Arduino.h>
#include <LiquidCrystal.h>
#include <Servo.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myservo;
void write_angle_lcd(int pos){
lcd.setCursor(0, 1);
lcd.print(pos);
lcd.print(" ");
}
void setup(){
myservo.attach(10);
lcd.begin(16, 2);
lcd.print("SERVO ANGLE");
lcd.setCursor(0, 1);
}
void loop(){
int pos;
for(pos = 0; pos <= 180; pos += 1){
myservo.write(pos);
write_angle_lcd(pos);
delay(50);
}
for(pos = 180; pos >= 0; pos -= 1){
myservo.write(pos);
write_angle_lcd(pos);
delay(50);
}
}
First, we have to configure the settings and select the arduino toolchain:
$ bii arduino:settings
...
$ bii configure -toolchain=arduino
...
Secondly, build this c++ example for arduino:
$ bii build
And upload it to your board:
$ bii arduino:upload
You are done! You’ll see your servo moving and the angle into the LCD.
Any doubts? Do not hesitate to contact us, visit our forum and feel free to ask any questions.