String to Number conversion of a variable

Posted on
Sun Apr 24, 2016 8:12 pm
srkinard offline
Posts: 320
Joined: Apr 10, 2016
Location: Austin, Texas

String to Number conversion of a variable

Running into an issue trying to do calculations on a value from an Arduino pin...list sensor analog output is 0-1023 and I am trying to convert it to a 0-100% number

Here's the code:

Code: Select all
set LIGHT_SENSOR_ANALOG_RAW to value of variable "A1LightRaw"
set lightLevel to ((1023 - LIGHT_SENSOR_ANALOG_RAW) / 10.23)
set value of variable "A1Light" to lightLevel & "%"


I get an error:
Code: Select all
Error                           script error: around characters 123 to 174
Error                           script error: A descriptor type mismatch occurred. (-10001)


Looks like AppleScript doesn't like the variable...seems like it's a string and not a true numeric.

What's the best way to convert the value from string to numeric?

Posted on
Sun Apr 24, 2016 11:16 pm
srkinard offline
Posts: 320
Joined: Apr 10, 2016
Location: Austin, Texas

Re: String to Number conversion of a variable

I figured it out...wasn't the math part failing, it was the variable line...changed to this and it works (plus did some rounding)

Code: Select all
set LIGHT_SENSOR_ANALOG_RAW to value of variable "A1LightRaw" as integer
set lightLevel to ((1023 - LIGHT_SENSOR_ANALOG_RAW) / 10.23)
set tempNumber to round (lightLevel)
set lightLevelPercent to tempNumber & "%" as text
set value of variable "A1Light" to lightLevelPercent


I'm sure it could be tightened up, code-wise, but the multi-line helps me keep track of steps I'm processing it as.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests