Kurs på MIT App Inventor och Arduino (2 / 5 steg)
Steg 2:3. Servo Motor kontroll med hjälp av Arduino och Android
I den här guiden kommer du att skapa en app för att styra en servomotor, du kommer att använda skjutreglaget i din app för att flytta din servo från 0-180, du behöver en servomotor att kopplas på arduino sida, se till att du också ansluta extern strömförsörjning så att din Arduino inte startar om under denna process,
#include / / TX RX programvarubibliotek för bluetooth
#include / / servo bibliotek
Servo myservo; servo namn
int bluetoothTx = 10; Bluetooth tx till 10 stift
int bluetoothRx = 11; Bluetooth rx till 11 stift
SoftwareSerial bluetooth (bluetoothTx, bluetoothRx);
void setup()
{
myservo.attach(9); bifoga servo signalen tråd till pin 9
Konfigurera USB-seriell anslutning till dator
Serial.BEGIN(9600);
Installera Bluetooth seriell anslutning till android
Bluetooth.BEGIN(9600);
}
void loop()
{
Läsa från bluetooth och skriva till usb följetong
IF(Bluetooth.available() > 0) / / få nummer från bluetooth
{
int servopos = bluetooth.read(); Spara mottagna nummer till servopos
Serial.println(servopos); seriella print servopos aktuella nummer från bluetooth
myservo.write(servopos); roate servo vinkeln fick från android app
}
}