Professional Documents
Culture Documents
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod;
int servoa2, servob2, servoc2, servod2; //vị trị hiện tại
int servoa1, servob1, servoc1, servod1; // vị trí trước đó
int servoa[50], servob[50], servoc[50],servod[50];
int speeddelay= 10;
int index=0;
String data ="";
void setup()
{
Serial.begin(9600);
myservoa.attach(3);
myservob.attach(5);
myservoc.attach(6);
myservod.attach(9);
servoa1=90;
servob1=0;
servoc1=120;
servod1=50;
myservoa.write(servoa1);
myservob.write(servob1);
myservoc.write( servoc1);
myservod.write(servod1);
}
void loop()
{
if (Serial.available()>0)
{
data = Serial.readString();
if (data.startsWith("sa"))
{
String dataIns= data.substring(2, data.length());
servoa1 = dataIns.toInt();
if (servoa2 < servoa1)
{
for (int i = servoa2; i < servoa1; i++)
{
myservoa.write(i);
delay(10);
}
}
else
{
if (servoa2 > servoa1)
{
for (int i = servoa2; i > servoa1; i--)
{
myservoa.write(i);
delay(10);
}
}
}
servoa2 = servoa1;
}
/////////////////////////////////////////////////////////////////////////////
/
if (data.startsWith("sb"))
{
String dataIns= data.substring(2, data.length());
servob1 = dataIns.toInt();
if (servob2 < servob1)
{
for (int i = servob2; i < servob1; i++)
{
myservob.write(i);
delay(10);
}
}
else
{
if (servob2 > servob1)
{
for (int i = servob2; i > servob1; i--)
{
myservob.write(i);
delay(10);
}
}
}
servob2 = servob1;
}
///////////////////////////////////////////////////////////////////////
if (data.startsWith("sc"))
{
String dataIns= data.substring(2, data.length());
servoc1 = dataIns.toInt();
if (servoc2 < servoc1)
{
for (int i = servoc2; i < servoc1; i++)
{
myservoc.write(i);
delay(10);
}
}
else
{
if (servoc2 > servoc1)
{
for (int i = servoc2; i > servoc1; i--)
{
myservoc.write(i);
delay(10);
}
}
}
servoc2 = servoc1;
}
/////////////////////////////////////////////////////
if (data.startsWith("sd"))
{
String dataIns= data.substring(2, data.length());
servod1 = dataIns.toInt();
if (servod2 < servod1)
{
for (int i = servod2; i < servod1; i++)
{
myservod.write(i);
delay(10);
}
}
else
{
if (servod2 > servod1)
{
for (int i = servod2; i > servod1; i--)
{
myservod.write(i);
delay(10);
}
}
}
servod2 = servod1;
}
if (data.startsWith("SAVE"))
{
servoa[index] = servoa2; // lưu vị trí vào một mảng
servob[index] = servob2;
servoc[index] = servoc2;
servod[index] = servod2;
index++;
}
if (data.startsWith("RUN")) {
runservo(); // Automatic mode - run the saved
steps
}
if ( data == "RESET") {
memset(servoa, 0, sizeof(servoa)); // Clear the array
data to 0
memset(servob, 0, sizeof(servob));
memset(servoc, 0, sizeof(servoc));
memset(servod, 0, sizeof(servod));
index = 0; // Index to 0
}
}
}
void runservo () {
while (data != "RESET") { // Run the steps over and
over again until "RESET" button is pressed
for (int i = 0; i <= index - 2; i++) { // Run through all
steps(index)
if (Serial.available() > 0) { // Check for
incomding data
data = Serial.readString();
if ( data == "PAUSE") { // If button
"PAUSE" is pressed
while (data != "RUN") { // Wait until
"RUN" is pressed again
if (Serial.available() > 0) {
data= Serial.readString();
if ( data == "RESET") {
break;
}
}
}
}
// If speed slider is changed
if (data.startsWith("ss")) {
String dataIns = data.substring(2, data.length());
speeddelay = dataIns.toInt(); // Change servo
speed (delay time)
}
}
// Servo 1
if (servoa[i] == servoa[i + 1]) {
}
if (servoa[i] > servoa[i + 1]) {
for ( int j = servoa[i]; j >= servoa[i + 1]; j--) {
myservoa.write(j);
delay(speeddelay);
}
}
if (servoa[i] < servoa[i + 1]) {
for ( int j = servoa[i]; j <= servoa[i + 1]; j++) {
myservoa.write(j);
delay(speeddelay);
}
}
// Servo 2
if (servob[i] == servob[i + 1]) {
}
if (servob[i] > servob[i + 1]) {
for ( int j = servob[i]; j >= servob[i + 1]; j--) {
myservob.write(j);
delay(speeddelay);
}
}
if (servob[i] < servob[i + 1]) {
for ( int j = servob[i]; j <= servob[i + 1]; j++) {
myservob.write(j);
delay(speeddelay);
}
}
// Servo 3
if (servoc[i] == servoc[i + 1]) {
}
if (servoc[i] > servoc[i + 1]) {
for ( int j = servoc[i]; j >= servoc[i + 1]; j--) {
myservoc.write(j);
delay(speeddelay);
}
}
if (servoc[i] < servoc[i + 1]) {
for ( int j = servoc[i]; j <= servoc[i + 1]; j++) {
myservoc.write(j);
delay(speeddelay);
}
}
// Servo 4
if (servod[i] == servod[i + 1]) {
}
if (servod[i] > servod[i + 1]) {
for ( int j = servod[i]; j >= servod[i + 1]; j--) {
myservod.write(j);
delay(speeddelay);
}
}
if (servod[i] < servod[i + 1]) {
for ( int j = servod[i]; j <= servod[i + 1]; j++) {
myservod.write(j);
delay(speeddelay);
}
}
}
}
}
3. PHẦN CỨNG :
-Gỗ
-Dây Jumper : Được sử dụng với Breadboard để kết nối các
module và linh kiện điện tử với nhau
-Bo mạch test (Breadboard): Dùng để cắm linh kiện
-Nút nhấn: Dùng để dạy lệnh cho cánh tay rôbôt
-Servo SG 90 :Cung cấp lực chuyển động cần thiết cho các thiết
bị máy móc khi vận hành
-Arduino Atemega 328 : Dùng để xử lý và điều khiển
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
delegate void setTextCallback(string s);
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
label11.Text = DateTime.Now.ToLongTimeString();
}
private void Form1_Load(object sender, EventArgs e)
{
Init_Port_Confs();
serialPort.ReadTimeout = 1000;
serialPort.Close();
DateTime time_d = DateTime.Now;// ngày tháng năm
hiện tại trong máy tính
label10.Text =
time_d.ToString("dd/MM/yyyy");//cập nhật thời gian ngày
tháng năm hiện tại vào label10
timer1.Start();
//timer2.Start();
label12.ForeColor = System.Drawing.Color.Red;
label12.Text = "Chưa Kết Nối";
string strSerialName =
comboBoxCom.SelectedItem.ToString();
string strBaudRate =
comboBoxBaudRate.SelectedItem.ToString();
Int32 iBaudRate =
Convert.ToInt32(strBaudRate);// chuyển kiểu dữ liệu của
strBaudRate từ chuỗi sang số và gán cho biến mới iBaudRate
serialPort.PortName =
strSerialName;//tên của serial Com đã chọn
serialPort.BaudRate = iBaudRate;//tốc độ
BaudRate của serial Com đã chọn
label12.ForeColor =
System.Drawing.Color.Blue;
label12.Text = "Đã kết nối";
ketnoi.Text = "Ngắt kết nối";//hiện chữ
"Ngắt kết nối" trên nút nhấn sau khi đã nhấn để mở Com nhận
dữ liệu từ arduino
ketnoi.BackColor =
System.Drawing.Color.Red;//màu cho nút Đóng COM
}
catch (System.Exception ex)// nếu trong Try
có lỗi thì catch sẽ giải quyết
{
MessageBox.Show("Error:" + ex.Message,
"Error");
return;
}
}
else // nếu com đang mở khi ta nhấn nút Ngắt kết
nối thì cổng Com sẽ đóng việc truyền dữ liệu
{
comboBoxCom.Enabled = true;
comboBoxBaudRate.Enabled = true;
}
}
private void Thoat_Click(object sender, EventArgs e)
{
DialogResult kq = MessageBox.Show("Bạn có chắc
chắn muốn thoát ?", "Điểu Khiển", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (kq == DialogResult.Yes)
{
Application.Exit();
}
}
if (textBox2.Text != String.Empty)
{
String khop1 = "sa"+ textBox2.Text ;
serialPort.Write(khop1);
}
else
{
MessageBox.Show("Chưa nhập giá trị",
"Error");
}
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
if (textBox4.Text != String.Empty)
{
String khop3 = "sc" + textBox4.Text;
serialPort.Write(khop3);
}
else
{
MessageBox.Show("Chưa nhập giá trị",
"Error");
}
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
private void button4_Click(object sender, EventArgs
e)
{
if (serialPort.IsOpen)
{
if (textBox5.Text != String.Empty)
{
String khop4 = "sd" + textBox5.Text;
serialPort.Write(khop4);
}
else
{
MessageBox.Show("Chưa nhập giá trị",
"Error");
}
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
if (serialPort.IsOpen)
{
textBox7.Text = trackBar2.Value.ToString();
String khop2 = "sb" + textBox7.Text;
serialPort.Write(khop2);
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
serialPort.Write("SAVE");
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
serialPort.Write("RUN");
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
serialPort.Write("PAUSE");
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
serialPort.Write("RESET");
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}
if (textBox10.Text != String.Empty)
{
String speed = "ss" + textBox10.Text;
serialPort.Write(speed);
}
else
{
MessageBox.Show("Chưa nhập giá trị",
"Error");
}
}
else
MessageBox.Show("Chưa kết nối cổng COM",
"Error");
}