Home > Blockchain >  Javascript calculate Points every minute between two variables as half sinus curve
Javascript calculate Points every minute between two variables as half sinus curve

Time:01-18

I want to create a half sinus curve between two points. I have a loop every minute to calculate the new value.

Start value: 6500
End value: 2700

The timerange ist for example sunrise to sunset (eg: 1673931600000 and 1673970886131) I have the "sun minutes per day". eg. 654.76885

Now, I want to calculate every minute the new value. => adaptiveCtTimed

Here a example of the Curve; curve

For a linear curve, I use the following code which works perfect.

sunMinutesDay = (sunset - sunrise) / 1000 / 60;
RangePerMinute = CtRange / sunMinutesDay;

if (await compareTime(adapter, sunrise, sunset, "between", ActualTime)) {
    adaptiveCtTimed = Math.round(maxCt - ((ActualTime - morningTime) / 1000 / 60) * RangePerMinute);
} 

CodePudding user response:

To create a half sinus curve between the two points, taking into account the sunrise to sunset time range, you can use a similar approach as before but also incorporate the sunMinutesDay and RangePerMinute variables into the calculation. Here's an example of how you could do this:

const startValue = 6500;
const endValue = 2700;
const totalMinutes = 654.76885;
const sunMinutesDay = (sunset - sunrise) / 1000 / 60;
const RangePerMinute = (startValue - endValue) / sunMinutesDay;

for (let i = 0; i < totalMinutes; i  ) {
     if (await compareTime(adapter, sunrise, sunset, "between", ActualTime)) {
        let adaptiveCtTimed = startValue - (Math.sin(i / totalMinutes * Math.PI) / 2   0.5) * RangePerMinute;
        console.log(adaptiveCtTimed);
     }
}

This code uses the sunMinutesDay and RangePerMinute variables to calculate the range of values per minute, and also uses the compareTime function to check if the current time is between sunrise and sunset. If it is, the Math.sin function is used to generate a half sinus curve, and the value of i / totalMinutes is multiplied by Math.PI to get the correct sinus curve shape. The value of (Math.sin(i / totalMinutes * Math.PI) / 2 0.5) is then used to calculate the new value and adjust it to the desired range of values.

Note: The compareTime function is not provided here, it should be implemented according to your need.

CodePudding user response:

Here my test with the Code snippet of @Rohan. BTW: I have a Interval around them):

if (LightGroups[Group].adaptiveCt && LightGroups[Group].ct !== adaptiveCtTimedInterpolated) {
    morningTime = (await getDateObject(LightGroups[Group].adaptiveCtTime)).getTime();
    sunMinutesDay = (sunset - morningTime) / 1000 / 60;
    RangePerMinute = CtRange / sunMinutesDay;

    if (await compareTime(adapter, morningTime, sunset, "between", ActualTime)) {
        adaptiveCtTimedInterpolated =
            maxCt -
            (Math.sin(((ActualTime - morningTime) / 1000 / 60 / sunMinutesDay) * Math.PI) / 2  
                0.5) *
                RangePerMinute;
    } else {
        adaptiveCtTimed = minCt;
    }
}

But now (it's 3 pm, I get value 6500)

Function getDateObject is for concerting string to timestamp (06:00)

  • Related