First of all let's correct some basic parts of the code:
const pi = 3.14;
const frequency = 50; // in Hz
"const" means that the variable you declare, can not be changed, but you need a variable type as well (e.g. const float pi = 3.14;)
int amplitude;
const int bias = amplitude;
In this part you declare 2 variables (amplitude ad bias) on startup amplitude is 0 so bias is as well.
Even though I can correct the faults is your code, it is never possible to make a sinewave in de way you want. This is because the mega only knows 1 and 0. It makes "analog" outputs using PWM and not a real analog voltage.
I have changed the program, like this :
float wave;
const pi = 3.14;
const frequency = 50; // in Hz
const phase = 0;
double time;
void setup()
{
pinMode(9,OUTPUT);
}
void loop()
{
time = micros();
wave= 5 * sin((2 * pi * f* time)+ phase ) + 5;
analogWrite(9,wave);
}
but it still do not work, the output wave from pin 9 on arduino is like spwm, not sine wave.
what's wrong with my code?
there is another code to show sine wave in arduinomega2560?
On this page you can read what analogWrite does.
It puts out a pwm signal on (in your case pin 9) with a dutycycle of wave.
Puting out a correct sine wave is a bit more complicated.
Try to google sine wave using pwm. you wil see that it involves much more then you would expect.
Does your 'code' compile...?
That's the frist requirement and I don't see where it possibly could.. generate a 50Hz sinewave..
There are many examples of sinewave generation available on the web and some on the Arduino Playground, Too.
Might be a good idea to look for sample code than ask why non compilable code won't work..
When you correct all compilation errors and calculation errors like this:
float wave;
//const pi = 3.14;//ISO C++ forbids generation of'pi' with no type
const double pi = 355.0/113.0;
//const frequency = 50; // in Hz//C++ forbids generation of'frequency' with no type
const double frequency = 50.0;
//const phase = 0;//ISO C++ forbids declaration of 'phase' with no type
const double phase = 0.0;
const int amplitude = 128;
const int offset = 128;
double time;
void setup(){
pinMode(9,OUTPUT);
}//setup()
void loop(){
time = micros();
// wave= 5 * sin((2 * pi * f* time)+ phase ) + 5;//'f' was not declared in this scope
wave= amplitude*sin((2*pi*frequency*time)+phase)+5;
analogWrite(9,wave);
}//loop()
you will get an output like the attached one which is a PWM representation of a 50 hz sine wave. If you want an analog sine wave you will have to low pass filter the output.
nilton61:
When you correct all compilation errors and calculation errors like this:
float wave;
//const pi = 3.14;//ISO C++ forbids generation of'pi' with no type
const double pi = 355.0/113.0;
//const frequency = 50; // in Hz//C++ forbids generation of'frequency' with no type
const double frequency = 50.0;
//const phase = 0;//ISO C++ forbids declaration of 'phase' with no type
const double phase = 0.0;
const int amplitude = 128;
const int offset = 128;
double time;
void setup(){
pinMode(9,OUTPUT);
}//setup()
void loop(){
time = micros();
// wave= 5 * sin((2 * pi * f* time)+ phase ) + 5;//'f' was not declared in this scope
wave= amplitudesin((2pifrequencytime)+phase)+5;
analogWrite(9,wave);
}//loop()
you will get an output like the attached one which is a PWM representation of a 50 hz sine wave. If you want an analog sine wave you will have to low pass filter the output.
Hello, sorry i can't understant what is 128 value of amplitude and 5 offset? Can explain me better please?