Hoi,
Als laatste wil ik de snelheid grenzen kunnen stellen per effect.
Dus ik heb calcDelay verhuisd naar staart.cpp
#include "effects.h"
#include "pins.h"
#include "output.h"
static EFFECTDIRECTION direction = DIR_NONE;
static int currentLed = 0;
static byte trails[5] = { 0, 0, 0, 0, 0 };
/**
@brief Calculates the delay interval based on the potentiometer value.
This function adjusts the delay interval used in a timing operation (e.g., LED effect).
It calculates the delay interval based on the input potentiometer value (`potValue`)
by mapping it to a range. The delay interval is used to control the speed of an effect
that moves in either a clockwise or counter-clockwise direction.
The function returns different delay intervals depending on whether the potentiometer value
is less than 482 (counter-clockwise), greater than 542 (clockwise), or near the middle (no delay).
The delay intervals are mapped as follows:
- **Counter-clockwise (potValue < 482)**: Mapped from `potValue` to a delay range between 500 ms and 75 ms.
- **Clockwise (potValue > 542)**: Mapped from `potValue` to a delay range between 75 ms and 500 ms.
- **Middle range (potValue between 482 and 542)**: Returns 0, which likely indicates no delay.
@param potValue The potentiometer value (integer), which determines the delay interval.
@return uint32_t The calculated delay in milliseconds, based on the potentiometer value.
- A value between 75 and 500 ms for clockwise or counter-clockwise direction.
- 0 ms if the potentiometer value is in the middle range.
*/
uint32_t calcDelay(int potValue)
{
// 512 - 30 (middle value - offset)
if (potValue < 482)
{
// calculate new interval for counterclockwise
return map(potValue, 482, 0, 500, 75);
}
// 512 + 30 (middle value + offset)
else if (potValue > 542)
{
// calculate new interval for clockwise
return map(potValue, 1023, 542, 75, 500);
}
// somewhere in the middle
else
{
return 0;
}
}
/**
@brief Calculates the direction based on the potentiometer value.
This function takes an input potentiometer value and returns a direction
based on its value relative to a middle reference point. The directions
are defined as either counter-clockwise (DIR_CCW), clockwise (DIR_CW),
or no direction (DIR_NONE) if the value is near the middle reference point.
The function uses a middle reference point of 512 and applies a threshold
of ±30 to determine the direction.
@param potValue The potentiometer value (integer) to determine the direction.
@return EFFECTDIRECTION The calculated direction based on the potentiometer value.
- DIR_CCW (Counter-Clockwise) for values less than 482
- DIR_CW (Clockwise) for values greater than 542
- DIR_NONE for values between 482 and 542 (inclusive)
*/
static EFFECTDIRECTION calcDirection(int potValue) {
// 512 - 30 (middle value - offset)
if (potValue < 482) {
// richting wordt linksom
return DIR_CCW;
}
// 512 + 30 (middle value + offset)
else if (potValue > 542) {
return DIR_CW;
}
// somewhere in the middle
else {
return DIR_NONE;
}
}
/**
@brief Creates a trailing LED effect that moves in a specified direction.
This function simulates a trailing effect where LEDs light up in a sequence,
starting from a `currentLed` position, and the trailing LEDs dim as they move along the sequence.
The `direction` is determined by the potentiometer value (`potValue`), which controls whether the
effect moves clockwise (DIR_CW) or counter-clockwise (DIR_CCW). The function also ensures the LEDs
wrap around when they reach the boundary.
The trail is maintained in an array (`trails`), where the LEDs move according to the direction
and their brightness fades as the trail progresses.
@param potValue The potentiometer value (integer) used to determine the direction (clockwise or counter-clockwise).
@return bool `true` if the LEDs were successfully set, `false` if there was an error setting the LED color.
*/
bool staart(int potValue) {
direction = calcDirection(potValue);
if (direction == DIR_CW)
{
currentLed++;
if (currentLed >= NUM_LEDS - 1 ) {
currentLed = 0;
}
}
if (direction == DIR_CCW)
{
currentLed--;
if (currentLed <= 0) {
currentLed = NUM_LEDS - 1;
}
}
for (int i = 4; i > 0; i--) {
trails[i] = trails[i - 1];
}
trails[0] = currentLed;
int brightness[4] = { 128, 64, 32, 0} ;
for (uint8_t cnt = 0 ; cnt < 4; cnt++) {
if (setLedColour(trails[cnt], CRGB(0, 0, brightness[cnt])) == false) {
return false;
}
} ;
return true;
}
Maar nu zit ik vast hoe calcDelay aan te roepen omdat dit nu in runeffect wordt gedaan voordat de effecten bekend zijn.
RUNRESULT runEffect(bool (*func)(int), int potValue) {
// Calculate the delay interval based on the potentiometer value
uint32_t interval = calcDelay(potValue);
// Check if the delay interval has elapsed
if (timer(interval)) {
En pas in de if then de functie wordt aangeroepen die het effect regelen.
Dus graag een paar hints of ideeën hoe ik dit op kan lossen ?