Skip to content

Instantly share code, notes, and snippets.

@jaidetree
Last active December 28, 2015 04:49
Show Gist options
  • Save jaidetree/7445423 to your computer and use it in GitHub Desktop.
Save jaidetree/7445423 to your computer and use it in GitHub Desktop.
Divides a sensorValue by 48 and displays it in the console.
#include <stdio.h>
float calcSensorValue(int originalValue) {
// Casting allows us to take an integer and treat it as a float,
// which means getting decimal numbers instead of whole numbers
// which would be inaccurate.
return 48 / (float)originalValue;
}
// Responsible for handling output.
// sensorValue is the original sensorValue
// result is expected to be the result of sensorValue / 48
// which is defined in the calcSensorValue
void output(int sensorValue, float result) {
printf("48 / Sensor Value(%i) = %f\n", sensorValue, result);
// more on printf may be found here: http://www.cplusplus.com/reference/cstdio/printf/
// Basically where %i is replace with an integer, so we give it the sensor value.
// %f is replaced with a float, so we give it the result.
// The order is specified by what comes first in the format string.
}
int main() {
// This could go in your loop somewhere where the sensorValue is being read
int sensorValue = 0;
int i;
for(i=1;i<5;i++) {
sensorValue = i * 15 ^ i; // Just to make it look interesting for this demo.
// What you want to do is pass in the sensor value
// Structering it this way means you can call output as many times
// as you need but you only have to specify how output works
// in one, and only one place.
output(sensorValue, calcSensorValue(sensorValue));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment