Go Down

Topic: string to int (pwm) (Read 22501 times) previous topic - next topic

I've posted this question also in the software interfacing part, but I think the problem is more on the Arduino hardware/software side.

I try to convert a string like from 000 to 255 to a value for the PWM output.
I use to method below, but sometimes other PWM outputs react on changes on another output.

Maybe the code isn't efficient enough.

Is there a way to convert string or hexicadecimal values ( 00-FF ) to an output value with the standard Arduino library ?
From string to int or someting.

Code: [Select]

if (serInString[0]==73)  // I
{ for (int i=1; i <= digseq; i++)
 { if (serInString[i]==',')
   { p++;
     i++;
     }    
   char valchar=serInString[i];
     if(p==8 || p==12 || p==13)                        // digital outputs
     { // other code      
     }
     else                                           // PWM outputs
     { valpwm = 0;
       valpwm = (serInString[i]-48)*100;
       valpwm = valpwm + (serInString[i+1]-48)*10;
       valpwm = valpwm + (serInString[i+2]-48);
     
       i=i+2;                                    
       analogWrite(p,valpwm);                   
     }
 }  
}

jims

You can use the standard C library function atoi(). It takes a string in decimal and returns an integer. I just checked and it only adds about 120 bytes to the sketch.

#2
Nov 14, 2007, 08:18 am Last Edit: Nov 14, 2007, 08:18 am by superliminal Reason: 1
thx.. this is good info! Question: which library do i need to import for the atoi to work. When i do it i get this:
error: invalid conversion from 'char' to 'const char*'

code:
char thisChar = "A";
     int a = atoi(thisChar);

mjk

atoi() expects a pointer to a zero-delimited string, not just a single character.

Just add an asterisk (*) to the variable declaration and keep using double quotes for the string:

Code: [Select]
char * thisChar = "A";
int a = atoi(thisChar);



ShowgetteR

Quote
atoi() expects a pointer to a zero-delimited string, not just a single character.

Just add an asterisk (*) to the variable declaration and keep using double quotes for the string:

Code: [Select]
char * thisChar = "A";
int a = atoi(thisChar);



