Hello,
I have a c# app that's constantly sending integers 0-255 as strings to Arduino.
I want a LED to light up when the value is above a certain threshold.
Arduino code:
void setup() {
pinMode(5, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(5, LOW);
while (Serial.available() > 0) {
int inChar = Serial.read();
if (inChar > 230){
digitalWrite(5, HIGH);
}
if (inChar == '\n') {
if (inChar > 230){
digitalWrite(5, HIGH);
}
}
}
}
I have the c# app simultaneously writing in a .txt as a sort of a "debug". The .txt contains multiple rows with 230+ values.
The LED doesn't light up.
However, if I upload this code:
Hi again,
I went over the guide but still feel hopeless without actually seeing the received values, since I cannot even modify the code in any direction.
So I took the example for numbers using atoi() but now it always goes inside the if().
Probably the number is a pointer to an integer or something.
Like I said, it is sending 0-255 numbers as a string.
And the .txt contains the right values (0-255) on a new line each.
edit: I'm assuming atoi(theinput) returns a pointer so it's always > my threshold.
Yes, this is what the documentation says. The problem, in my opinion, lies somewhere else.
The only way the code can get inside the if() is if the value is a pointer, I don't see what else could be it.
I am not sure what atoi() returns in my scenario.
The above is hard coded values of 231 and 229 to show the LED toggle on and off with the following Arduino code, which is your code slightly mopdified.
void setup() {
pinMode(5, OUTPUT);
Serial.begin(9600);
digitalWrite(5, LOW);
}
void loop() {
while (Serial.available() > 0) {
int inChar = Serial.read();
if (inChar > 230){
digitalWrite(5, HIGH);
}
if (inChar<230) {
digitalWrite(5, LOW);
}
}
}
If you still want to do this using an ASCII string then take a closer look at the link Delta_G provided (Serial Input Basics) and study example 5
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable.
You can send data in a compatible format with code like this (or the equivalent in any other programming language)
It makes debugging much easier if you send data in human readable form. I would only send binary data if it was the only way to achieve the required performance.
Stoil:
Isn't this supposed to turn, let's say 505151, to 233 ?
The way I wrote the code it is not. My recvWithEndMarker() function is only intended to receive the data. Make another function elsewhere something like this and call it from loop()
No, it currently does not work. I'm not new to receiving data from serial, it's just that I can always open the serial monitor and modify the code accordingly, but this time..
Stoil:
No, it currently does not work. I'm not new to receiving data from serial,
it's just that I can always open the serial monitor and modify the code accordingly,
but this time..
... you should make the c# code print out anything it receives from serial, so you can have debug output.
I repeat for the nth time, I'm sending strings from my c# app:
foreach(var r in _spectrumdata)
{
port.WriteLine(r.ToString());
}
With the current code it always enters the if(num>254);
edit: weirdly or not, I tried writing to the EEPROM the received data with and without atoi() and it just receives it receives "178" without atoi() and "2" with atoi().
Delta_G:
How do you know it is getting into that if statement? If it does and sets the pin HIGH, then the very next line in loop sets it right back to LOW. So how would you ever see that without a scope?
The atoi() is outside of the loop. Isn't the end of the loop the full data ?
WriteLine appends a new line. Do I really need a start marker if all the data I receive is of the type and has an end marker ?
I know because I tried with a lower number such as 1, which basically means it would flicker non stop.
Like I said, if I put 0 in the if, it flickers like mad.
Where would you like me to put the atoi() in this case ? I do have an end marker tho.
I'm trying to make some leds light up when there's a beat while music is playing.