Determining time (x) increments between y-values on a sine wave

I'm trying to figure out the x-increments between the integer values of y, of the function:

y = 72sin(x-[PI/2])+72, and enter them into an array of length 144, which is how many integer y-values there should be.

Any idea how best to achieve this?

Since sin() returns a float or double, the result, or any integer multiple of the result, will almost never be an exact integer.

Instead, you need to decide whether a given value can be considered "close enough" to an integer, by some criterion.

e.g.

int y = x;
if ( fabs(x - y) < 0.01) Serial.println("close enough");

Or just round off the result to the nearest integer, if it is for a display.

I'm at this point:

#include "Math.h"

float y;

void setup() {
Serial.begin(115200);
}

void loop() {

for (int i = 0; i < 144; i++){
y = ((asin((i-72)/72)+(PI/2))/PI);
Serial.println(y);
delay(100);
}


...but the Serial Monitor is returning a "y" value of 0.50 for all but one value, the first one, for which it returns 0.

This is integer math. No wonder you are seeing low value integers.

Modified to this:

#include "Math.h"


double x;
double y;

void setup() {
Serial.begin(115200);
}

void loop() {

for (int i = 0; i < 144; i++){
  x = i;
Serial.println((asin((x-72)/72)+(PI/2))/PI);
delay(300);
}


}

An improvement, but still only seeing two decimal places on the output.

Still something about "i" being an integer?

Serial.println((asin((x-72)/72)+(PI/2))/PI, 5);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.