Ok, I have the exact same problem as the thread starter, but I don't understand that example of code...  :'( :'( I tried now for abaout 2 hour and I don't understand it... Google doesn't help me out neither anymore.

The upper quoted code works, but: I have my Arduino receiving the Serial.input, which comes as chars (ASCII) and when I change "A" to the read out values from Serial Connection, the compiler sais:

Quote
error: invalid conversion from 'char' to 'char*


My code looks as following:

Code: [Select]

int ledPin = 11;
char val;
void setup()
{
 // begin the serial communication
 Serial.begin(9600);
 pinMode(ledPin, OUTPUT);

}

void loop()
{


 // check if data has been sent from the computer
 if (Serial.available()) {
   // read the most recent byte (which will be from 0 to 255)
    val = Serial.read();
   // set the brightness of the LED

   
   
  // byte var = parseChar(val);
   
   Serial.println(val);
   
char * thisChar = val;
int a = atoi(thisChar);


 Serial.println(a);

 // analogWrite(ledPin, var);
   Serial.flush();
 
 }
}


This example should just get the values and print them back via Serial.write... Later on I wanted to PWM a simple Led (later an array of LEDs, but for now I have to get this thing here working...)

I already read something that sais I have to set up an array like val[3] for example, but I dunno how. Please help me  :-[

AlphaBeta

#5
Mar 15, 2009, 02:21 am Last Edit: Mar 15, 2009, 02:26 am by AlphaBeta Reason: 1
Try changing
Code: [Select]

char * thisChar = val;

to
Code: [Select]

char * thisChar = &val; //point to the adress of val


Or:
[UNTESTED CODE]
Quote


int ledPin = 11;
char* val = "0";

void setup()
{
 // begin the serial communication
 Serial.begin(9600);
 pinMode(ledPin, OUTPUT);
}

void loop()
{
 // check if data has been sent from the computer
 if (Serial.available()) {
  // read the most recent byte (which will be from 0 to 255)
   val[0] = Serial.read();
   
   Serial.println(val);
   
   int a = atoi(val);
   Serial.println(a);

// analogWrite(ledPin, var);
   Serial.flush();
 }
}

ShowgetteR

Thank you _very_ much for the also _very_ quick answer.

Both of your tips work for me, but I'm faced to a new problem: How can I send values of 0-255 and get them converted to int? I experienced that this doesn't work and I don't know why...  :o

If this is too much for you, I can help me out with google I think...

ShowgetteR

Let me re-ask that question:

When I initialize the char array (string) to "AAA" (So it has a 3 characters), I want the new value to be, of course, the value of Serial.read().

But when I do

Quote
val[0] = Serial.read();

and I send, let's say 255 from my Computer, only the first character gets changed equal the Serial.read() value. If I change it to
Quote
val[1] = Serial.read();
or
Quote
val[2] = Serial.read();

the second or the third char get the Serial.read() value assigned. How can I set the var from AAA to let's say 255?

I tried with a for() function and val[i++], but this doesn't work as expected neither.

Nigel

Well, I'm very much a beginner but I might be able to help out.  I got around this same problem by reading a byte and subtracting 48 to convert from a character to its equivalent integer value.  Look at an ASCII table - 0 = 48, 1 = 49, 2 = 50 etc.  

Once I had the integer equivalent of the character, I simply multiplied by its significance and added them together something like this:

Serial.read(info);
val += (info * 100);
Serial.read(info);
val += (info * 10);
Serial.read(info);
val += info;

for a number I knew had only three digits.

Hope this helps!

Pilkaster

I want to send a string from example matlab to arduino. If I'm controlling a motor with pwm I want to send for example "255" from matlab (fprintf('255')). This will come as a string to arduino and I want to convert it from 255 as a string to 255 as an int. Then I can send the int with the value 255 to my motor and it will be set to run at full speed.

I'm sorry to say but Nigel here has some errors in his code. It should be info = Serial.read(); instead. I tried this and it gave me strange high numbers that I can't use.

I've also tried char * string but then you have to set it to something and it will then be a problem when I want to send a string thru the serialport. It is also impossible to set the char * string directly like this:
string = Serial.read().

AWOL

#10
Nov 04, 2009, 03:02 pm Last Edit: Nov 04, 2009, 04:16 pm by AWOL Reason: 1
Quote
I tried this and it gave me strange high numbers that I can't use.


That's because he didn't actually do the subtraction of the '0' offset he started talking about.
Code: [Select]

info = Serial.read () - '0';
val += info*100;  etc


Or you could use "atoi".

[edit]
Quote
I've also tried char * string but then you have to set it to something and it will then be a problem when I want to send a string thru the serialport. It is also impossible to set the char * string directly like this:
string = Serial.read().

Reading a string like this isn't supported - besides, it would be difficult to do.
The terminating null-character almost certainly hasn't been sent by the transmitter, so how do you decide when the string is complete? [/edit]
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Warne

Hi, I've got a similar problem. I'm using C# and I'm sending 16 integer values such as 12,458,789,13,45,89,100,57,36,46,96,123,658,758,45,67 via a serial communication. Now when i send those values from C# to the arduino, i want my aruino to store them inti an array such as

   Value[0] = 12;
   Value[1] = 458;
   Value[2] = 789;
   etc...

I'm able to send 12 from C# and then the arduino reads each character and store array[0] = 1 & array[1] = 2 but i want to be able to store the whole integer. Any Help?

Regards,
Matthew


PaulS

When you send the integer values, is there a separator between the values? If not, how do you know where the ascii representation of one value ends and the next begins?

Are you able to get the entire string sent by the C# application into a character array on the Arduino?

Go Up