Advertisement
learnelectronics

PWM Stuff

Jun 3rd, 2017
3,314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. //Blank PWM @ 50% on
  2. void setup() {
  3. pinMode(9,OUTPUT);
  4.  
  5. }
  6.  
  7. void loop() {
  8.  analogWrite(9,127);
  9.  
  10. }
  11.  
  12. //-------------------------------------------------------------------------
  13. //PWM Controlled by pot
  14. int x=0;
  15. int y=0;
  16.  
  17.  
  18. void setup() {
  19. pinMode(9,OUTPUT);
  20. pinMode(A0,INPUT);
  21.  
  22. }
  23.  
  24. void loop() {
  25. x=(analogRead(A0));
  26. y=(map(x,0,1023,0,255));
  27.  
  28.  analogWrite(9,y);
  29.  
  30. }
  31.  
  32. //--------------------------------------------------------------
  33.  
  34. //PWM with frequency control
  35.  
  36. /*
  37.  * Control Freq of PWM using PWM Library
  38.  * my code: https://pastebin.com/eLWitAki
  39.  * library: http://code.google.com/p/arduino-pwm-frequency-library/downloads/list
  40.  */
  41. #include <PWM.h>
  42.  
  43.  
  44. int led = 9;              
  45. int32_t frequency = 1000;
  46.  
  47. void setup()
  48. {
  49.  
  50.   InitTimersSafe();
  51.  
  52.  
  53.   bool success = SetPinFrequencySafe(led, frequency);
  54.  
  55.  
  56.   if(success) {
  57.     pinMode(13, OUTPUT);
  58.     digitalWrite(13, HIGH);    
  59.   }
  60. }
  61.  
  62. void loop()
  63. {
  64.  
  65.   pwmWrite(led, 127);
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement