I want to read from potentiometers via Attiny24A a single time. So far I managed to read the potentiometer at the start once but I want to read the pot values in a random place of the code. I've written the test code below which blinks an LED according to the potentiometer position:
#include <avr/io.h>
#include <avr/interrupt.h>
#define LEDButtonPin PA3
#define PotEnablePin PA2
#define pot_DEL_pin PA0
#define pot_DUR_pin PA1
volatile int analogResult;
void setup() {
DDRA &= ~(1 << pot_DUR_pin);
DDRA |= ((1 << LEDButtonPin) | (1 << PotEnablePin));
PORTA |= (1 << PotEnablePin); // power up POTs (Pot is not connected to VCC but a pin to save power)
delay(100);
ADMUX |= (1 << MUX0); // ADC1 (PA1) channel is selected
ADCSRB &= ~((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0)); // Free running mode
DIDR0 |= (1 << ADC1D); // Digital input disable to save power
ADCSRA |= (1 << ADEN) | (1 << ADIE) | (1 << ADATE); // ADEN: ADC Enable, ADIE: ADC Interrupt Enable, ADATE: ADC Auto Trigger Enable
sei(); // enable interrupts
ADCSRA |= (1 << ADSC) | (1 << ADPS1) | (1 << ADPS0); //start convertion, lowers ADC clock by 1/8 of CPU clock
}
ISR(ADC_vect) {
analogResult = (ADCH << 8) | ADCL;
}
void loop() {
PORTA |= (1 << LEDButtonPin);
delay(analogResult);
PORTA &= ~(1 << LEDButtonPin);
delay(analogResult);
PORTA |= (1 << LEDButtonPin);
delay(analogResult);
PORTA &= ~(1 << LEDButtonPin);
ADCSRA &= ~(1 << ADEN);
delay(3000);
ADCSRA &= ~(1<<ADIF); // clear flag
ADCSRA |= (1 << ADEN);
ADCSRA |= (1 << ADSC);
}
The code actually first enables free running mode, then reads the analog value, then disables the ADC when the conversion is done. This is a hard way of doing things and ADC reads with a gigantic delay. I turn the potentiometer, but it affects the LED blink rate seconds later. I would be glad if you could tell me a proper way to read from rhe potentiometer a single time.
Datasheet: Attiny24A datasheet
Thanks.
Edit: I tried the code below which is without interrupts but it only reads the pot for a single time at the start. I can't reread.
#include <avr/io.h>
#include <avr/interrupt.h>
#define LEDButtonPin PA3
#define PotEnablePin PA2
#define pot_DEL_pin PA0
#define pot_DUR_pin PA1
volatile int analogResult;
void setup() {
DDRA &= ~(1 << pot_DUR_pin);
DDRA |= ((1 << LEDButtonPin) | (1 << PotEnablePin));
PORTA |= (1 << PotEnablePin); // power up POTs
delay(100);
ADMUX |= (1 << MUX0); // ADC1 (PA1) channel is selected
DIDR0 |= (1 << ADC1D); // Digital input disable to save power
ADCSRA |= (1 << ADPS1) | (1 << ADPS0); // lowers ADC clock by 1/8 of CPU clock
}
void loop() {
ADCSRA |= (1 << ADSC); //start convertion
while (ADCSRA & (1 << ADSC) ); //wait
analogResult = (ADCH << 8) | ADCL; // read val
ADCSRA &= ~(1<<ADIF); // clear flag
PORTA |= (1 << LEDButtonPin);
delay(analogResult);
PORTA &= ~(1 << LEDButtonPin);
delay(analogResult);
PORTA |= (1 << LEDButtonPin);
delay(analogResult);
PORTA &= ~(1 << LEDButtonPin);
ADCSRA &= ~(1 << ADEN);
delay(3000);
ADCSRA |= (1 << ADEN);
}
Edit 2: I fixed minor issues in Edit1 but no help, it still does a single conversion only:
#include <avr/io.h>
#include <avr/interrupt.h>
#define LEDButtonPin PA3
#define PotEnablePin PA2
#define pot_DEL_pin PA0
#define pot_DUR_pin PA1
volatile int analogResult = 200;
void setup() {
setupPots();
}
void loop() {
PORTA |= (1 << LEDButtonPin);
delay(analogResult);
PORTA &= ~(1 << LEDButtonPin);
delay(analogResult);
PORTA |= (1 << LEDButtonPin);
delay(analogResult);
PORTA &= ~(1 << LEDButtonPin);
delay(3000);
readPots();
}
void setupPots() {
DDRA &= ~(1 << pot_DUR_pin);
DDRA |= ((1 << LEDButtonPin) | (1 << PotEnablePin));
DIDR0 |= (1 << ADC1D); // Digital input disable to save power
ADCSRA |= (1 << ADPS1) | (1 << ADPS0); // lowers ADC clock by 1/8 of CPU clock
}
void readPots() {
PORTA |= (1 << PotEnablePin); // power up POTs
delay(100);
ADMUX |= (1 << MUX0); // ADC1 (PA1) channel is selected
ADCSRA |= (1 << ADEN); // Turn on ADC
ADCSRA |= (1 << ADSC); //start convertion
while (ADCSRA & (1 << ADSC) ); //wait for conversion
analogResult = (ADCH << 8) | ADCL; // read val
//ADCSRA &= ~(1 << ADEN); // Turn off ADC
// PORTA &= ~(1 << PotEnablePin); // power down POTs
}
Note: I want to turn off ADC, I am building a battery powered system. But even I commented that part out, ADC still only does a single conversion.