Hello, as the title says, I have some new core functions I would like to add to future IDE updates.
- avg( ) "Average", this function will average the contents of a numeric array and return the value.
template<class T, size_t size> T avg(T(&x)[size])
{
T tmp;
for(int i = 0, j = size; i < j; i++)
{
tmp += x[i];
} return (tmp/size);
}
Example sketch:
int myIntArray[5] = {25,324,420,1000,10};
float myFloatArray[5]={25,324,420,1000,10};
void setup()
{
Serial.begin(115200);
Serial.println(avg(myIntArray));
Serial.println(avg(myFloatArray));
}
void loop() {}
Output:
355
355.80
- shed(), Nothing fancy, just a simple little function to get the values after the decimal point, of a float or double
float shed(float val)
{
return val - long(val); // stupidly simple function
}
Example Sketch:
float val = 123.4567;
void setup(){
Serial.begin(115200);
Serial.println(shed(val),4);
}
void loop() {}
Output:
0.4567
- _map "templated map" this allows the map function to do ints, longs and floats with their expected outputs
template<class T>
T _map(T x, T in_min, T in_max, T out_min, T out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Example Sketch:
void setup()
{
Serial.begin(115200);
for(byte i = 0; i < 101; i++)
{
Serial.print(map(i, 0, 100, 0, 10));
Serial.print("\t");
Serial.println(_map<float>(i, 0, 100, 0, 10));
delay(10);
}
}
void loop() {}
Output: See attachment
I'm still thinking of more I can make, but for now this is all I got.
results.txt (810 Bytes)