Sorry for what may be the hundredth one of these but everything I have found does not work for me (or I don't understand it, new to C++), or include c++ methods not on this.
I'm sending a string two numbers (both are only from 0 to 100) in a string like "15:65" from a c# program I have running. I simply want to do is split that into the two numbers and then convert them to an int.
Do you know what a case statement is? Normally I would say to use the strtok() function, which you still could, but that's a little much for what your doing.
What you could do instead is use an IF statement to check if the value your receiving is a number or a colon. Once you can tell the difference, you can use case statements to collect the two numbers and convert them into ints. If you do get a semicolon, you can then switch the case statement and collect the values for the second number. Once you have both numbers and use them, you then need to clear them to start over for new data.
This is an example you can use to collect the data and convert it to an int. Not the full code, it's just an idea
char val = Serial.read();
if( val <= '0' || val >= '9' ) // val is NOT a digit
{
CaseInc++; // will switch case if not a digit
}
// make case statement here
FirstNum = FirstNum * 10 + ( val - '0' ); // first number
Do I use While(serial.available) or if, As I do need the full value each loop?
Also how does the converting to int work, as im using LED's and they go crazy with what's on it now even when the serial input is "0:0". Also wouldn't it be "if(c < '0' || c > '9')" else I would not get numbers with 0 or 9?
That's fine, just make sure count = 1 (globally).
The conversion method works like this.
FirstNum starts at 0, then you get a value from the serial monitor, "c". Now because c is a char, you need it to be converted to an int, so (c - '0') does just that. Now "FirstNum = FirstNum * 10 + (c - '0')" takes single digits and meshes them together, into however large of a int or long.
Example:
c = '5'; FirstNum = 0.
0 = 0 * 10 + ('5' - '0');
0 = 0 * 10 + (5); by order of operations, multiplication is done first.
0 = 0 + 5;
Now FirstNum = 5. Get a new value.
c = '9'; FirstNum = 5.
5 = 5 * 10 + ('9' - '0');
5 = 5 * 10 + (9); by order of operations, multiplication is done first.
5 = 50 + 9;
Now FirstNum = 59.
If your LEDs are going crazy, make sure your using the PWM (~)pins. If that still doesn't work, post your full code.
Ok I see what your doing, but in this case, I'll need to add a new line to the code I gave you. In order for this to work, you need to change your string to include a period at the end of it "XX:YY**.**"
Actually, I'll change the code you provided.
if(Serial.available() > 0){
char c = Serial.read();
if(c <= '0' || c >= '9')
{
count++;
}
if( c == '.')
{
count = 0; t1 = 0; t2 = 0; // if the received char is a period, reset count and t1,t2 back to zero
}
switch(count){
case 0:
t1 = t1 * 10 + (c - '0');
break;
case 1:
t2 = t2 * 10 + (c - '0');
break;
}
}
This is no longer needed. Change incomingByte the way you were going to.
if (Serial.available() > 0) {
incomingByte = Serial.parseInt();
}
What string were you sending that gave both red and blue to show?
When I tested my code, I noticed it was adding the colon to the second number, this should take care of that.
int count= 0, t1= 0,t2= 0;
void setup(){
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0){
char c = Serial.read();
if(c == '.') // if c is a period, show data and clear everything
{
showme(t1,t2); // Replace with fade(); & led.update();
count = 0;
t1 = 0;
t2 = 0; // if the received char is a period, reset count and t1,t2 back to zero
}
if(c == ':') // if c is a colon, change cases
{
count++;
}
else // same as "if(c >= '0' || c <= '9')"
{
if(c != '.') // if c is a period, do NOT add it to t1 or t2, discard it!
{
switch(count){
case 0:
t1 = t1 * 10 + (c - '0');
break;
case 1:
t2 = t2 * 10 + (c - '0');
break;
}
}
}
}
}
void showme( int D1, int D2){
Serial.print("D1: ");
Serial.print(D1 );
Serial.print(" : ");
Serial.print("D2: ");
Serial.println(D2);
}
Well im probably screwing something up, But T1 comes in wrong, All i changed was:
if(c == '.') // if c is a period, show data and clear everything
{
fade();
led.update();
Serial.println(t1);
count = 0;
t1 = 0;
t2 = 0; // if the received char is a period, reset count and t1,t2 back to zero
}
I think the issue here is the difference between Strings and strings. One is a string object the other is an array of chars. You might need to use another method than what I gave you.
Does it work with the Arduino serial monitor? Try it with just entering the string into the Arduino serial monitor and see if it works or not. What I gave you is meant for char arrays not Strings.
Not the best video due to camera phone, Currently working on colour fading option instead of Black -> colour... But for some reason the first colour works fine but always fades to pink. As seen here: - YouTube