Servo HS-55 :

Polohy serva :

Zapojenie konektora :

Zapojenie ARDUINO Nano a servo :

Kód arduino v cpp:
#include <Servo.h>
Servo servo1;
char inChar = 0;
String inputString = "";
boolean stringComplete = false;
int servoDegrees = 0;
void setup()
{
Serial.begin(9600);
servo1.attach(14); //analog pin 0
}
void loop()
{
if (stringComplete)
{
servoDegrees = inputString.toInt();
if (servoDegrees >= 0 && servoDegrees <= 180)
{
servo1.write(servoDegrees);
Serial.println(servoDegrees);
}
else
{
Serial.println("Bad value");
}
inputString = "";
stringComplete = false;
}
}
void serialEvent()
{
while (Serial.available() > 0)
{
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n')
{
stringComplete = true;
}
}
}
GUI aplikacia :

Kód v c# :
using System;
using System.IO.Ports;
using System.Windows.Forms;
namespace ServoGuiExample
{
public partial class Form1 : Form
{
private string selectedPortName = string.Empty;
private SerialPort mySerialPort = null;
public Form1()
{
InitializeComponent();
//Get all serial ports
this.comboBox1.Items.AddRange(SerialPort.GetPortNames());
}
private void button1_Click(object sender, EventArgs e)
{
this.selectedPortName = this.comboBox1.Text;
if (!string.IsNullOrEmpty(this.selectedPortName))
{
this.mySerialPort = new SerialPort(this.selectedPortName, 9600);
this.mySerialPort.Open();
}
}
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
if (this.mySerialPort != null && this.mySerialPort.IsOpen)
this.mySerialPort.WriteLine(trackBar1.Value.ToString());
}
}
}







