Square Wave Generator with Variable voltage and frequency

Im still new to this arduino and stuff so i hope u all can help me. I doing a project to generate square wave which the value of the voltage(amplitude) and frequency could be change whenever.

From what i read, digitalWrite only able to get the value to HIGH or LOW, and the frequency is somehow related to the delay.

This is as far as the code that i have done, but i can't seem to get the result i wanted.

int PIN = 13; int pin = 12; int freq,k ,X;

void setup() { pinMode(PIN, OUTPUT); X="value of Voltage"; freq="value of Frequency"; k=(1/2*freq); }

void loop() { int state = 0; int k; //time delay

while(1) { if(state == 0) { analogWrite(PIN,0); state = 1; } else { analogWrite(PIN, X); state = 0; } delay(k); } }

after running the code, this error shown up and i don't know whats wrong.

Arduino: 1.6.7 (Windows 7), Board: "Arduino/Genuino Uno"

Sketch uses 1,302 bytes (4%) of program storage space. Maximum is 32,256 bytes. Global variables use 17 bytes (0%) of dynamic memory, leaving 2,031 bytes for local variables. Maximum is 2,048 bytes. avrdude: ser_open(): can't open device "\.\COM1": The system cannot find the file specified.

Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.

This report would have more information with "Show verbose output during compilation" enabled in File > Preferences.

nexusIDP: ... the frequency is somehow related to the delay.

Well, you're not wrong there but I think if you are going to tackle a project (or homework?) then I think you could read up a bit more about frequency, amplitude, types of waveform and so on. The fact that you did not even mention PWM indicates a lack of awareness of the topic you are tackling.

And what about your data types? What do think happens when you divide an integer by 2?

So what was the result you wanted? Presumably to upload your code to the Arduino. I suggest you read the simple guides to get this bit working first! The BLINK program is your best friend.

Oh, and read how to format your code on this forum, the way you have done it is not the way. Go back, do it the way the moderators have requested, and edit your post so it's all nice and neat.

avrdude: ser_open(): can't open device "\.\COM1": The system cannot find the file specified.

You shouldn't need AVRDUDE. If you are running Windows, See [u]this page[/u].

The [u]Blink Example[/u] puts-out a 1/2Hz square wave on pin 13. Shorter delays will give you higher frequency.

There's no true-analog output on the Arduino, so you'll always get ~5V... You can't control the amplitude without external hardware.

analogWrite(), which is PWM, can approximate analog for some applications such as dimming an LED. But it's NOT analog and you won't get a square wave. You'll get pulses at the "wrong" frequency.

... There is a small "error" when using the delay() method because it takes some time to execute the code between delays. At higher frequencies, use millis() or micros() like the Blink Without Delay example for more accurate results.

nexusIDP: avrdude: ser_open(): can't open device "\.\COM1": The system cannot find the file specified.

Problem uploading to board.

The IDE can't find your Arduino device on port COM1

See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.

You might try looking at the trouble shooting guide here: http://www.arduino.cc/en/Guide/Troubleshooting#upload

Most likely your Arduino board is either appearing on a different COM port (go to Tool -> Port -> select port on IDE) or else you don't have the correct device driver installed.

If you're new to this it's generally a good idea to test out the installation using "blink" or other simple example prior to new development work.