how to write a 5v-0v pulse @ 2000 Hz
im using arduino duemilanove and i need to wirte a 2kHz pulse, also how can i change the duty cycle by using PWM. can someone pls help?
Try starting by reading this:
Normal PWM is about 500Hz.
Have you looked at Tone()?
http://arduino.cc/en/Reference/Tone
It claims to be a 50% duty cycle.
2000 Hz is 500uS period.
So one way:
byte outputPin = 2; // or whatever you use
byye pinState;
unsigned long changeTime; // 32 bit number
void setup(){
pinMode (outputPin, OUTPUT);
}
void loop(){
if (micros() >= changeTime){
changeTime = changeTime + 250; // set up next transition time
pinState = 1-pinstate; // 1,0,1,0,1,0
digitalWrite (outputPin, pinState); // other ways to do this too, some significantly faster
}
}
That bit of code
pinState = 1-pinstate;
Is interesting. I have always used
pinState = !pinState;
Is there an efficiency advantage to your method?
RandallR:
That bit of codepinState = 1-pinstate;Is interesting. I have always used
pinState = !pinState;Is there an efficiency advantage to your method?
I don't think there's a difference (in what? program size/execution speed/beauty).
Officially, pinState should be a boolean, not a byte.
pinState = !pinState; is correct and nothing can be faster than that, IMO. But I would never zing our MVM CrossRoads
I would not try to ZING anyone. There are always gaps in my knowledge and I am always looking to learn more.
As my teach used to say "The only dumb question is the one you don't ask".
I don't know if
pinstate = !pinstate
or
pinstate = 1 - pinstate
is faster or takes less room or whatever. Retrolefty showed me that, I thought it was neat & definitely keeps the value at 0 or 1.
CrossRoads:
pinstate = 1 - pinstate
...definitely keeps the value at 0 or 1...
..if it never is anything else than 0 or 1.
Once you set it to 0xff or 255 or -1, it's hard to reach the equivalent to false by doing x = 1-x ![]()
BTW: Spying Arduino.h, I see that byte and boolean are the same.
And in fact, a boolean can have 256 different values !
It's time to study C basics again: In Ansi C there is no boolean (nor bool nor byte), unless you include its definition header file.
And as I am about C basics: ! is the logical not : turns everyting != 0 to 0 and turns a 0 to a 1 (independent of the bool data type)
(~ is the bitwise not)
Might be interesting to see how much machine code and how many cycles it takes comparing x= 1-x with x = ! x
Thanks for triggering me to dig into the basics ![]()
These little chips are amazing. So much power in such a small package. However, they also have significant limitation. So I try to make a habit of writing the most efficient code I can. For example, I won't use and 'int' if a 'char' will do. It may only save one byte of storage and a few instructions but I may need that RAM, flash, or processor cycles.
Some would say, "Don't worry about it until it becomes and issue". But that is the wrong time to try and go back and optimize your code. Do it right the first time and you won't need to do it over.