Controlling Servo / Led Brightness with C++

Hey guys, im fairly new to Arduino, and trying to interface it with Visual Studio C++

ive written a simple program with 2 buttons, one to turn off the LED, and one to turn on the LED.

the program sends a String to the arduino, which receives it and depending whether its L (on) or H (off) does w.e with it.

now im trying to implement a trackbar in the Form, with a value from 1-255, to control the PWM frequency on the arduino pin, hence changing the brightness of the LED.

the C++ code is as follows:
private: System::Void trackBar1_Scroll(System::Object^ sender, System::EventArgs^ e) {
trackBar1->Minimum = 1;
trackBar1->Maximum = 255;
this->Arduino->Open();
this->Arduino->Write(trackBar1->Value);
this->Arduino->Close();

}

but VS wont let me compile, giving the following error:
error C2664: 'void System::IO::Ports::SerialPort::Write(System::String ^)' : cannot convert parameter 1 from 'int' to 'System::String ^'
1> No user-defined-conversion operator available, or
1> No standard conversion exists from the boxed form of the arithmetic type to the target type

does anyone know how to send the int value through the serial port?

thanks for any help in advance :slight_smile:

A PC int is usually bigger than an arduino int, and would be sent as four bytes

It means that you need to convert the int that you get from trackBar1->Value to a string before you can call the Write function with the value:

char buffer[10];
sprintf(buffer, "%i", trackBar1->Value);
this->Arduino->Write(buffer);

oh, thank you so much! :smiley:

cant compile that either :confused: error:

convert parameter 1 from 'char [10]' to 'System::String ^'
1> Reason: cannot convert from 'char *' to 'System::String ^'
1> No user-defined-conversion operator available, or
1> Cannot convert an unmanaged type to a managed type

So, make it a String, not a char array

how?

Well, this isn't an arduino question, but I'd check the methods in System::String