#include <avr/io.h>
#include <avr/sleep.h>
void disableADC() {
ADCSRA &= ~(1 << ADEN); // Clear ADC enable bit
}
void disableAnalogComparator() {
ACSR |= (1 << ACD); // Set analog comparator disable bit
}
#define INPUT_A_PIN PB5
#define INPUT_B_PIN PB2
#define OUTPUT_A_PIN PB3
#define OUTPUT_B_PIN PB1
#define OUTPUT_C_PIN PB4
// Variable to store the previous state of input pin 2
bool prevInputStateB = LOW;
// Variable to track if output B has been triggered
bool outputBActive = false;
void setup() {
// Add a brief delay to allow the microcontroller to stabilize
delay(300);
// Disable unused peripherals
disableADC();
disableAnalogComparator();
// Disable EEPROM
EECR &= ~(1 << EEMPE); // Clear EEPROM master write enable bit
EECR &= ~(1 << EEPE); // Clear EEPROM write enable bit
// Declare Input and Output Pins
pinMode(INPUT_A_PIN, INPUT_PULLUP);
pinMode(INPUT_B_PIN, INPUT_PULLUP);
pinMode(OUTPUT_A_PIN, OUTPUT);
pinMode(OUTPUT_B_PIN, OUTPUT);
pinMode(OUTPUT_C_PIN, OUTPUT);
}
void loop() {
// Read the state of input pins
bool inputA = digitalRead(INPUT_A_PIN);
bool inputB = digitalRead(INPUT_B_PIN);
// Process input condition when nothing is plugged into the two jacks
if (!inputA && !inputB) {
// If inputA is low and inputB is low, set output A and B low
digitalWrite(OUTPUT_A_PIN, LOW);
digitalWrite(OUTPUT_B_PIN, LOW);
digitalWrite(OUTPUT_C_PIN, LOW);
}
// Process input condition when nothing is Headphone only is plugged in
else if (inputA && !inputB) {
// If inputA is low and inputB is low, set output A and B low
digitalWrite(OUTPUT_A_PIN, HIGH);
digitalWrite(OUTPUT_B_PIN, LOW);
digitalWrite(OUTPUT_C_PIN, LOW);
}
// Process input condition when AV Jack is plugged in
else {
// If inputA is low and inputB is high, set output A and B high
digitalWrite(OUTPUT_A_PIN, HIGH);
digitalWrite(OUTPUT_B_PIN, LOW);
delay(200); // Delay for 200 milliseconds
digitalWrite(OUTPUT_C_PIN, HIGH);
}
// Check for rising edge on video out jack on input B
if (!prevInputStateB && inputB) { // If input B was previously low and is now high
outputBActive = true; // Set output B to be active
}
// Check for falling edge on video out jack on input B
if (prevInputStateB && !inputB) { // If input B was previously high and is now low
outputBActive = true; // Set output B to be active
}
if (outputBActive) {
digitalWrite(OUTPUT_B_PIN, HIGH); // Set output B high
delay(200); // Delay for 200 milliseconds
digitalWrite(OUTPUT_B_PIN, LOW); // Set output B low
outputBActive = false; // Reset the flag for output B
}
// Store the current state of input B for the next iteration
prevInputStateB = inputB;
// Enter idle mode to conserve power
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sleep_cpu();
// Code execution resumes from here when awakened
}