C# controlling PWM

Hello guys! I've justo bought my arduino and I have some problems with the communication between an interface made in C# and the arduino program. I wanna control the luminosity of a LED with a trackbar in the visual interface, but I don't know what it's wrong with the code. I'll thank you a lot if you could give me some help. I think it could be a problem with te values I'm sending to the arduino. I'm not sure if my write "0" is taken as a zero for the PWM control.

Thats the ARDUUINOO CODE:

int Csharp; // how bright the LED is

void setup() { // declare pin 9 to be an output: Serial.begin(9600); pinMode(9, OUTPUT); }

void loop() {

if(Serial.available() > 1) { Csharp= Serial.read(); Serial.println(Csharp); analogWrite(9, Csharp); } }

THAT'S THE C#CODE:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; //PERMITE EL USO DE LOS PUERTOS.

namespace PWM_LED { public partial class Form1 : Form { public Form1() { InitializeComponent(); serialPort1.Open(); //HABILITA EL SERIALPORT1 }

private void trackBar1_Scroll(object sender, EventArgs e) { int pwm; serialPort1.Write("pwm"); label2.Text = Convert.ToString(pwm); }

private void button1_Click(object sender, EventArgs e) { serialPort1.Write("255"); //PARA EL BOTON DE ENCENDIDO }

private void button2_Click(object sender, EventArgs e) { serialPort1.Write("0"); // PARA EL BOTON DE APAGADO }

 if(Serial.available() > 1)

should be

 if(Serial.available() > 0)

but also

 serialPort1.Write("255");

I know nothing about C#, does this send a string (I assume it does) or binary value. If it sends a string it will never work.

Likewise with

serialPort1.Write("pwm");

Why are you sending this to the Arduino?


Rob

Thanks! I've changed te condition in the arduino but, it doesn't work :-[ I've got kind of desperate cause I think the problem is what you mean about the type of the variable. however, I don't know how i can fix it :(

You haven't answred the other questions re the print in C#.


Rob

ok sorry, the pwm variable is the pwm=convert.toint32(trackbar1.value); but at this moment I've leave that cause i can't turnon/ off the LED with the PWM function putting a 255 for a high level and 0 for a low level.

The button1_Click() function IS sending a string. The Arduino is NOT expecting a string. Change the “255” to just 255.

Similarly, in button2_Click, change “0” to just 0.

In trackBar1_Scroll, you have a variable named pwm that you never value. Then, you are sending the string “pwm”. You need to set the variable pwm to the track bar’s position. Look in the Properties folder, or the intellisense list, to see what the track bar property of interest is, and set pwm to that value.

Then, get rid of the quotes in the write function call.

You need something like this:

Hello guys!
I've justo bought my arduino and I have some problems with the communication between an interface made in C# and the arduino program.
I wanna control the luminosity of a LED with a  trackbar in the visual interface, but I don't know what it's wrong with the code. I'll thank you a lot if you could give me some help.
I think it could be a problem with te values I'm sending to the arduino. I'm not sure if my write "0" is taken as a zero for the PWM control.

Thats the ARDUUINOO CODE:

char Csharp[4];   // 255 is received as '2','5','5' aka as a String

void setup()  {
 // declare pin 9 to be an output:
  Serial.begin(9600);
 pinMode(9, OUTPUT);
}

void loop()  {

  if(Serial.available() > 1)
{
 for(int i=0; i<4; i++){
Csharp[i]=Serial.read();
}
}

analogWrite(9,atoi(Csharp));


}

The for loop reads the string, and the atoi converts the string number to an int value that you can pass to analogWrite.

I´ve tried the thing you say but the code doesn't accept the 255 instead of "255". This is my code modified with your help, but Ican't make it works :(

the thing i'm trying to do is that:

http://vimeo.com/3726170

but the guy doesn't tell how the code is.

ARDUINO CODE:

int Csharp; // how bright the LED is

void setup() { // declare pin 9 to be an output: Serial.begin(9600); pinMode(9, OUTPUT); }

void loop() {

if(Serial.available() > 0) { Csharp=Serial.read(); analogWrite(9, Csharp); } }

C# CODE:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO.Ports; //PERMITE EL USO DE LOS PUERTOS.

namespace PWM_LED { public partial class Form1 : Form { public Form1() { InitializeComponent(); serialPort1.Open(); //HABILITA EL SERIALPORT1 }

private void trackBar1_Scroll(object sender, EventArgs e) { int pwm; pwm = Convert.ToInt32(trackBar1.Value); serialPort1.Write("pwm"); label2.Text = Convert.ToString(pwm); }

private void button1_Click(object sender, EventArgs e) { serialPort1.Write("255"); }

private void button2_Click(object sender, EventArgs e) { serialPort1.Write("0"); }

}

You have copied your code and you say that it doesnt work, well try my code instead…
If it doesnt work read about serial string receive or just send a char with the value that you want instead of a string.

Sorry I'd not read your answer. I tried it and the button OFF now works. the button ON turn on the LED but with a low intensity and the trackbar doesn't work . I'll read something more about receiving and sending info with the serial thank u for the help :)

@Senso
You have this:

if(Serial.available() > 1)
{
 for(int i=0; i<4; i++){
Csharp[i]=Serial.read();
}
}

So, if there is one byte available, it’s OK to read all 4 of them?

Where’s the NULL terminator that atoi() is looking for?

erialPort1.Write("255");
           serialPort1.Write("0");
serialPort1.Write("pwm");

You are still sending a string, instead of a value.

I know it’s a string
but the method doesn’t allow to send anything else :S
that’s the problem PaulS

did u see the video I posted?
maybe u could have an idea for doing it

The SerialPort.Write() method is overloaded. There is an overload that takes an array of bytes (a byte can hold a value between 0 and 255, which is plenty since the maximum value that analogWrite can handle is 255), an offset (0), and the length of the array (1).

Create a byte array of length 1, store the value in the array, and use the Write overload to write a byte array.

I'm sorry I'm new in this and I think I understand you idea but I don't know how to write the code

byte[] data = new byte[1];
data[0] = 255; // or 0 or the value from pwm
serialPort1.Write(data, 0, 1);

It doesn't work :'( but whatever it was very very thank u for spending ur time in order to help me.

Greetings from Mexico :)

It doesn't work

What doesn't work? The code I posted doesn't compile? The C# application doesn't send the right the data? The Arduino does not get the right data?

I think the arduino doesn't recognize the data sent by C#. I send 255 or 0 as you said to me and there isn't any change in the LED state.

Does the RX light flash when the data is sent?