How to add a formula

Hello everyone,
I’m working with my photon and a potentiometer. And I managed to measure the voltage and publish it online, but I want to do something with the measured voltage. For example, I want a formula like (voltage*0,01 =) and I’ve already searched on, but I can’t find it.
Can someone help me out? Thanks in advance,

This is my code for now

const String topic = "Potentio";

//this enables Serial communication. When TRUE the Spark will send the measurement value over
//the USB serial every "intervalSerial" seconds. Only change to FALSE when no further debugging
//is needed

//Sensor is connected to this pin.
const int inPin = A0; 

//number of seconds between uploads to the Spark Cloud. This may never be lower than 60
//because IFTTT does not allow for more triggers than 1 per minute.
const int intervalOnline = 5; 

//number of seconds between uploads over the USB serial
const int intervalSerial = 1; 

//number of milliseconds between measurements. This could be as low as needed, but higher values
//results in a more stable behaviour of the ADC. 
const int intervalMeasurement = 100;

int measurement;

void setup(){
    //start Serial communication
    if (DEBUG_SERIAL) Serial.begin(9600);

    //set the inPin to INPUT

void loop(){
    //get timestamp
    int t=millis();

    //every intervalMeasurement milliseconds, do a measurement 
    if (t%(intervalMeasurement)<1){
        measurement = analogRead(inPin);

    //every intervalSerial seconds, send measurement over Serial USB
    if (DEBUG_SERIAL){
        if (t%(intervalSerial*1000)<5){
    //every intervalOnline seconds, publish measurement to Spark Cloud
    if (t%(intervalOnline*1000)<10){
        String measurementText = String(measurement, DEC);


When you already have the voltag reating in a variable, just apply whatever calculation you want to that variable and store the result into a new variable or store it back in the original

double voltage = 123.4;
double result;

result = voltage * 0.1; // calculate and store in other variable
// or 
voltage *= 0.1; // this is the same as voltage = voltage * 0.1;

But how do I store the voltage in a variable? Could you perhaps add a calculation in my code?

Like you would with any other variable? You’ve even done so in your code, repeatedly:

int t=millis(); //assigns the millis() value to a new variable 't'

measurement = analogRead(inPin); // assigns the analog value from inPin to a variable 'measurement'

You already have a couple of calculations in your original code…

if (t%(intervalOnline*1000)<10){
  // ...

That will multiply the intervalOnline value with 1000. That is then used with a modulo operation on t and checked if the result is smaller than 10.


But how exactly can I multiply the value that is published online by 4. What exactly do I need to add in the code?

I wonder what’d happen if you swapped that ‘1000’ for another value… Say… 4?

All the information you’re asking for is already implemented in your original code. How exactly did you come up with that ;)?

You can multiply by four prior to publishing like so:

However, you can’t change the value once you have published, if that is what you are asking.

Thanks a lot, problem solved;)

Thanks a lot, you really saved me here;)

btw, do you perhaps know how I can add the word “degrees” so I will read my measured values online like "value degrees"

This tutorial might be interesting:

Or just read the docs
String Class

//every intervalOnline seconds, publish measurement to Spark Cloud
    if (t%(intervalOnline*1000)<10){
        String measurementText = String(measurement * 4, DEC) + " degrees";

Strings can be added (concatenated) using the addition operator.

1 Like