/*
Using void setup() and void loop():
Sketch uses 460 bytes (5%) of program storage space. Maximum is 8192 bytes.
Global variables use 9 bytes (1%) of dynamic memory, leaving 503 bytes for local variables. Maximum is 512 bytes.
Using int main(void) and while() loop:
Sketch uses 140 bytes (1%) of program storage space. Maximum is 8192 bytes.
Global variables use 0 bytes (0%) of dynamic memory, leaving 512 bytes for local variables. Maximum is 512 bytes.
_delay_ms():
Error if used like this: _delay_ms(delay_time):
The _delay_ms() function in <util/delay.h> only works when the delay time is known at compile time,
*/
#define F_CPU 8000000UL // 8 MHz internal oscillator
#include <avr/io.h>
#include <util/delay.h>
#include <TinyDebug.h>
// Pin Definitions
#define POT_PIN PB2 // ADC1
#define LED_PIN PB1
void adc_init(void) {
// ADMUX – ADC Multiplexer Selection Register
// Set the ADC reference to Vcc (5V) and select ADC1 (PB2) as input
// Table 17-3. Voltage Reference Selections for ADC
// REFS2:X REFS1:0 REFS0:0 VCC used as Voltage Reference, disconnected from PB0 (AREF).
// (REFS bits default to Vcc in ATtiny85, so we don’t set them explicitly)
ADMUX = (1 << MUX0); // MUX0 = 1 selects ADC1 (PB2)
// ADCSRA – ADC Control and Status Register A
// Enable the ADC, set prescaler to 64 for 125 kHz ADC clock (8,000,000 / 64 = 125,000)
// Ideal ADC clock range is 50–200 kHz for good resolution
ADCSRA = (1 << ADEN) // Bit 7 – ADEN: ADC Enable
| (1 << ADPS2) | (1 << ADPS1); // Prescaler = 64
// DIDR0 – Digital Input Disable Register 0
// Disable digital input buffer on ADC1 (PB2) to reduce power/noise
// Strongly recommended for better ADC readings
DIDR0 = (1 << ADC1D);
}
uint16_t adc_read(void) {
// Bit 6 – ADSC: ADC Start Conversion
// In Single Conversion mode, write this bit to one to start each conversion.
ADCSRA |= (1 << ADSC); // Start conversion
while (ADCSRA & (1 << ADSC)); // Wait for conversion to finish
return ADC; // Return combined ADCL/ADCH value (10-bit), ADCL (low byte) and ADCH (high byte)
}
/*
// ===== Using void setup(), and void loop() =====
void setup() {
Debug.begin();
DDRB |= (1 << LED_PIN); // Set LED pin as output
adc_init();
}
void loop() {
uint16_t value = adc_read(); // Read analog value (0–1023)
// Map value of ADC to a delay range: 0 -> 1ms to 1023 -> 511ms
uint16_t delay_time = (value / 2) + 1; // Prevent 0 ms delay
Debug.print("delay_time:"); Debug.println(delay_time);
PORTB |= (1 << LED_PIN); // Turn LED on
delay(delay_time);
PORTB &= ~(1 << LED_PIN); // Turn LED off
delay(delay_time);
}
// ===== Using void setup(), and void loop() =====
*/
//*
// ===== Using int main(void), and while (1) loop =====
int main(void) {
Debug.begin();
DDRB |= (1 << LED_PIN); // Set LED pin as output
adc_init(); // Initialize the ADC
while(1) {
uint16_t value = adc_read(); // Read analog value (0–1023)
// Map value of ADC to a delay range: 0 -> 1ms to 1023 -> 511ms
uint16_t delay_time = (value / 2) + 1; // Prevent 0 ms delay
Debug.print("delay_time:"); Debug.println(delay_time);
// _delay_ms() throws an error message unless used in a for loop.
// Seet ChatGPT for detains
PORTB |= (1 << LED_PIN); // Turn LED on
for (uint16_t i = 0; i < delay_time; i++) {
_delay_ms(1);
}
PORTB &= ~(1 << LED_PIN); // Turn LED off
for (uint16_t i = 0; i < delay_time; i++) {
_delay_ms(1);
}
return 0;
}
// ===== Using int main(void), and while (1) loop =====
//*/