pleasedontcode

Inverted PWM rev_01

Dec 29th, 2025
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Inverted PWM
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2025-12-30 00:36:34
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* reverse duty cycle for output pin no. 11 */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Arduino.h>
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. // Variable for the PWM output
  34. int pwmPin = 11;
  35.  
  36. // System Requirement 1: reverse duty cycle for output pin no. 11
  37. // This means that when duty cycle is 0, PWM output should be maximum (255), and when duty cycle is 255, PWM output should be minimum (0).
  38. // We will create a variable for duty cycle and invert it in the loop.
  39.  
  40. int dutyCycle = 0;
  41. int increment = 1;
  42.  
  43. void setup() {
  44.     // initialize PWM pin
  45.     pinMode(pwmPin, OUTPUT);
  46. }
  47.  
  48. void loop() {
  49.     // change duty cycle
  50.     dutyCycle += increment;
  51.  
  52.     // reverse duty cycle for output pin no. 11
  53.     // invert duty cycle
  54.     int invertedDutyCycle = 255 - dutyCycle;
  55.  
  56.     // write PWM value to pin
  57.     analogWrite(pwmPin, invertedDutyCycle);
  58.  
  59.     // change direction at limits
  60.     if (dutyCycle <= 0 || dutyCycle >= 255) {
  61.         increment = -increment;
  62.     }
  63.  
  64.     // delay for visibility
  65.     delay(10);
  66. }
  67.  
  68. /* END CODE */
  69.  
Advertisement
Add Comment
Please, Sign In to add comment