Dec 26, 2011 06:08 PM
(This post was last modified: Dec 28, 2011 05:19 PM by Chappie.)
Hey guys, I decided to make this tutorial because I often see people on Youtube asking how exactly this is done. Alot of people enjoy using the Arduino but one problem is some people want to be able to control the Arduino rather then have it run through endless loops.
This is VERY easy and all it is, is pretty much communicating through serial ports. This can be used for things such as creating a program control panel to turn lights on/off, change a motor speed or simply control a robot you made.
Before we get into visual studio we must set up the Arduino. So First in the Arduino program, you are going to want to create a Int value for each byte you will be receiving.
So at the top before setup create your integer
Next you are going to want to start your Serial in the setup void, So create the void and then code in the following.
Once that is done you are almost finished allowing your Arduino to receive data. Next in you're void loop you are going to want this.
Now that that is all setup, we are going to move onto Visual Studio... I will be using C# in this example, but i assume it wont be hard to figure out if you are using a different language.
First we are going to want to create our Windows Form program and name it what ever you want, I will name mine "ArduinoApp".
We are going to want to double click on the Form and add some things to get it set up to our Serial port.
We are then going to add the some references at the top so we can access our Serial port.
*If you are using normal .Net language then you will most likely need to do 'Import' rather then 'using'.
Once that is done we are now going to want to declare our Serial port as 'com3'(Or what ever name you want it to have) in our class.
Now in our Form load we want to connect and open our Port for Communiction.
Now that we are connected to our Port we are going to want to send it data correct? Well here is our last step!
Now you can create a button or anything really, and write the signal to your port. I chose to use a button.
So now, when i open my program I can send the byte "1" to my Arduino and we have our Arduino coded to act upon receiving a byte valued as the string "1".
I will give you my Source codes I am using for anyone interested in seeing it in more detail. this source controls Audio frequency's to play tunes, as well as a "Funky Town" beat with VB controls.
Download: http://www.mediafire.com/?sjr9z6fpgxzv8zv
I know i already posted this earlier, but I figured it would be better off here then in gadgets section.
This is VERY easy and all it is, is pretty much communicating through serial ports. This can be used for things such as creating a program control panel to turn lights on/off, change a motor speed or simply control a robot you made.
Before we get into visual studio we must set up the Arduino. So First in the Arduino program, you are going to want to create a Int value for each byte you will be receiving.
So at the top before setup create your integer
Code
int incomingbyte;Next you are going to want to start your Serial in the setup void, So create the void and then code in the following.
Code
void setup()
{
pinMode(speakerPin, OUTPUT); // This can be ignored
Serial.begin(9600); // This is what you want
}Once that is done you are almost finished allowing your Arduino to receive data. Next in you're void loop you are going to want this.
Code
void loop()
{
if(Serial.available() > 0) // This checks if it is receiving any bytes
{
incomingbyte = Serial.read(); // This is creating our integer to obtain the bytes value.
if(incomingbyte == '1') // In VB I sent the [u]STRING[/u] 1, This is NOT a integer value, hence the reason of the semi quote.
{
playkey(note1); // This can be ignored because it is playing a void in my source that plays an audio frequency. but here is where you will put what will happen if the 'value' is true.
}
}
}Now that that is all setup, we are going to move onto Visual Studio... I will be using C# in this example, but i assume it wont be hard to figure out if you are using a different language.
First we are going to want to create our Windows Form program and name it what ever you want, I will name mine "ArduinoApp".
We are going to want to double click on the Form and add some things to get it set up to our Serial port.
We are then going to add the some references at the top so we can access our Serial port.
Code
using System.IO.Ports;Once that is done we are now going to want to declare our Serial port as 'com3'(Or what ever name you want it to have) in our class.
Code
public partial class Form1 : Form
{
[u]SerialPort com3 = new SerialPort();[/u]Now in our Form load we want to connect and open our Port for Communiction.
Code
private void Form1_Load(object sender, EventArgs e)
{
/* This is all Optional for people who don't want to get confused.
com3.BaudRate = 9600;
com3.DataBits = 8;
com3.Parity = Parity.None;
com3.StopBits = StopBits.One;
com3.Handshake = Handshake.None;
*/
com3.PortName = "COM3"; // This is my port, you may have a different one. You can check this by going to your Arduino program clicking 'Tools' on the tool bar and then going to 'Serial Port'.
com3.Encoding = System.Text.Encoding.Default; //This is important for clear communication.
com3.Open(); // This opens the port for the program.
com3.RtsEnable = true; // this allows RTS signal to be sent.
}Now that we are connected to our Port we are going to want to send it data correct? Well here is our last step!
Now you can create a button or anything really, and write the signal to your port. I chose to use a button.
Code
private void button1_Click(object sender, EventArgs e)
{
com3.Write("1");
}So now, when i open my program I can send the byte "1" to my Arduino and we have our Arduino coded to act upon receiving a byte valued as the string "1".
I will give you my Source codes I am using for anyone interested in seeing it in more detail. this source controls Audio frequency's to play tunes, as well as a "Funky Town" beat with VB controls.
Download: http://www.mediafire.com/?sjr9z6fpgxzv8zv
I know i already posted this earlier, but I figured it would be better off here then in gadgets section.


![[Image: tkg318I.png]](https://i.imgur.com/tkg318I.png)
