I'm new to Arduino and have a small problem with a project of mine.
I'm trying to make a sine wave in Arduino which I have to read into MATLAB (that last part works fine). I searched in other topics for a solution of my specific problem, but could not find it.
The problem is that I make a sine wave according to the general formula (2pif*t). The parameter t is calculated with the millis()/1000.00 function. The frequency f is a parameter I can set for myself. The thing is, the frequency has to be very low to get any values (visible in the serial monitor). I know it has something to do with aliasing, but I'm not sure in what way and how I can calculate the maximum frequency.
float ttime;
float f=1;
float sinserial;
void setup() {
// put your setup code here, to run once:
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
// calculate the time and the sinewave
ttime = millis()/1000;
sinserial = sin(2*PI*f*ttime);
// print the results to the serial monitor:
Serial.print("time = ");
Serial.print(ttime);
Serial.print("\t output = ");
Serial.println(sinserial);
// wait 2 milliseconds before the next loop
delay(2);
}
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
It would be quite difficult to calculate the maximum frequency. Easier to find out by trial and error. If you increase the serial speed, you will be able to get to a higher frequency, e.g. 115200.
Please read the forum guidelines before you break any more rules.
Thanks for your help, but I still don't get it. I think I'm also confused with sampling rate, baud rate, frequencies etc. Can anyone help me with that?
I also changed the f which you can see in the attached figure. I don't understand why the changes occurs.
Since millis() and 1000 are both integers this division will result in an integer number of seconds, even though you then store that integer in a float. If you use a float constant (1000.0) then the result will be a floating point number with fractions of a second.