A bit cheeky

3 replies [Last post]
allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011

Because it's not directly related to the HAH but I'm trying to write an arduino sketch to keep my boiler running while the box is off.

 

I'm trying to do the equivalent of:

tonumber(frame:getValue("input.state","text"))

 

I've trawled many sites so either I'm missing something obvious or it isn't straight forward like it is in most programming languages with which I'm familiar.

Is there an easy way to read a string coming in on the serial port and convert it to a number?

brett
Offline
Providence, United States
Joined: 9 Jan 2010
The function that you need is

The function that you need is atoi() - example.

#include <stdio.h>
#include <stdlib.h>
main() {
  char a[10];
  char *b = (char *)malloc(10);
  sprintf(a,"1");
  sprintf(b,"1");
  int c = atoi(a);
  int d = atoi(b);
  printf("%d %d\n", c, d);
free(b);
 }
allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Thanks

Thanks Brett, I'll give it a whirl and see how I get on.

Whatever happend to the simplicity of something like val(a$) ?

 

allanayr
Offline
Ayr, United Kingdom
Joined: 25 Sep 2011
Thanks

Brett, Thanks for the pointers, in the end I went for this:

String tempString = "";
char tempMsg[10];


tempString = inputString;
tempString.toCharArray(tempMsg,10);
Thermostat = atof(tempMsg);

Seems to work OK too.

 

Hardware Info