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 docs.particle.io, 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
#define DEBUG_SERIAL TRUE
//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
pinMode(inPin,INPUT);
}
void loop(){
//get timestamp
int t=millis();
//every intervalMeasurement milliseconds, do a measurement
if (t%(intervalMeasurement)<1){
measurement = analogRead(inPin);
delay(1);
}
//every intervalSerial seconds, send measurement over Serial USB
if (DEBUG_SERIAL){
if (t%(intervalSerial*1000)<5){
Serial.println(measurement);
delay(5);
}
}
//every intervalOnline seconds, publish measurement to Spark Cloud
if (t%(intervalOnline*1000)<10){
String measurementText = String(measurement, DEC);
Spark.publish(topic,measurementText);
delay(10);
}
}
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;
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.