New to this forum, so let me know if I'm doing something incorrectly. I am using code to create a sine wave and it is working fine and showing up on serial plotter. But as you can see in the attached image, there is a break in the sine wave where it fails to go to negative cycle and seems to restart. It's not a problem, but I'd like to understand this.
// y(t) = A sin (wt + p)
// where A = Amplitude, w = angular frequency, t = time, p = phase
void setup() {
// put your setup code here, to run once:
Serial.begin(4800);
}
void loop() {
// put your main code here, to run repeatedly:
for (int t=0; t<360; t++) {
float y1 = 1 * sin( (30.00/180 * t) + 0);
Serial.print(y1);
Serial.print(" ");
Serial.println(" ");
delay(5);
}
}
Please post the test code. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.
The loop function loops (it is called repeatedly by the hidden main program), which means it starts over after the "for" loop contained within it terminates.
I learned this from a tutorial and it got me started using sine wave gen and serial plotter, but I can see that I need to better understand the math and rewrite the code which I will be doing soon - you guys are really giving me an education! Also, I see you are all using a baud rate of 115200 - what is the thinking behind selecting a suitable baud rate? Thanks.
I got this code from a tutorial in order to learn to use Arduino's serial plotter. The replies from this forum have been very educational. I am planning to rewrite using radians. Thanks for this important insight.
Thanks to all who helped educate me on the thinking behind creating a sine wave. I'm now using radians and the included code (and higher baud rate) to more easily create this function.
Helpful, thanks. Question - is this code-generated waveform which I am able to see on the serial plotter, available from one of the pins - maybe pin 8? (I'm using the MEGA 2560) That is, can I connect to one of the pins and ground on the 2560 with my o-scope and see the same waveform the serial plotter is displaying? Or maybe the serial plotter is using communication pins, not analog output pins - so measuring with meter or o-scope would interfere with the waveform?