yess
let see
i've forgotten the c# application
i'm trying to send te order from the serial monitor in the arduino program
and the rx flash but the led doesn't change.
that's the code i'm using in arduino:
char Csharp[3]; // 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() > 0)
{
for(int i=0; i<4; i++){
Csharp*=Serial.read();*
}
}
analogWrite(9,atoi(Csharp));
}
char Csharp[[glow]3[/glow]]; // 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() > 0)
{
for(int i=0; i<[glow]4[/glow]; i++){
Csharp[i]=Serial.read();
}
}
How do four characters fit into a three byte array?
i did the change and now when I send a 0 the LED turns off
buy when I send 255 it turns On very very LOW
I thought maybe could be the LED but that wasn't the problem haha
Maybe it is because you aren't terminating the string.
(when you post code, please use the # (code) icon on the editor's toolbar)
I send 0 and its OFF
send 64 its On low
send 128 its on higher
but 255 its lower
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() > 0)
{
for(int i=0; i<4; i++){
Csharp[i]=Serial.read();
}
}
analogWrite(9,atoi(Csharp));
}
See reply #24
if(Serial.available() > 0)
{
for(int i=0; i<4; i++){
Csharp[i]=Serial.read();
And if "Serial.available" returns one?
It supposed it has to do that. if returns 0 it means the serial port is not sending anything.
I think I've found the problem with that code I found:
int PwmLed = 9;
int Csharp;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
if (Serial.available() > 0) {
Csharp = Serial.read();
Serial.println(Csharp, DEC);
analogWrite(PwmLed, Csharp);
}
}
when I write in the serial monitor of arduino I write 0 but the value that arduino takes its 33. I think it takes the ascci value of the char i write. How can I convert that "0" ascii with value 33 into a a zero wich means ZERO for turning off the LED?
I write 0 but the value that arduino takes its 33. I
You really are screwed.
ASCII zero is 0x30, which is 48 in decimal.
I don't know how you got 33.
hahaha sorry your'e right i wanna say ! = 33 which is the lower char i could find
do u have some idea for solving the problem?
Reading string data from the serial monitor and reading bytes from C# are two different processes.
Which one do you want to do? Either can be made to work.
string data
OK, so the C# application needs to send "255" or "000" for the limits. For the slider, the value needs to be converted to a string. C# makes this easy, with the ToString() method that everything must implement.
What is not so easy is ensuring that the string has exactly 3 characters. You need to figure out how to do that.
On the Arduino side, then, you need something like this:
int val = -1;
if(Serial.available() >= 3)
{
char valStg[4];
valStg[0] = Serial.read();
valStg[1] = Serial.read();
valStg[2] = Serial.read();
valStg[3] = '\0';
val = atoi(valStg);
}
if(val >= 0)
{
analogWrite(pin, val);
val = -1;
}
Hi guys!!
I've solved the problem with some ideas from you and other topics in this forum. I think there are methods easier than this, but for now it's working!!
I'm really greatful with you. I know I'm new and sometimes I ask things maybe you think are silly and my english is not good enough hahaha.
I post it if someone wants to check it.
FELIZ NAVIDAD Y AÑO NUEVO A TODOS
ARDUINO CODE:
byte Csharp;
int PwmLed = 9;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(PwmLed, OUTPUT);
}
void loop() {
if (Serial.available() > 0) {
Csharp = Serial.read();
Serial.println(Csharp,DEC);
analogWrite(PwmLed, int(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
{
byte[] data = new byte[1];
public Form1()
{
InitializeComponent();
serialPort1.Open(); //HABILITA EL SERIALPORT1
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
byte pwm;
pwm = Convert.ToByte(trackBar1.Value);
data[0] = pwm;
serialPort1.Write(data, 0, 1);
label2.Text = Convert.ToString(pwm);
}
private void button1_Click(object sender, EventArgs e)
{
data[0] = 255; // or 0 or the value from pwm
serialPort1.Write(data,0,1);
}
private void button2_Click(object sender, EventArgs e)
{
data[0] = 0; // or 255 or the value from pwm
serialPort1.Write(data,0,1);
}
}
}
if (Serial.available() > 0) {
Csharp = Serial.read();
Serial.println(Csharp,DEC);
analogWrite(PwmLed, int(Csharp));
}
That's OK, but... 'a' will result in a brighter LED than 'A' (even 'z' will be brighter than 'A'), and you will not get near full brightness.
Suggest you continue trying to turn "200" into 200.
It will be worth it, promise!
I get what you mean. Nevertheless, I think I won't have this problem, cause in Button ON I'm sending a 255 as a byte, Button OFF sends a 0 as a byte, and the trackbar values are only numbers which will be converted in byte and send like that to the arduino. With this, 0 is a LED OFF and 255 is a LED ON with the maximum brightness