Program in C# on PC to control arduino

Discussion in 'Programmer's Corner' started by jakethehusky, Mar 30, 2016.

  1. jakethehusky

    Thread Starter New Member

    Mar 24, 2016
    22
    0
    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
     
  2. WBahn

    Moderator

    Mar 31, 2012
    17,720
    4,788
    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.
     
  3. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,769
    969
    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..
     
  4. jakethehusky

    Thread Starter New Member

    Mar 24, 2016
    22
    0
    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 (Text):
    1.  
    2. int GreenChange = Convert.ToInt32(slrGReen.Value);
    3. txtGreen.Text = Convert.ToString(GreenChange);
    4. myport.WriteLine("h");
    5. myport.WriteLine(txtGreen.Text);
    6.  
    7.  
    8. To send the number and a letter and read it in the Arduino like this:
    9.  
    10. if (data == 'h') {
    11. BrightGreen = Serial.parseInt() * 25;
    12. analogWrite(GREEN, BrightGreen);
    13.  
    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 .
     
  5. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,769
    969
    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
     
  6. jakethehusky

    Thread Starter New Member

    Mar 24, 2016
    22
    0
    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.
     
  7. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,769
    969
    You just need to do a
    Code (Text):
    1. while (Serial.available()>0) {
    2. greenval = Serial.parseInt();
    3. }
    or similar depending on the need..

    Here.. watch this..
     
  8. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,004
    3,757
    Just send the value as an 8-bit unsigned Char value. 0-255
     
    ErnieM likes this.
Loading...