I am trying to use potientiometer to control the frequency of the blinking led patterns. I have updated the code as I feel that ISR code is not useful in this for now. But after i tried to make it as a function, it still didn't work. Please help.
int count = 0;
int n;
void setup() {
AdcInit();
DDRD = 0xFF;
DDRB = 0xF5;
PORTB = 0x03;
Serial.begin(57600);
}
int updateBlinkDelay() {
n = adc_read(0);
Serial.println(n);
return 100 + (n * 100 / 1023);
}
void AdcInit(void) {
ADMUX = 0x40;
ADCSRA = 0x87;
}
int adc_read(int adcx)
{
ADMUX &= 0xf0;
ADMUX |= adcx;
ADCSRA |= 0x40;
while ((ADCSRA & 0x40));
return ADCL | (ADCH <<8);
}
void firstpattern() {
for (int i = 0; i <= 7; i++) {
PORTD |= (1 << i);
PORTD &= ~(1 << i);
delay(updateBlinkDelay());
}
}
void secondpattern() {
for (int i = 0; i < 7; i++) {
PORTD |= (1 << i);
PORTD &= ~(1 << i);
delay(updateBlinkDelay());
}
for (int i = 7; i >= 0; i--) {
PORTD |= (1 << i);
PORTD &= ~(1 << i);
delay(updateBlinkDelay());
}
}
void thirdpattern() {
for (int i = 4; i <= 5; i++) {
PORTD |= (1 << i);
delay(updateBlinkDelay());
}
for (int i = 5; i >= 4; i--) {
PORTD &= ~(1 << i);
delay(updateBlinkDelay());
}
for (int i = 6; i <= 7; i++) {
PORTD |= (1 << i);
delay(updateBlinkDelay());
}
for (int i = 7; i >= 6; i--) {
PORTD &= ~(1 << i);
delay(updateBlinkDelay());
}
}
void loop() {
n = adc_read(0);
Serial.println(n);
if ((PINB & 0x01) == 0) { // Check if the button is pressed
// Button is pressed logic here
count++;
delay(100);
}
if (count > 3) {
count = 0; // Reset the counter after reaching the limit
}
if (count == 1) {
firstpattern();
}
if (count == 2) {
secondpattern();
}
if (count == 3) {
thirdpattern();
}
}