Jumat, 29 Mei 2015

Aplikasi yang digunakan untuk menghidupkan danmematikan 3 buah LED

Asalammualaikum Wr.Wb

Selamat datang kembali sahabat mardianto, kali ini saya akan membuat :::::::
Sebuah aplikasi yang digunakan untuk menghidupkan dan
mematikan 3 buah LED yang ada pada Arduino. Pengaturan komunikasi
serial dapat dilakukan langsung pada aplikasi tanpa harus mengubah
program aplikasi.



ini adalah bentuk codingannya yang pertama ::::


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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int a = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(a==1)
            {
                button1.Text = "Connect";
                serialPort1.Close();
                a = 0;
                this.Close();
            }
            else
            {
                button1.Text = "Disconnect";
                serialPort1.PortName = comboBox1.Text;
                serialPort1.BaudRate = Convert.ToInt16(comboBox2.Text);
                serialPort1.Open();
                a = 1;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "On")
            {
                button2.Text = "Off";
                serialPort1.Write("1");

            }
            else
            {
                button2.Text = "On";
                serialPort1.Write("0");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text == "On")
            {
                button3.Text = "Off";
                serialPort1.Write("3");

            }
            else
            {
                button3.Text = "On";
                serialPort1.Write("2");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (button4.Text == "On")
            {
                button4.Text = "Off";
                serialPort1.Write("5");

            }
            else
            {
                button4.Text = "On";
                serialPort1.Write("4");
            }
        }
    }
}








dan ini codingan yang ke dua :::

 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;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        int a = 0;
        string rx;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (a == 1)
            {
                button1.Text = "Connect";
                serialPort1.Close();
                a = 0;
                this.Close();
            }
            else
            {
                button1.Text = "Disconnect";
                serialPort1.PortName = comboBox1.Text;
                serialPort1.BaudRate = Convert.ToInt16(comboBox2.Text);
                serialPort1.Open();
                a = 1;
            }
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
          
            rx = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(tampil));
        }
        void tampil(object sender, EventArgs e)
        {
            textBox1.AppendText(rx);
          
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text == "0")
            {
              
                pictureBox1.Image = WindowsFormsApplication2.Resource1.mati;
                textBox1.Clear();
              
            }
            else if (textBox1.Text == "1")
            {
              
                pictureBox1.Image = WindowsFormsApplication2.Resource1.hidup;
                textBox1.Clear();
              
            }
            else if (textBox1.Text == "2")
            {

                pictureBox2.Image = WindowsFormsApplication2.Resource1.mati;
                textBox1.Clear();

            }
            else if (textBox1.Text == "3")
            {

                pictureBox2.Image = WindowsFormsApplication2.Resource1.hidup;
                textBox1.Clear();

            }
            else if (textBox1.Text == "4")
            {

                pictureBox3.Image = WindowsFormsApplication2.Resource1.mati;
                textBox1.Clear();

            }
            else if (textBox1.Text == "5")
            {

                pictureBox3.Image = WindowsFormsApplication2.Resource1.hidup;
                textBox1.Clear();

            }
        }
    }
}


Baiklah untuk lebih tau cara kerja programnya silahkan kunjungi....


https://youtu.be/Hg9t5w6vJ4g

Tidak ada komentar:

Posting Komentar