效用:控制伺服馬達由20到160度往復旋轉,以10度為一個移動單位,每移動一個單位停留0.5秒,並將當前的角度位置顯示在液晶顯示器上。
應用:機械手臂。
電路接法:
圖形程式方塊:
程式碼:
#include <Servo.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
int _ABVAR_1_variablename = 0 ;
Servo servo_pin_5;
LiquidCrystal_I2C lcd_I2C_27(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
void setup()
{
servo_pin_5.attach(5);
lcd_I2C_27.begin (16, 2);
lcd_I2C_27.setBacklight(HIGH);
}
void loop()
{
for(_ABVAR_1_variablename = 20;
20<=160?_ABVAR_1_variablename <= 160:_ABVAR_1_variablename >= 160;
20<=160?_ABVAR_1_variablename = _ABVAR_1_variablename + 10:_ABVAR_1_variablename = _ABVAR_1_variablename - 10)
{
servo_pin_5.write( _ABVAR_1_variablename );
lcd_I2C_27.setCursor( (1) - 1, (1) - 1 );
lcd_I2C_27.print( "Current ang:" );
lcd_I2C_27.print( _ABVAR_1_variablename );
lcd_I2C_27.setCursor( (1) - 1, (2) - 1 );
lcd_I2C_27.print( "Round trip" );
delay( 500 );
lcd_I2C_27.setCursor( (1) - 1, (1.) - 1 );
lcd_I2C_27.print( "---------------" );
}
delay( 1000 );
for(_ABVAR_1_variablename = 160;
160<=20?_ABVAR_1_variablename <= 20:_ABVAR_1_variablename >= 20;
160<=20?_ABVAR_1_variablename = _ABVAR_1_variablename + 10:_ABVAR_1_variablename = _ABVAR_1_variablename - 10)
{
servo_pin_5.write( _ABVAR_1_variablename );
lcd_I2C_27.setCursor( (1) - 1, (1) - 1 );
lcd_I2C_27.print( "Current ang:" );
lcd_I2C_27.print( _ABVAR_1_variablename );
delay( 500 );
lcd_I2C_27.setCursor( (1) - 1, (1) - 1 );
lcd_I2C_27.print( "----------------" );
}
delay( 1000 );
}
沒有留言:
張貼留言