// Pin definitions
const int potPin = A0; // Potentiometer input
// Signal Pins
const int pin8_Signal = 8; // Controls NAND 1 (LED 1)
const int pin7_Signal = 7; // Controls NAND 2 (LED 2)
// Enable Pins
const int enable_9 = 9; // Holds NAND 1 Input 2 HIGH
// ==================================================
// TIMER 1: For the Alternating Phases (Uses millis)
// ==================================================
unsigned long previousMillis = 0;
const unsigned long phaseTime = 500; // MANUALLY CHANGE THIS (500 = half second)
bool isPin8Active = true; // Tracks whose turn it is
// ==================================================
// TIMER 2: For Software PWM / Intensity (Uses micros)
// ==================================================
unsigned long previousMicros = 0;
const unsigned long pwmPeriod = 5000; // 5000 microseconds = 200Hz Refresh Rate
bool pwmState = false; // Tracks the fast flickering for dimming
void setup() {
pinMode(pin8_Signal, OUTPUT);
pinMode(pin7_Signal, OUTPUT);
pinMode(enable_9, OUTPUT);
// Set the Enable pins HIGH so the NAND gates act as inverters
digitalWrite(enable_9, HIGH);
// Start with both OFF (HIGH = OFF for inverted NAND)
digitalWrite(pin8_Signal, LOW);
digitalWrite(pin7_Signal, LOW);
}
void loop() {
// ---------------------------------------------------------
// 1. MACRO TIMER: Switch between Pin 8 and Pin 7
// ---------------------------------------------------------
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= phaseTime) {
previousMillis = currentMillis; // Reset timer
isPin8Active = !isPin8Active; // Flip the active pin
}
// ---------------------------------------------------------
// 2. READ POTENTIOMETER FOR INTENSITY ONLY
// ---------------------------------------------------------
int potValue = analogRead(potPin);
// Map pot to how many microseconds the LED should be ON during the PWM cycle
unsigned long onDuration = map(potValue, 0, 1023, 0, pwmPeriod);
// ---------------------------------------------------------
// 3. MICRO TIMER: Generate the PWM signal without delays
// ---------------------------------------------------------
unsigned long currentMicros = micros();
// If a full PWM period (5000us) has passed, start a new cycle
if (currentMicros - previousMicros >= pwmPeriod) {
previousMicros = currentMicros;
pwmState = true; // Start the cycle by turning LED ON
}
// If the ON time for this specific cycle has passed, turn it OFF
if (currentMicros - previousMicros >= onDuration) {
pwmState = false;
}
// ---------------------------------------------------------
// 4. APPLY THE LOGIC TO THE PINS
// ---------------------------------------------------------
if (isPin8Active) {
// It is Pin 8's turn. Pin 7 MUST remain strictly OFF.
digitalWrite(pin7_Signal, HIGH);
// Apply the fast PWM state to Pin 8 (Remember: LOW = ON, HIGH = OFF)
if (pwmState && onDuration > 0) {
digitalWrite(pin8_Signal, LOW);
} else {
digitalWrite(pin8_Signal, HIGH);
}
} else {
// It is Pin 7's turn. Pin 8 MUST remain strictly OFF.
digitalWrite(pin8_Signal, HIGH);
// Apply the fast PWM state to Pin 7
if (pwmState && onDuration > 0) {
digitalWrite(pin7_Signal, LOW);
} else {
digitalWrite(pin7_Signal, HIGH);
}
}
}