Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Asynchronous LED
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2025-10-12 12:13:10
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* 1) Use only core Arduino libraries; no hardware; */
- /* match setup() and loop(). 2) Reference */
- /* PROJECT_188; keep requirements generic until */
- /* components are defined. 3) Tie requirements to */
- /* LIBRARIES and PROTOTYPES; avoid sensor/actuator */
- /* claims. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void reportStatus(void);
- // LED scaffolding: simple non-blocking demo using a configurable LED_PIN with optional PWM
- #ifndef LED_PIN
- #define LED_PIN 13 // Modify this to point to your LED (e.g., an external LED on a GPIO pin)
- #endif
- #ifndef USE_PWM
- #define USE_PWM 1 // Set to 0 to use digitalOnly (no PWM) on pins without PWM support
- #endif
- // PWM and pin setup differences between platforms
- #if USE_PWM
- #if defined(ESP32)
- #include <Arduino.h>
- static const int LED_PWM_CHANNEL = 0;
- static const int LED_PWM_RESOLUTION = 8; // 8-bit PWM
- static const int LED_PWM_FREQ = 1000; // 1 kHz
- // Initialize LED PWM module
- void setupLEDModule() {
- pinMode(LED_PIN, OUTPUT);
- ledcSetup(LED_PWM_CHANNEL, LED_PWM_FREQ, LED_PWM_RESOLUTION);
- ledcAttachPin(LED_PIN, LED_PWM_CHANNEL);
- ledcWrite(LED_PWM_CHANNEL, 0);
- }
- void ledOn() {
- ledcWrite(LED_PWM_CHANNEL, 255);
- }
- void ledOff() {
- ledcWrite(LED_PWM_CHANNEL, 0);
- }
- void ledToggle() {
- static bool ledState = false;
- ledState = !ledState;
- ledcWrite(LED_PWM_CHANNEL, ledState ? 255 : 0);
- }
- #else
- // Non-ESP32 platforms with PWM support
- void setupLEDModule() {
- pinMode(LED_PIN, OUTPUT);
- analogWrite(LED_PIN, 0);
- }
- void ledOn() { analogWrite(LED_PIN, 255); }
- void ledOff() { analogWrite(LED_PIN, 0); }
- void ledToggle() {
- static uint8_t pwmVal = 0;
- pwmVal = (pwmVal == 0) ? 255 : 0;
- analogWrite(LED_PIN, pwmVal);
- }
- #endif
- #else
- // No PWM support; use digitalWrite
- void setupLEDModule() {
- pinMode(LED_PIN, OUTPUT);
- digitalWrite(LED_PIN, LOW);
- }
- void ledOn() { digitalWrite(LED_PIN, HIGH); }
- void ledOff() { digitalWrite(LED_PIN, LOW); }
- void ledToggle() {
- static bool state = false;
- state = !state;
- digitalWrite(LED_PIN, state ? HIGH : LOW);
- }
- #endif
- // Optional helper to showcase LED API usage (can be invoked manually if desired)
- void demoBlink() {
- // Simple demonstration sequence (blocking). Include for completeness; not used by main loop.
- ledOn();
- delay(100);
- ledOff();
- delay(100);
- ledToggle();
- delay(200);
- ledToggle();
- }
- void setup(void)
- {
- // Core Arduino initialization; no hardware components used
- // Initialize LED module (scaffolding)
- setupLEDModule();
- // Optional initial state
- ledOff();
- Serial.begin(9600);
- // Do not wait for Serial readiness to avoid potential hangs on some boards
- Serial.println("PROJECT_188: Initializing (core Arduino, no hardware)");
- // Optional status report
- reportStatus();
- }
- /* The main program loop runs repeatedly. It performs a non-blocking toggle of the LED every 500 ms
- and prints a heartbeat over Serial every second to demonstrate non-hardware operation using the core Arduino API. */
- void loop(void)
- {
- static unsigned long ledPrevMillis = 0;
- static unsigned long hbPrevMillis = 0;
- static unsigned long heartbeat = 0;
- const unsigned long LED_INTERVAL = 500; // 500 ms for LED toggle
- const unsigned long HB_INTERVAL = 1000; // 1 second for heartbeat
- unsigned long current = millis();
- // Non-blocking LED toggle every 500 ms
- if (current - ledPrevMillis >= LED_INTERVAL) {
- ledPrevMillis = current;
- ledToggle();
- }
- // Optional heartbeat via Serial every second
- if (current - hbPrevMillis >= HB_INTERVAL) {
- hbPrevMillis = current;
- heartbeat++;
- Serial.print("PROJECT_188 heartbeat: ");
- Serial.println(heartbeat);
- }
- }
- void reportStatus(void)
- {
- // Report current generic status and library usage
- Serial.println("PROJECT_188 status: OK");
- Serial.println("LIBRARIES: Core Arduino");
- Serial.println("PROTOTYPES: setup, loop, reportStatus");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment