Quizz...

This is an intentionally not commented small program:

float y;
float x;
float z;

void setup() 
{
// Enable Serial Output
Serial.begin(9600);
y=100;
x=0;
z=0.05;
}

void loop() 
{
x=x+(y*z);
y=y-(x*z);
Serial.println (x);
delay(30);
}

Quiz:

What is it doing? Try to figure out.

After that, you may upload it and run the serial plotter.

:)

Isn't this supposed to be a sine wave?

It's a waste of a good Arduino and our time!

Mark

Bonus marks: Edit - I made it too obvious. Can you spot the bug?

Yes. You have neglected the second order term (z*z/2) so it will spiral rather than go round a circle and the assignments to x and y should use a temporary variable so that both expressions use the old versions of x and y.

Bressenham's algorithm does this better and in integer arithmetic.

MarkT:
Yes. You have neglected the second order term (z*z/2) so it will spiral rather than go round a circle
and the assignments to x and y should use a temporary variable so that both expressions use the
old versions of x and y.

Bressenham’s algorithm does this better and in integer arithmetic.

Wrong, just wrong.

Bressenham’s algorithm, has nothing, really nothing to do with a sine generation.

Well it can generate a x,y coords of a circle, so it has something to do with it, but not linear with the angle. How are we to tell this post isn't about drawing a circle?

If you want sine/cosine, then CORDIC is the way to do it.

More

RIN67630:
Bressenham’s algorithm, has nothing, really nothing to do with a sine generation.

Nothing?

If I want sine, I table integers with as many places as I need. I generate the table I need on the PC and paste into my source.

const word sineTbl[ 90 ] PROGMEM = { // sine x 500 to 3 places for blinking led sinewave timer
   0,    9,   17,   26,   35,   44,   52,   61,   70,   78,
  87,   95,  104,  112,  121,  129,  138,  146,  155,  163, 
 171,  179,  187,  195,  203,  211,  219,  227,  235,  242, 
 250,  258,  265,  272,  280,  287,  294,  301,  308,  315, 
 321,  328,  335,  341,  347,  354,  360,  366,  372,  377, 
 383,  389,  394,  399,  405,  410,  415,  419,  424,  429, 
 433,  437,  441,  446,  449,  453,  457,  460,  464,  467, 
 470,  473,  476,  478,  481,  483,  485,  487,  489,  491, 
 492,  494,  495,  496,  497,  498,  499,  499,  500,  500 
};

Arduino floats are about 100 times slower than using more accurate (9 places, not 6) longs.

Table lookup is instant compared to generate the value every time, however you do that. That is how primitive flight sims ran on 1 MHz 6502's.

Uno has 32K flash space. What the bootloader and sketch don't use is okay for PROGMEM data.

AWOL: Nothing?

Yes, nothing. That algorithm is a pure graphical presentation technique, it has nothing, but absolutely nothing to do with a sine generation.

Ever wondered why sine and cosine etc are termed "circular functions" ?

You have to bend the algorithm to get the circle so really it is not 'about' the circle.

I must say that I am baffled to see how in that forum you love to be destructive.

I brought a not-so-well known example on how to quickly realise a sinus function without using any trigonometrical function and your response here is to find the very last detail why that one could possibly not be the absolutely perfect solution in any aspects, providing solutions based on arrays, and discussing about algorithms to optimise the presentation…

When I see the example sketch that was displayed in the promo graphic for the release of v1.6.6 Arduino Serial plotter, whereas they used even the math library to do the job, I just thought that it can be done much easier:

//Simple Sine Wave

#include "math.h"       // This will allow us to run the "sin" function.

void setup() {

  // Enable Serial Output
  Serial.begin(9600);
  while (!Serial);

}

void loop() {

  // Describe the upward swing of the sine wave
  for (double i = -1.0; i <= 1.0; i = i + 0.1) {
    Serial.println (sin(i));
    delay(10);
  }

  // Describe the downward swing of the sine wave
  for (double i = 1.0; i >= -1.0; i = i - 0.1) {
    Serial.println (sin(i));
    delay(10);
  }
  
}

The simple code

x=x+(y*z);
y=y-(x*z);

just does the same job in two lines without any trigonometry.

Can anyone explain why?
Regards

I must say that I am baffled to see how in that forum you love to be destructive.

No, we just love to find alternate ways of being constructive.

// Describe the upward swing of the sine wave
...
  // Describe the downward swing of the sine wave
..

Why would anyone write a sine wave generator as two separate loops?

Can anyone explain why?

Did you see my earlier post about circular functions?

AWOL: Why would anyone write a sine wave generator as two separate loops?

Good question, why? and that in the sketch promoting the own Arduino's IDE's serial plot?

Did you see my earlier post about circular functions?

That was not an explanation at all. Would you please mind to explain what you meant? The submitted code precisely contains none of the circular functions, but it works nevertheless. Why?

(by the way: I do know why.)

Good question, why? and that in the sketch promoting the own Arduino’s IDS serial plot?

Ian Duncan Smith?
What’s has the Secretary of State for Work and Pensions go to do with it?

RIN67630: The simple code

x=x+(y*z);
y=y-(x*z);

just does the same job in two lines without any trigonometry.

Can anyone explain why? Regards

I presented table lookup as a much faster alternative, more than 100x faster last time I checked with a do-it-a-million-times-each-way sketch.

GoForSmoke: I presented table lookup as a much faster alternative, more than 100x faster last time I checked with a do-it-a-million-times-each-way sketch.

Did that answer the question?

By the way: my example is [u]definitively much faster[/u]: i had only two lines of code to type :P

: my example is definitively much faster: i had only two lines of code to type

Idiot.