CCCPaste

PWM LED rev_03

/********* 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: PWM LED
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2025-10-12 12:16:46

********* 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 *****/


/****** DEFINITION OF LIBRARIES *****/

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void reportStatus(void);

#ifndef LED_PIN
#define LED_PIN 9
#endif

#if (LED_PIN == 3) || (LED_PIN == 5) || (LED_PIN == 6) || (LED_PIN == 9) || (LED_PIN == 10) || (LED_PIN == 11)
#define LED_PWM_AVAILABLE 1
#else
#define LED_PWM_AVAILABLE 0
#endif

static uint8_t currentBrightness = 0; // internal brightness tracking (0-255 for PWM; 0/1 for non-PWM)

void setupLEDModule();
void ledOn();
void ledOff();
void ledToggle();
void ledSetBrightness(uint8_t level);
void demoBlink();

void setup(void)
{
	// Initialize LED module and basic serial for status
	setupLEDModule();
	ledOff();

	Serial.begin(9600);
	Serial.println("PROJECT_188 UNO-only LED API initialized");
	reportStatus();
}

void loop(void)
{
	static unsigned long ledPrevMillis = 0;
	unsigned long now = millis();
	if (now - ledPrevMillis >= 500) {
		ledPrevMillis = now;
		ledToggle();
	}
}

// Implementations

void setupLEDModule() {
	pinMode(LED_PIN, OUTPUT);
	if (LED_PWM_AVAILABLE) {
		analogWrite(LED_PIN, 0);
		currentBrightness = 0;
	} else {
		digitalWrite(LED_PIN, LOW);
		currentBrightness = 0;
	}
}

void ledOn() {
	if (LED_PWM_AVAILABLE) {
		currentBrightness = 255;
		analogWrite(LED_PIN, 255);
	} else {
		digitalWrite(LED_PIN, HIGH);
		currentBrightness = 1;
	}
}

void ledOff() {
	if (LED_PWM_AVAILABLE) {
		currentBrightness = 0;
		analogWrite(LED_PIN, 0);
	} else {
		digitalWrite(LED_PIN, LOW);
		currentBrightness = 0;
	}
}

void ledToggle() {
	if (LED_PWM_AVAILABLE) {
		currentBrightness = (currentBrightness == 0) ? 255 : 0;
		analogWrite(LED_PIN, currentBrightness);
	} else {
		currentBrightness = (currentBrightness == 0) ? 1 : 0;
		digitalWrite(LED_PIN, currentBrightness ? HIGH : LOW);
	}
}

void ledSetBrightness(uint8_t level) {
	if (level > 255) level = 255;
	if (LED_PWM_AVAILABLE) {
		currentBrightness = level;
		analogWrite(LED_PIN, level);
	} else {
		if (level > 0) {
			digitalWrite(LED_PIN, HIGH);
			currentBrightness = 1;
		} else {
			digitalWrite(LED_PIN, LOW);
			currentBrightness = 0;
		}
	}
}

void demoBlink() {
	// Optional demonstration of the API (blocking sequence)
	ledOn();
	delay(100);
	ledOff();
	delay(100);
	ledToggle();
	delay(100);
	ledToggle();
}

void reportStatus(void)
{
	Serial.println("PROJECT_188 status: OK");
	Serial.print("LED_PIN: ");
	Serial.println(LED_PIN);
	Serial.print("PWM available: ");
	Serial.println(LED_PWM_AVAILABLE ? "yes" : "no");
	Serial.println("API: ledOn(), ledOff(), ledToggle(), ledSetBrightness(uint8_t), setupLEDModule()");
}

/* END CODE */