pleasedontcode

PWM LED rev_03

Oct 12th, 2025
221
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: PWM LED
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-12 12:16:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* 1) Use only core Arduino libraries; no hardware; */
  21.     /* match setup() and loop().  2) Reference */
  22.     /* PROJECT_188; keep requirements generic until */
  23.     /* components are defined.  3) Tie requirements to */
  24.     /* LIBRARIES and PROTOTYPES; avoid sensor/actuator */
  25.     /* claims. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void reportStatus(void);
  35.  
  36. #ifndef LED_PIN
  37. #define LED_PIN 9
  38. #endif
  39.  
  40. #if (LED_PIN == 3) || (LED_PIN == 5) || (LED_PIN == 6) || (LED_PIN == 9) || (LED_PIN == 10) || (LED_PIN == 11)
  41. #define LED_PWM_AVAILABLE 1
  42. #else
  43. #define LED_PWM_AVAILABLE 0
  44. #endif
  45.  
  46. static uint8_t currentBrightness = 0; // internal brightness tracking (0-255 for PWM; 0/1 for non-PWM)
  47.  
  48. void setupLEDModule();
  49. void ledOn();
  50. void ledOff();
  51. void ledToggle();
  52. void ledSetBrightness(uint8_t level);
  53. void demoBlink();
  54.  
  55. void setup(void)
  56. {
  57.     // Initialize LED module and basic serial for status
  58.     setupLEDModule();
  59.     ledOff();
  60.  
  61.     Serial.begin(9600);
  62.     Serial.println("PROJECT_188 UNO-only LED API initialized");
  63.     reportStatus();
  64. }
  65.  
  66. void loop(void)
  67. {
  68.     static unsigned long ledPrevMillis = 0;
  69.     unsigned long now = millis();
  70.     if (now - ledPrevMillis >= 500) {
  71.         ledPrevMillis = now;
  72.         ledToggle();
  73.     }
  74. }
  75.  
  76. // Implementations
  77.  
  78. void setupLEDModule() {
  79.     pinMode(LED_PIN, OUTPUT);
  80.     if (LED_PWM_AVAILABLE) {
  81.         analogWrite(LED_PIN, 0);
  82.         currentBrightness = 0;
  83.     } else {
  84.         digitalWrite(LED_PIN, LOW);
  85.         currentBrightness = 0;
  86.     }
  87. }
  88.  
  89. void ledOn() {
  90.     if (LED_PWM_AVAILABLE) {
  91.         currentBrightness = 255;
  92.         analogWrite(LED_PIN, 255);
  93.     } else {
  94.         digitalWrite(LED_PIN, HIGH);
  95.         currentBrightness = 1;
  96.     }
  97. }
  98.  
  99. void ledOff() {
  100.     if (LED_PWM_AVAILABLE) {
  101.         currentBrightness = 0;
  102.         analogWrite(LED_PIN, 0);
  103.     } else {
  104.         digitalWrite(LED_PIN, LOW);
  105.         currentBrightness = 0;
  106.     }
  107. }
  108.  
  109. void ledToggle() {
  110.     if (LED_PWM_AVAILABLE) {
  111.         currentBrightness = (currentBrightness == 0) ? 255 : 0;
  112.         analogWrite(LED_PIN, currentBrightness);
  113.     } else {
  114.         currentBrightness = (currentBrightness == 0) ? 1 : 0;
  115.         digitalWrite(LED_PIN, currentBrightness ? HIGH : LOW);
  116.     }
  117. }
  118.  
  119. void ledSetBrightness(uint8_t level) {
  120.     if (level > 255) level = 255;
  121.     if (LED_PWM_AVAILABLE) {
  122.         currentBrightness = level;
  123.         analogWrite(LED_PIN, level);
  124.     } else {
  125.         if (level > 0) {
  126.             digitalWrite(LED_PIN, HIGH);
  127.             currentBrightness = 1;
  128.         } else {
  129.             digitalWrite(LED_PIN, LOW);
  130.             currentBrightness = 0;
  131.         }
  132.     }
  133. }
  134.  
  135. void demoBlink() {
  136.     // Optional demonstration of the API (blocking sequence)
  137.     ledOn();
  138.     delay(100);
  139.     ledOff();
  140.     delay(100);
  141.     ledToggle();
  142.     delay(100);
  143.     ledToggle();
  144. }
  145.  
  146. void reportStatus(void)
  147. {
  148.     Serial.println("PROJECT_188 status: OK");
  149.     Serial.print("LED_PIN: ");
  150.     Serial.println(LED_PIN);
  151.     Serial.print("PWM available: ");
  152.     Serial.println(LED_PWM_AVAILABLE ? "yes" : "no");
  153.     Serial.println("API: ledOn(), ledOff(), ledToggle(), ledSetBrightness(uint8_t), setupLEDModule()");
  154. }
  155.  
  156. /* END CODE */
  157.  
Advertisement
Add Comment
Please, Sign In to add comment