A bit cheeky

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?

Member for

16 years 10 months

brett

13 years 11 months ago

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);
 }

Member for

15 years 1 month

allanayr

13 years 11 months ago

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

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

 

Member for

15 years 1 month

allanayr

13 years 11 months ago

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.