Go Down

Topic: Converting strings to 'real' numbers ? (Read 1 time) previous topic - next topic

myozone

Jun 07, 2008, 11:12 am Last Edit: Jun 07, 2008, 11:13 am by myozone Reason: 1
As the subject, I think that's what I want to do. Well, what I want to do is convert 9 bytes (from serial) to 'real' numbers to move a servo (in degrees) like this :-

W239 048 is the sort of 9 bytes, what I'm trying to do is extract the 239 and 048 to send to two servos (to azimuth and elevation) but can't think how to do it !  :( :-[

Dave.

You can convert a character digit to a numerical digit using the following:

char charDigit = '3';
int digit = charDigit - '0';  // this is the same as digit = 3;

Next note that the number 239 = 2 * 100 + 3 * 10 + 9

In general, in a base-10 number system, a number equals:

sum(digit n * 10 ^ n) from n = (num digits - 1) to 0.

From here you should be able to write a simple algorithm to turn a string of numerical characters into an integer.

- Ben

myozone

#2
Jun 07, 2008, 11:54 am Last Edit: Jun 07, 2008, 03:33 pm by myozone Reason: 1
Thanks, sounds good. I'm trying to make two servos 'track' the moon for example using software called Orbitron - it has a plug-in to control a ham radio az-el rotor and can output in the form WAAA EEE - AAA being the azimuth and EEE the elevation in degrees. Then I could send it to, two servos and also a bit of code to get over the 180 degree limit of servos

If (az=>180)
{tilt=180-el} ;
{dir=az-180};

'Tilt' and 'dir' are to be sent to the servos and az and el from the coversion from WAAA EEE

Dave.

spankyham

The following is an excerpt from a program to interface with an RS232 controlled instrument, which returned a string, of which the numerical data was extracted to be passed to a D2A function:

char SERIALDATA[50]

Serial.println("MEAS POW L1 D2?");  //RS232 Query.
 int i = 0;                          //loop integer.
 int X = 0;                          
 while (X != 58){
   if (Serial.available() > 0) {     //Check serial buffer for info.
     X = Serial.read();              //Read byte from serial buffer.
     SERIALDATA = X;              //Store byte to string.
     i++;                            //Go to next byte.
   }
 }

 long P1 = SERIALDATA[0]-48;
 long P2 = SERIALDATA[1]-48;
 long P3 = SERIALDATA[2]-48;
 long P4 = SERIALDATA[4]-48;
 long P5 = SERIALDATA[5]-48;
 long POWER = 10000*P1+1000*P2+100*P3+10*P4+P5;

 return POWER;

Essentially it just reads each ascii character as its byte reference and then subtracts 48 to get the actual number. The numbers then need to be recombined.

myozone

#4
Jun 07, 2008, 03:27 pm Last Edit: Jun 07, 2008, 03:29 pm by myozone Reason: 1
Ah thanks  :) I just needed the right pointer  ;)

Just curious what does the 'RS232 controlled instrument' do ?

Dave.

lilpunk1302

RS232 is the standard serial interface on your computer,

It's like USB in a way,

Certain number of data pins, ad expectations for how they're triggered to send data.

myozone

I knew what RS232 was, just curious what type of RS232 controlled instrument Spankyham was using - Thanks anyway.

Dave.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy