This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot-open:remotelab:sut:generalpurpose2:b7 [2020/04/28 16:57] – created pczekalski | en:iot-open:remotelab:sut:generalpurpose2:b7 [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| === Prerequisites === | === Prerequisites === | ||
| - | The servo is controlled using predefined, precise PWM timing. | + | You need to understand how a typical servomotor looks like and how it works.\\ |
| - | <code c> | + | The servo is controlled using predefined, precise PWM timing. |
| - | #include <Servo.h> | + | The library is '' |
| - | </code> | + | |
| + | According to the algorithm of your choice to implement your code, you may hard code rotations one by one or i.e. declare a table with rotation targets and then iterate over it, so familiarity with array operations in C is essential in this case. | ||
| === Scenario === | === Scenario === | ||
| - | In this scenario, you will rotate the servo to the 0 degrees, then 45, 90, 135 and 180 degrees counter wise, then 180, 90, 0, clockwise. Note - Arrow pointing left means servo is set to 0, pointing right is 180 degrees, and when 90 degrees, arrow points down. | + | In this scenario, you will rotate the servo to 0 degrees, then 45, 90, 135 and 180 degrees counter wise, then 180, 90, 0, clockwise. Note - Arrow pointing left means servo is set to 0, pointing right is 180 degrees, and when 90 degrees, arrow points down. |
| + | We use LCD to get feedback about the requested servo angle and to compare it with the result, but please note, it is just for information only and is not necessary to implement servo rotation. | ||
| + | |||
| + | <note warning> | ||
| === Result === | === Result === | ||
| Line 28: | Line 32: | ||
| <code c> | <code c> | ||
| #include < | #include < | ||
| - | |||
| </ | </ | ||
| == Step 2 == | == Step 2 == | ||
| - | Instantiate software controler component for the LCD display: | + | Define a servo management class: |
| <code c> | <code c> | ||
| - | LiquidCrystal_I2C lcd(0x3F, | + | ... |
| - | // | + | Servo servo; |
| + | ... | ||
| + | </code> | ||
| - | Adafruit_BMP280 bmp; | + | If you intend to implement your solution with rotation targets array, here is a hint on array declaration: |
| + | <code c> | ||
| + | ... | ||
| + | byte angles[] = {0, | ||
| + | ... | ||
| </ | </ | ||
| == Step 3 == | == Step 3 == | ||
| - | Declare a buffer | + | Instantiate software controller component |
| <code c> | <code c> | ||
| - | char buffer [11]; | + | ... |
| - | float pres; | + | LiquidCrystal_I2C lcd(0x3F, |
| + | //8 and 9 for a 20 chars and 4 line display | ||
| + | // | ||
| + | ... | ||
| </ | </ | ||
| == Step 4 == | == Step 4 == | ||
| - | Initialize | + | In the '' |
| <code c> | <code c> | ||
| ... | ... | ||
| Line 54: | Line 66: | ||
| lcd.backlight(); | lcd.backlight(); | ||
| lcd.home(); | lcd.home(); | ||
| + | lcd.print(" | ||
| ... | ... | ||
| - | | + | |
| - | { | + | |
| - | lcd.print(" | + | |
| - | delay(1000); | + | |
| - | }; | + | |
| - | | + | |
| - | bmp.setSampling(Adafruit_BMP280:: | + | |
| - | Adafruit_BMP280:: | + | |
| - | Adafruit_BMP280:: | + | |
| - | Adafruit_BMP280:: | + | |
| - | Adafruit_BMP280:: | + | |
| ... | ... | ||
| </ | </ | ||
| - | The BMP sensor address | + | Setting servo to the desired angle is as simple as calling '' |
| + | \\ | ||
| - | == Step 5 == | + | If you implement your solution using rotation targets array, here is a hint on how to implement |
| - | Read in the loop: | + | |
| <code c> | <code c> | ||
| ... | ... | ||
| - | | + | |
| + | { | ||
| ... | ... | ||
| </ | </ | ||
| - | '' | + | The '' |
| - | To convert float into the string with formatting use '' | + | |
| + | <note tip>Give it at least 2s gap between setting subsequent rotation target for the servo. Otherwise, you may be unable to reliably observe results via video stream.</ | ||
| + | |||
| + | == Step 5 == | ||
| + | Keep '' | ||
| <code c> | <code c> | ||
| ... | ... | ||
| - | sprintf(buffer," | + | void loop() |
| - | ... | + | { |
| - | | + | } |
| - | ... | + | |
| </ | </ | ||
| - | <note tip>'' | ||
| - | '' | ||
| === Result validation === | === Result validation === | ||
| - | Observe | + | Observe the arrow rotating |