Bilgisayar arayüzünü orjinal konu ekinden indirebilirsiniz. Arayüzdeki butonlara tıklayarak çıkıştaki ledleri ya da röleleri kontrol edebilirsiniz. Devre mevcut hali ile led çıkışlı tasarlanmıştır. Arduinonuz için bir shild tasarlayarak röle çıkışlı hale getirebilirsiniz.



scriptaccess" value="always">



C# kodları:
description">Kod:
 using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ON_OFF // Uygulamamızın Adı
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
            
        }

       /* 
       Aşağıdaki kod dizisinde yaptığımız resimlere tıklandığında serial üzerinden ilgili buton resmi için ilgili arduino                pinini tetikleyecek bir String değer göndermek. Aynı zamanda görsellik için her resme tıkladığımızda resimin                    değişip off yada on resminin açılması. Bunuda picture.Box ların görünürlüğünü kontrol eden "visible" özelliği ile            yaptık.Aşağıdaki "pictureBox1_Click" kısmında Serialden String "1" verisi göndererek 1 nolu ledin yanması                      sağlanacaktır.
       */

        private void pictureBox1_Click(object sender, EventArgs e) 
        {
            pictureBox1.Visible = false;
            pictureBox2.Visible = true;
            serialPort1.Write("1");
            
        }

        /*
            Aşağıdaki "pictureBox2" ise ilk butonun ON resmidir.  "pictureBox2_Click" ile arduinodaki ilk lede string            "2" gönderilerek ledin kapanması sağlanır. Yine bu tıklama işlemi ile resim değişerek ilk resim OFF ortaya çıkarak             ON resmi kaybolur.
        */

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            pictureBox1.Visible = true;
            pictureBox2.Visible = false;
            serialPort1.Write("2");
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < System.IO.Ports.SerialPort.GetPortNames().Length; i++)
            {
                comboBox1.Items.Add(System.IO.Ports.SerialPort.GetPortNames()[i]);
            }
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            pictureBox3.Visible = false;
            pictureBox4.Visible = true;
            serialPort1.Write("3");
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            pictureBox3.Visible = true;
            pictureBox4.Visible = false;
            serialPort1.Write("4");
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            pictureBox5.Visible = true;
            pictureBox6.Visible = false;
            serialPort1.Write("6");
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            
            pictureBox5.Visible = false;
            pictureBox6.Visible = true;
            serialPort1.Write("5");
        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            pictureBox7.Visible = false;
            pictureBox8.Visible = true;
            serialPort1.Write("7");
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            pictureBox7.Visible = true;
            pictureBox8.Visible = false;
            serialPort1.Write("8");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = comboBox1.Text;
            label6.Text = "Arduino'ya Bağlandı (" + comboBox1.Text + ")";
            serialPort1.Open();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label6.Text = "COM Port Seç !";
            serialPort1.Close();
        }
       
    }
}
Arduino kodları:
Kod:
 int gelen;
int Led1=4;
int Led2=5;
int Led3=6;
int Led4=7;

void setup() {

Serial.begin(9600);
pinMode(Led1,OUTPUT);
pinMode(Led2,OUTPUT);
pinMode(Led3,OUTPUT);
pinMode(Led4,OUTPUT);
}
void loop() 
{
  if(Serial.available() >0)  // Serial haberleşmenin aktif olup olmadığının kontrolü
    { 
      gelen = Serial.read(); // Serialden gelen veri "gelen" değişkenine yazılır
      if(gelen == '1')            // Gelen verilerin kontrolü yapılarak ilgili ledler yakılır.
      {
        digitalWrite(Led1,HIGH);
      }
      if(gelen == '2')
      {
        digitalWrite(Led1,LOW);
      }
      if(gelen == '3')
      {
        digitalWrite(Led2,HIGH);
      }
      if(gelen == '4')
      {
        digitalWrite(Led2,LOW);
      }
      if(gelen == '5')
      {
        digitalWrite(Led3,HIGH);
      }
      if(gelen == '6')
      {
        digitalWrite(Led3,LOW);
      }
      if(gelen == '7')
      {
        digitalWrite(Led4,HIGH);
      }
      if(gelen == '8')
      {
        digitalWrite(Led4,LOW);
      }
    }
}
Kodlama ve arayüz tasarımı: Robot projeleri mvaslan.blogspot.com.tr

Fritzing çizimi:
Fritzing dosyasını orjinal konu ekinden indirebilirsiniz.


Kaynak: C# ile Arduino Led - Röle Kontrolü