#define BIT_3 (1<<3)
#define BIT_2 (1<<2)
#define BIT_1 (1<<1)
#define BIT_0 (1<<0)
#define TIMER4_ON_BIT PRTIM4;
const int leds[3] = {BIT_2, BIT_1, BIT_0};
// Define the LED pins - CHANGE AND DEL
const int ledPins[] = {47, 48, 49}; // Example pins for LEDs
const int numLeds = 3;
// Define the timer frequency values for different tones
#define OCR4A_VALUE_400HZ 78
#define OCR4A_VALUE_250HZ 125
#define OCR4A_VALUE_800HZ 39
int freq[] = {78, 125, 39, 0};
const int numTones = 4;
#define PORT_OC4A PORTH
// Define task flags
bool taskAActive = false;
bool taskBActive = false;
// Define task durations
const unsigned long taskADuration = 2000; // 2 seconds
const unsigned long taskBDuration = 4000; // 4 seconds
const unsigned long pauseDuration = 1000; // 1 second pause
unsigned long previousMillis = 0;
void setup() {
DDRL |= BIT_2; // bit-wise OR 1<<2 to set pin to output mode
DDRL |= BIT_1;
DDRL |= BIT_0;
//INITIALIZE PIN 6
DDRH |= BIT_3;
//CLEAR TIMER 4 CONTROL REG
TCCR4A = 0;
TCCR4B = 0;
TCCR4B |= BIT_2;
TCCR4A |= 0b01010100; // Toggle OC4A on Compare Match
}
// Task A: LED Sequence
void taskA() {
static int currentLed = 0;
PORTL |= leds[currentLed]; // Turn on current LED
delay(333); // LED on time
PORTL &= !leds[currentLed]; // turn off LED
currentLed = (currentLed + 1) % numLeds; // Move to next LED
// Check if it's time to deactivate task A
if (millis() - previousMillis >= taskADuration) {
taskAActive = false;
previousMillis = millis(); // Reset timer
}
}
// Task B: Timer tone output
void taskB() {
static int currentTone = 0;
OCR4A = freq[currentTone]; // play current tone
delay(1000); //tone on time
currentTone = (currentTone + 1) % numTones; // move to next tone
// Check if it's time to deactivate task B
if (millis() - previousMillis >= taskBDuration) {
OCR4A = 0; // Turn off tone
taskBActive = false;
previousMillis = millis(); // Reset timer
}
}
// Task C: Control the operation of Task A and Task B
void taskC() {
// Check if Task A is active
if (taskAActive) {
taskA();
}
// Check if Task B is active
if (taskBActive) {
taskB();
}
}
void loop() {
// Task A for 2 seconds
taskAActive = true;
previousMillis = millis();
while (millis() - previousMillis < taskADuration) {
taskC();
}
// Task B for 4 seconds
taskBActive = true;
previousMillis = millis();
while (millis() - previousMillis < taskBDuration) {
taskC();
}
// Pause for 1 second
delay(pauseDuration);
}