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);
}
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
}
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
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.
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.
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
}
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
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.