0% found this document useful (0 votes)
100 views

Connect To The Arduino With C

This document summarizes a project connecting an Arduino board to a Windows computer program using C#. The C# program displays a GUI with buttons to turn an LED on the Arduino on and off. When the user enters an email address and clicks send, the program emails the current state of the LED to that address. The Arduino code receives serial commands of 1 and 0 to turn the LED on and off. The C# code uses serial communication to control the Arduino and includes functions for the button clicks and sending email.

Uploaded by

Jose Luna
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views

Connect To The Arduino With C

This document summarizes a project connecting an Arduino board to a Windows computer program using C#. The C# program displays a GUI with buttons to turn an LED on the Arduino on and off. When the user enters an email address and clicks send, the program emails the current state of the LED to that address. The Arduino code receives serial commands of 1 and 0 to turn the LED on and off. The C# code uses serial communication to control the Arduino and includes functions for the button clicks and sending email.

Uploaded by

Jose Luna
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Connect to the Arduino with C#

Last Updated on Tuesday, 19 February 2013 21:46 Written by Administrator Sunday, 13 September 2009 12:12 One of the coolest things about the Arduino environment is its compatibility with almost anything. After doing a few projects using only the board and some other components, I started to realise the limitations of only using such a hardware based environment. Being able to make a robot was one thing, but wouldnt it be much cooler to be able to, say, email you when something happened or control it from a computer program? That is what Ive done on this project. Using an Arduino board, Visual C# and a tutorial from the Microsoft 'Coding for Fun' site, Ive made a simple program thats controlled from a Windows Form. The user can turn the LED on an Arduino board on and off, then e-mail the state of it to their e-mail address. Its a proof of concept project, that could hopefully be applied to more useful things. Before going on to explaining the program, Id like to say a bit about what I think is the coolest platform for this sort of thing - C#. I spent hours trying to send e-mails using command prompt and php scripts, but C# deals with all the tricky low level coding so its a lot easier to make much cooler programs. For home robotics projects, Id recommend C# to absolutely anyone. Right then, on to the project. Below is the form as it would appear when it first opens. The user can press the On button, which would then turn the LED on the arduino board on.

Once the On button has been pressed, the Off button becomes pressable:

And once the Off button is pressed and an e-mail address is entered:

Arduino Code
void setup() { pinMode(13, OUTPUT); Serial.begin(9600); } void loop() { if(Serial.available()) { int c = Serial.read(); if (c == '1') { digitalWrite(13,HIGH); } else if (c == '0') { digitalWrite(13,LOW); }

} }

C# Code
I'd recommend putting in some error checking if this were a real project, which I haven't done here. The two main areas for checking are the e-mail address and the connection to the serial port. using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms; System.IO.Ports;

namespace SerialCommunication { public partial class Form1 : Form { bool ledState = false; public Form1() { InitializeComponent(); serialPort1.PortName = "COM4"; serialPort1.BaudRate = 9600; serialPort1.Open(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) serialPort1.Close(); } private void buttonOn_Click(object sender, EventArgs e) { serialPort1.Write("1"); textBox1.Text = "LED is on!"; buttonOn.Enabled = false; buttonOff.Enabled = true; ledState = true; }

private void buttonOff_Click(object sender, EventArgs e) { serialPort1.Write("0"); textBox1.Text = "LED is off!"; buttonOn.Enabled = true; buttonOff.Enabled = false; ledState = false; } private void send_mail(string address) { System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add(address); message.Subject = "Hi there."; message.From = new System.Net.Mail.MailAddress(" you@example.com"); if (ledState == true) { message.Body = "The LED is on"; } if (ledState == false) { message.Body = "The LED is off"; } System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.ukgateway.net"); smtp.Send(message); } private void buttonSend_Click(object sender, EventArgs e) { send_mail(textBox_mail.Text); } } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy