Tuesday, May 26, 2015

Serial Port untuk menghidupkan dan mematikan 3 buah LED yang ada pada Arduino

Assalamalaikum wr.wb.

bailklah disini saya akan memberikan sedikit ilmu tentang membuat aplikasi sederhana untuk mengidupkan dan mematikan lampu LED pada arduino menggunakan visual studio.
baiklah aplikasi yang digunakan yaitu:
1.visual studio
2.Arduino
3.hyperterminal
4.virtual serial port driver

>>cara menggunakan hyperterminal:
setting COM berlawanan dengan COM yang terdapat pada Visual Studio dan setting juga baudrate nya menjadi sama sama 9600 agar data yang dikirim dan diterima menjadi sinkron. lalu klik Connect.




>>ini adalah program utuhnya:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace Kontrol_3_led
{
    public partial class Form1 : Form
    {
        Image biru = Properties.Resources.biru;
        Image putih = Properties.Resources.putih;

        public Form1()
        {
            InitializeComponent();
           
            pictureBox1.BackgroundImage = putih;
            pictureBox2.BackgroundImage = putih;
            pictureBox3.BackgroundImage = putih;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.Open();
            comboBox1.Items.Add("COM1");
        }

        private void on1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("1");
                pictureBox1.BackgroundImage = biru;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);      
            }
        }

        private void off1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("0");
                pictureBox1.BackgroundImage = putih;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            Application.Exit();
        }

        private void kirim_Click(object sender, EventArgs e)
        {
            serialPort1.Write(textBox1.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (serialPort1.BytesToRead != 0)
            {
                textBox2.Text = serialPort1.ReadExisting();
            }
        }

        private void on2_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("2");
                pictureBox2.BackgroundImage = biru;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
             
            }
        }

        private void off2_Click(object sender, EventArgs e)
        {

            try
            {
                serialPort1.Write("3");
                pictureBox2.BackgroundImage = putih;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void on3_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("4");
                pictureBox3.BackgroundImage = biru;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        private void off3_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Write("5");
                pictureBox3.BackgroundImage = putih;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
         
        }
     
    }
}

No comments:

Post a Comment