Pages: [1]   Go Down
Author Topic: Converting strings to 'real' numbers ?  (Read 890 times)
0 Members and 1 Guest are viewing this topic.
Cornwall UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !  smiley-sad :-[

Dave.
« Last Edit: June 07, 2008, 04:13:26 am by myozone » Logged

Las Vegas, NV
Offline Offline
God Member
*****
Karma: 0
Posts: 507
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Cornwall UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 07, 2008, 08:33:08 am by myozone » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Cornwall UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah thanks  smiley I just needed the right pointer  smiley-wink

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

Dave.
« Last Edit: June 07, 2008, 08:29:17 am by myozone » Logged

Vancouver, BC, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 164
Arduino 4 Prez
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Cornwall UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 68
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Dave.
Logged

Pages: [1]   Go Up
Jump to: