![]() |
|
[C#] Using VS to communicate with MicroController(Arduino) - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Using VS to communicate with MicroController(Arduino) (/showthread.php?tid=2051683) |
Using VS to communicate with MicroController(Arduino) - Chappie - Dec 26, 2011 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 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()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()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 : FormNow in our Form load we want to connect and open our Port for Communiction. Code private void Form1_Load(object sender, EventArgs e)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)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. * - pr0t0typ3 - Dec 26, 2011 LOL ! My brother just got one of these "chips" for christmas and is testing different things with it :D Nice tutorial ;) * - Chappie - Dec 26, 2011 (Dec 26, 2011 06:28 PM)pr0t0typ3 Wrote: LOL ! My brother just got one of these "chips" for christmas and is testing different things with it :D Yeah, Micro-controllers are very fun to mess around with... You can also do ALOT with them.. I have friends who have made Paint Ball turrets with cameras that detect movement and shoot at them, all using the Arduino platform. But I hope he might find use of this Tut :D * - Exemplification - Dec 27, 2011 Are you sure that is Visual Basic? Looks like C# to me :D * - TomTheBeast - Dec 27, 2011 Yea, that really does look like C#. * - Chappie - Dec 28, 2011 (Dec 27, 2011 09:09 PM)Wiggles Wrote: Are you sure that is Visual Basic? My apologies, I'm used to referring to any language in visual studio as VB. I meant, this is C# in Visual Studio :P |