GY-63_MS5611/libraries/MCP4725/examples/smooth2Value/smooth2Value.ino

91 lines
1.7 KiB
Arduino
Raw Normal View History

//
// FILE: smooth2Value.ino
// AUTHOR: Rob Tillaart
// PURPOSE: test mcp4725 lib
// DATE: 2013-12-01
2020-11-27 05:20:37 -05:00
// URL: https://github.com/RobTillaart/MCP4725
2021-12-21 10:28:24 -05:00
2020-11-27 05:20:37 -05:00
#include "Wire.h"
#include "MCP4725.h"
2021-01-29 06:31:58 -05:00
MCP4725 MCP(0x62); // 0x62 or 0x63
2021-12-21 10:28:24 -05:00
2020-11-27 05:20:37 -05:00
void setup()
{
Serial.begin(115200);
Serial.print("MCP4725 test program: ");
Serial.println(MCP4725_VERSION);
2021-01-29 06:31:58 -05:00
MCP.begin();
Serial.print("\nValue:\t");
2021-01-29 06:31:58 -05:00
Serial.println(MCP.getValue());
Serial.println();
Serial.println("smooth2Value(2000, 10)");
2020-11-27 05:20:37 -05:00
smooth2Value(2000, 10);
Serial.print("Value:\t");
2021-01-29 06:31:58 -05:00
Serial.println(MCP.getValue());
Serial.println();
Serial.println("smooth2Value(100, 50)");
2020-11-27 05:20:37 -05:00
smooth2Value(100, 10);
Serial.print("Value:\t");
2021-01-29 06:31:58 -05:00
Serial.println(MCP.getValue());
Serial.println();
//////////////////////////////////////////////////
Serial.println("\n\nPERFORMANCE\n");
uint32_t start = micros();
2020-11-27 05:20:37 -05:00
for (int i = 0; i < 100; i++)
{
2020-11-27 05:20:37 -05:00
smooth2Value(i * 10, 10);
}
uint32_t end = micros();
2021-01-29 06:31:58 -05:00
Serial.print("100x MCP.smooth2Value(i*10, 10):\t");
Serial.println(end - start);
Serial.print("\nDone... (start triangle mode)");
}
2021-12-21 10:28:24 -05:00
2020-11-27 05:20:37 -05:00
void loop()
{
// different gradients
2020-11-27 05:20:37 -05:00
smooth2Value(4095, 4096);
smooth2Value(0, 4096);
smooth2Value(4095, 2048);
smooth2Value(0, 2048);
smooth2Value(4095, 1024);
smooth2Value(0, 1024);
}
2020-11-27 05:20:37 -05:00
int smooth2Value(uint16_t value, uint16_t steps)
{
if (value > MCP4725_MAXVALUE) return MCP4725_VALUE_ERROR;
2020-11-27 05:20:37 -05:00
if (steps > 1)
{
2021-01-29 06:31:58 -05:00
uint16_t startValue = MCP.getValue();
2020-11-27 05:20:37 -05:00
float delta = (1.0 * (value - startValue)) / steps;
for (uint16_t i = 0; i < steps - 1; i++)
{
2021-01-29 06:31:58 -05:00
MCP.setValue( round(startValue + i * delta) );
}
}
2020-11-27 05:20:37 -05:00
// get the end value right
2021-01-29 06:31:58 -05:00
return MCP.setValue(value);
}
2021-12-21 10:28:24 -05:00
2020-11-27 05:20:37 -05:00
// -- END OF FILE --
2021-12-21 10:28:24 -05:00