Program in C# on PC to control arduino

Thread Starter

jakethehusky

Joined Mar 24, 2016
22
I need help programming an Arduino that needs to control a rgb-led strip (pwm port 9 - R, pwm port 10 - G, pwm port 11 - B), The circuit is working just fine. But I am having trouble sending information to the Arduino, what's the best way to sent a number from 0 to 255 to control one of the pwm ports.

I am using visual studio.

Thanks in advance
Jake
 

WBahn

Joined Mar 31, 2012
30,060
The main thing you have to achieve is the ability for your program to influence something in the real world to begin with -- that means you need some kind of hardware and the necessary library files to give you an API to it from C#. There are a number of way to go about this. Probably the two most common would be a USB-based device or a Ethernet-based device.
 

mcgyvr

Joined Oct 15, 2009
5,394
Is the arduino connected to the computer via USB (serial)?

The simple way (without any visual studio coding,etc..) is to use the serial monitor and send the data to the arduino that way..
You can have the arduino prompt for user input and read the response and act accordingly..

Or write your own program to communicate to it the same way..
 

Thread Starter

jakethehusky

Joined Mar 24, 2016
22
Is the arduino connected to the computer via USB (serial)?

The simple way (without any visual studio coding,etc..) is to use the serial monitor and send the data to the arduino that way..
You can have the arduino prompt for user input and read the response and act accordingly..

Or write your own program to communicate to it the same way..
I can do that no problem, but me and my friend made a little program to control the Arduino.

2283e3d455012fce7ec40310a60f03af.png
Screenshot - https://gyazo.com/2283e3d455012fce7ec40310a60f03af

This is made in visual studio and it works for now, pressing a button well send a letter to the Arduino which is looping "char data = serial.read()" and than a bunch of if statements example:"if (data == a) {CODE}". The only other thing we've achieved is sending a number from 0 to 9.
We use this:
Code:
int GreenChange = Convert.ToInt32(slrGReen.Value);
txtGreen.Text = Convert.ToString(GreenChange);
myport.WriteLine("h");
myport.WriteLine(txtGreen.Text);


To send the number and a letter and read it in the Arduino like this:

if (data == 'h') {
BrightGreen = Serial.parseInt() * 25;
analogWrite(GREEN, BrightGreen);
Using this method we can send a number from 0 to 9 and we multiply it by 25 since the max number is 255. and the difference in the rgb lights isn't really noticeable above 200. GREEN in this scenario is pin 10.

But I would like to find a way to send a completely accurate 0 to 255 number using the slider in my program. The only way I can think of now is the send letters ABBBAABABABAA with A being 0 and B being 1 so I can send a binary number, but this would probably glitch out .
 

mcgyvr

Joined Oct 15, 2009
5,394
So whats stopping you from sending numbers greater than 9?
Set the slider to output 0 as the min and 255 as max and receive that in the arduino
 

Thread Starter

jakethehusky

Joined Mar 24, 2016
22
it sends it separately Arduino will then see three lines of code, "2","2","5" I could build a converter for this in the Arduino program.
 

mcgyvr

Joined Oct 15, 2009
5,394
it sends it separately Arduino will then see three lines of code, "2","2","5" I could build a converter for this in the Arduino program.
You just need to do a
Code:
while (Serial.available()>0) {
greenval = Serial.parseInt();
}
or similar depending on the need..

Here.. watch this..
 
Top