pleasedontcode

Asynchronous LED rev_02

Oct 12th, 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: Asynchronous LED
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2025-10-12 12:13:10
  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. /* START CODE */
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void reportStatus(void);
  37.  
  38. // LED scaffolding: simple non-blocking demo using a configurable LED_PIN with optional PWM
  39. #ifndef LED_PIN
  40. #define LED_PIN 13 // Modify this to point to your LED (e.g., an external LED on a GPIO pin)
  41. #endif
  42.  
  43. #ifndef USE_PWM
  44. #define USE_PWM 1 // Set to 0 to use digitalOnly (no PWM) on pins without PWM support
  45. #endif
  46.  
  47. // PWM and pin setup differences between platforms
  48. #if USE_PWM
  49. #if defined(ESP32)
  50. #include <Arduino.h>
  51. static const int LED_PWM_CHANNEL = 0;
  52. static const int LED_PWM_RESOLUTION = 8; // 8-bit PWM
  53. static const int LED_PWM_FREQ = 1000; // 1 kHz
  54.  
  55. // Initialize LED PWM module
  56. void setupLEDModule() {
  57.     pinMode(LED_PIN, OUTPUT);
  58.     ledcSetup(LED_PWM_CHANNEL, LED_PWM_FREQ, LED_PWM_RESOLUTION);
  59.     ledcAttachPin(LED_PIN, LED_PWM_CHANNEL);
  60.     ledcWrite(LED_PWM_CHANNEL, 0);
  61. }
  62.  
  63. void ledOn() {
  64.     ledcWrite(LED_PWM_CHANNEL, 255);
  65. }
  66.  
  67. void ledOff() {
  68.     ledcWrite(LED_PWM_CHANNEL, 0);
  69. }
  70.  
  71. void ledToggle() {
  72.     static bool ledState = false;
  73.     ledState = !ledState;
  74.     ledcWrite(LED_PWM_CHANNEL, ledState ? 255 : 0);
  75. }
  76. #else
  77. // Non-ESP32 platforms with PWM support
  78. void setupLEDModule() {
  79.     pinMode(LED_PIN, OUTPUT);
  80.     analogWrite(LED_PIN, 0);
  81. }
  82.  
  83. void ledOn() { analogWrite(LED_PIN, 255); }
  84. void ledOff() { analogWrite(LED_PIN, 0); }
  85. void ledToggle() {
  86.     static uint8_t pwmVal = 0;
  87.     pwmVal = (pwmVal == 0) ? 255 : 0;
  88.     analogWrite(LED_PIN, pwmVal);
  89. }
  90. #endif
  91. #else
  92. // No PWM support; use digitalWrite
  93. void setupLEDModule() {
  94.     pinMode(LED_PIN, OUTPUT);
  95.     digitalWrite(LED_PIN, LOW);
  96. }
  97.  
  98. void ledOn() { digitalWrite(LED_PIN, HIGH); }
  99. void ledOff() { digitalWrite(LED_PIN, LOW); }
  100. void ledToggle() {
  101.     static bool state = false;
  102.     state = !state;
  103.     digitalWrite(LED_PIN, state ? HIGH : LOW);
  104. }
  105. #endif
  106.  
  107. // Optional helper to showcase LED API usage (can be invoked manually if desired)
  108. void demoBlink() {
  109.     // Simple demonstration sequence (blocking). Include for completeness; not used by main loop.
  110.     ledOn();
  111.     delay(100);
  112.     ledOff();
  113.     delay(100);
  114.     ledToggle();
  115.     delay(200);
  116.     ledToggle();
  117. }
  118.  
  119. void setup(void)
  120. {
  121.     // Core Arduino initialization; no hardware components used
  122.     // Initialize LED module (scaffolding)
  123.     setupLEDModule();
  124.     // Optional initial state
  125.     ledOff();
  126.    
  127.     Serial.begin(9600);
  128.     // Do not wait for Serial readiness to avoid potential hangs on some boards
  129.     Serial.println("PROJECT_188: Initializing (core Arduino, no hardware)");
  130.     // Optional status report
  131.     reportStatus();
  132. }
  133.  
  134. /* The main program loop runs repeatedly. It performs a non-blocking toggle of the LED every 500 ms
  135.    and prints a heartbeat over Serial every second to demonstrate non-hardware operation using the core Arduino API. */
  136. void loop(void)
  137. {
  138.     static unsigned long ledPrevMillis = 0;
  139.     static unsigned long hbPrevMillis = 0;
  140.     static unsigned long heartbeat = 0;
  141.     const unsigned long LED_INTERVAL = 500; // 500 ms for LED toggle
  142.     const unsigned long HB_INTERVAL = 1000; // 1 second for heartbeat
  143.  
  144.     unsigned long current = millis();
  145.     // Non-blocking LED toggle every 500 ms
  146.     if (current - ledPrevMillis >= LED_INTERVAL) {
  147.         ledPrevMillis = current;
  148.         ledToggle();
  149.     }
  150.  
  151.     // Optional heartbeat via Serial every second
  152.     if (current - hbPrevMillis >= HB_INTERVAL) {
  153.         hbPrevMillis = current;
  154.         heartbeat++;
  155.         Serial.print("PROJECT_188 heartbeat: ");
  156.         Serial.println(heartbeat);
  157.     }
  158. }
  159.  
  160. void reportStatus(void)
  161. {
  162.     // Report current generic status and library usage
  163.     Serial.println("PROJECT_188 status: OK");
  164.     Serial.println("LIBRARIES: Core Arduino");
  165.     Serial.println("PROTOTYPES: setup, loop, reportStatus");
  166. }
  167.  
  168. /* END CODE */
  169.  
Advertisement
Add Comment
Please, Sign In to add comment