#include <avr/io.h>
#include <avr/interrupt.h>
#include <TM1637TinyDisplay.h>
#define CLK_PIN 2 // CLK pin to D0
#define DIO_PIN 0 // DIO pin to D1
#define POT_PIN A2 // Potentiometer pin to A0
#define LED_PIN 1 // LED pin
#define BUTTON_PIN 3 // Pin PB3 for button input
#define BUTTON_PRESSED() (!(PINB & (1 << BUTTON_PIN)))
volatile bool relayOn = false; // Flag to track relay status
volatile uint32_t timerMillis = 0; // Variable to store elapsed milliseconds
volatile uint32_t startTime = 0; // Variable to store the start time when the button is pressed
volatile uint32_t blinkCounter = 0; // Counter for LED blinking
const uint32_t timerResolution = 1000; // Timer resolution in Hz
TM1637TinyDisplay display(CLK_PIN, DIO_PIN);
void setupTimer0() {
// Configure Timer0
TCCR0A = 0; // Clear Timer0 control registers
TCCR0B = 0;
// Set CTC (Clear Timer on Compare Match) mode
TCCR0A |= (1 << WGM01);
// Set prescaler to 64
TCCR0B |= (1 << CS01) | (1 << CS00);
// Set Timer0 compare value for 1ms
OCR0A = 125; // (clock frequency / (prescaler * desired frequency)) - 1
// Enable Timer0 compare interrupt
TIMSK |= (1 << OCIE0A);
}
void setup() {
display.begin();
display.setBrightness(3); // Set brightness to a value between 0 and 7
DDRB |= (1 << LED_PIN);
PORTB |= (1 << BUTTON_PIN);
// Enable global interrupts
sei();
// Setup Timer0
setupTimer0();
}
void loop() {
static int prevCounterValue = 0; // Variable to store the previous counter value
int potValue = analogRead(POT_PIN); // Read the value of the potentiometer
int mappedValue = map(potValue, 0, 1023, 1, 10); // Map the potentiometer value to the range of 1 to 10
if (BUTTON_PRESSED() && !relayOn) {
startTime = timerMillis; // Record the time when the button is pressed
relayOn = true; // Turn on the relay
PORTB |= (1 << LED_PIN);
}
if (relayOn) {
if (timerMillis - startTime >= mappedValue * 60000) {
relayOn = false;
PORTB &= ~(1 << LED_PIN);
}
}
if (relayOn && blinkCounter >= 1000) {
uint32_t elapsedTime = timerMillis - startTime;
uint32_t remainingTime = mappedValue * 60000 - elapsedTime;
int secondsRemaining = remainingTime / 1000;
// Update the display only if the counter value has changed significantly
if (secondsRemaining != prevCounterValue) {
display.showNumberDec(secondsRemaining, false);
prevCounterValue = secondsRemaining; // Update the previous counter value
}
} else if (!relayOn) {
// If the relay is off, display the mapped value
if (mappedValue != prevCounterValue) {
display.showNumberDec(mappedValue, false);
prevCounterValue = mappedValue; // Update the previous counter value
}
}
}
//void loop(){
// if (BUTTON_PRESSED() && !relayOn) {
// startTime = timerMillis; // Record the time when the button is pressed
// relayOn = true; // Turn on the relay
// PORTB |= (1 << LED_PIN);
// }
// int potValue = analogRead(POT_PIN); // Read the value of the potentiometer
// int mappedValue = map(potValue, 0, 1023, 1, 10); // Map the potentiometer value to the range of 1 to 10
// display.showNumberDec(mappedValue, false); // Display the mapped value on the TM1637 display
// //delay(50); // Delay for stability, adjust as needed
// if (relayOn) {
// if (timerMillis - startTime >= mappedValue * 60000) {
// relayOn = false;
// PORTB &= ~(1 << LED_PIN);
// }
// }
// if (relayOn && blinkCounter >= 1000) {
// uint32_t elapsedTime = timerMillis - startTime;
// uint32_t remainingTime = mappedValue * 60000 - elapsedTime;
// int secondsRemaining = remainingTime / 1000;
// // Display the remaining time in seconds
// display.showNumberDec(secondsRemaining, false);
// }
// else if (!relayOn)
// {
// display.showNumberDec(mappedValue, false);
// // delay(50);
// }
// }
ISR(TIMER0_COMPA_vect)
{
timerMillis++; // Increment elapsed milliseconds
blinkCounter++; // Increment blink counter
}
Comments
Post a Comment