I created a sketch to generate code for an array of all of the values of sin(x) from 0 to 90 (for a future project). I know calculating sin(x) takes more time than cycling through an array of pre-calculated values. But how much faster is it? I tried to measure the speed with the serial monitor but I am not sure I am doing it right. This is my first sketch with an array by the way! I am a little confused on why the compiler complained when I used 90 instead of 91, I thought arrays are 0 indexed? (it is late at night...) Anyways..I used micros to measure it and the numbers seem about right, 1208 manually and 344 with an array. I am still a little new at programming but would appreciate any help from the guru's. If you guys have any suggestions or pointers, it would help a lot. I really want to learn this stuff. Man, you know it is late at night when you press CTRL + U to upload a post. ![]()
// this sketch takes 1208 microseconds to find all of the sin values manually
float x = 0.00;
int count = 0;
unsigned long start = 0;
void setup(){
Serial.begin(115200);
}
void loop(){
while(count < 1){
start = micros();
Serial.println(start);
for( x = 0; x <= 90; x += 1){
sin(x);
x++;
}
Serial.println(micros());
count++;
}
}
// this sketch gives me 344 micro seconds to completely cycle the array
float sine2[91] = {0.00, 0.84, 0.91, 0.14, -0.76, -0.96, -0.28, 0.66, 0.99,
0.41, -0.54, -1.00, -0.54, 0.42, 0.99, 0.65, -0.29, -0.96,
-0.75, 0.15, 0.91, 0.84, -0.01, -0.85, -0.91, -0.13, -0.76,
0.96, 0.27, -0.66, -0.99, -0.40, 0.55, 1.00, 0.53, -0.43,
-0.99, -0.64, 0.30, 0.96, 0.75, -0.16, -0.92, -0.83, -0.02,
0.85, 0.90, 0.12, -0.77, -0.95, -0.26, 0.67, 0.99, 0.39,
-0.56, -1.00, -0.52, 0.44, 0.99, 0.64, -0.30, -0.97, -0.74,
0.17, 0.92, 0.83, -0.03, -0.86, -0.90, -0.11, 0.77, 0.95,
0.25, -0.68, -0.99, -0.39, 0.57, 1.00, 0.51, -0.44, -0.99,
-0.63, 0.31, 0.97, 0.73, -0.18, -0.92, -0.82, 0.04, 0.86, 0.89 };
unsigned long start = 0;
int count = 0;
void setup(){
Serial.begin(115200);
}
void loop(){
while(count<1){
start = micros();
Serial.println(micros());
for(int array = 0; array <= 90; array++){
sine2[array];
}
Serial.println(micros());
count++;
}
}
For those interested, this sketch has a few tiny bugs but it works. Just verify the values are right. The idea was to use the serial monitor to print out pre-formatted code I could copy and paste it into a sketch. I tried to comment the bugs, but hopefully you guys like the concept.
// this sketch takes 77ms to print all of the sin values
float x = 0.00;
int count = 0;
void setup(){
Serial.begin(115200);
Serial.println("Sine value generator");
delay(100);
}
void loop(){
while(count < 1){
int total = 0;
Serial.println("Starting. Just copy and paste the array into your code. \nArray's are zero indexed and I have 90 values. Just add 1 from your index to get the right value");
Serial.println("");
unsigned long start = millis();
Serial.print("Time = ");
Serial.print(start);
Serial.println("ms");
Serial.println(" ");
Serial.print("float sine[91] = {");
for( x = 0; x <= 84; x += 1){ // 84 + 8 = 92. If you use 90, it makes 98 values.. needs to be fixed
int wait = 0;
for(wait = 0; wait <= 8; wait++){ // I wanted the values to come out as an 8 x something block instead of a long line
Serial.print(sin(x)); //sine is in radians!
Serial.print(", ");
x++;
}
x = x-1; // if you do not subtract 1 from x, you will loose a sin value every 8 cycles.
Serial.println(""); // this is to preformat the text block for you but it is a little buggy
Serial.print(" ");
}
Serial.println("};");
Serial.println("Just remove the last comma from the array");
Serial.println("Self assembling code");
total = millis() - start;
Serial.print("Total time = ");
Serial.print(total);
Serial.println("ms");
Serial.println(millis());
count++;
}
}