Using potentiometer to control frequency

int iCnt = 0;
int count = 0;
int freq; // Initial frequency
int n;
int z;

void setup() {
  AdcInit();
  InitTimer();
  DDRD = 0xff;
  DDRB = 0xf5;
  PORTB = 0x03;
  Serial.begin(57600);
}

void AdcInit(void){
  ADMUX = 0x40;
  ADCSRA = 0x87;
}
uint16_t adc_read(uint8_t adcx){
  ADMUX &= 0xf0;
  ADMUX |= adcx;
  ADCSRA |= 0x40;
  while ((ADCSRA & 0x40));
  return ADCL | (ADCH<<8);
}
void InitTimer(){
  TCCR2A = 0x02;
  TCCR2B = 0x04;
  OCR2A= 249;
  TIMSK2 = 0x02;
}

ISR(TIMER2_COMPA_vect) {
  n = adc_read(0); // Update n within the ISR

  if (n < 200) {
    freq = n * 12.36 + 3.21 / 2; 
  } else if (n < 400) {
    freq = n * 12.36 + 3.21 / 2 ;
  } else {
    freq = n * 12.36 + 3.21 / 2 ;
  }
}


void firstpattern(int iCnt) {
   for (int i = 0; i <= 7; i++) {
    PORTD |= (1 << i);   
    delay(500); 
    PORTD &= ~(1 << i);  
     delay(500);
  }
}

void secondpattern(int iCnt) {
  for (int i = 0; i < 7; i++) {
    PORTD |= (1 <<  i);  
    delay(500);        
    PORTD &= ~(1 <<  i);       
  }
  for (int i = 7; i >= 0; i--) {
    PORTD |= (1 <<  i); 
    delay(500);              
    PORTD &= ~(1 << i);    
  }
}

void thirdpattern(int iCnt) {
  for (int i = 4; i <= 5; i++) {
    PORTD |= (1 << i);
    delay(500);
  }
  for (int i = 5; i >= 4; i--) {
    PORTD &= ~(1 << i);
    delay(500);
  }
  for (int i = 6; i <= 7; i++) {
    PORTD |= (1 << i);
    delay(500);
  }
  for (int i = 7; i >= 6; i--) {
    PORTD &= ~(1 << i);
    delay(500);
  }
}

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(500);
  }

  if (count > 3) {
    count = 0; // Reset the counter after reaching the limit
  }

  if (count == 1) {
    firstpattern(iCnt);
  }
  if (count == 2) {
    secondpattern(iCnt);
  }
  if (count == 3) {
    thirdpattern(iCnt);
  }
}

I trying to use potentiometer to control the frequency of the led patterns. But I'm not sure what is wrong with my code. Please help

Circuit

Hi @yongen02

welcome to the arduino-forum.
Well done posting your code as a code-section in your very first posting.

Please help us help you.

What behaviour of the code do you observe?
And describe what different behaviour you want from the code

best regards Stefan

1 Like

Hi, @yongen02
Welcome to the forum.

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Can you please post an EXPORT jpg of your schematic from Tinker.
Not every one has that CAD, and when I tried the link, the page was missing.

Thanks... Tom... :grinning: :+1: :coffee: :australia:

2 Likes

Hi Stefan,

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();
  }
}

Hi Tom,

Thanks for the advise. I will send the jpg here.

Thank you

Did I miss where you set the button pins to INPUT_PULLUP?

[quote="yongen02, post:4, topic:1209767"]
PORTB = 0x03;

I have quoted it for you above. Please see.

Thank you!

Nvm

Hi, @yongen02

As you are using mainly machine code commands and not C++ can you increase the number of comments in your code, explaining what you are doing?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

Will this work, using int math?
1023 * 100 = 102,300, which is well above the 32k max of an int.

Not very good with low level code, and I doubt if it's easier/portable.
Why not x = map (0, 1023, 100, 200);
Leo..

Hi Tom,

I have tried to put more comments in my code. I hope this helps.

Thank you.

int count = 0;
int n;

void setup() {
  AdcInit();
  DDRD = 0xFF; //all port d is output
  DDRB = 0xF5; // port b 0-1 is input
  PORTB = 0x03; // pullup resistor
  Serial.begin(57600);
}

int updateBlinkDelay() { //calculate the frequency
  n = adc_read(0);
  Serial.println(n);
  return 100 + (n * 100 / 1023);
}

void AdcInit(void) {
  ADMUX = 0x40;
  ADCSRA = 0x87; 
}

int adc_read(int adcx) //analogread
{
  ADMUX &= 0xf0;
  ADMUX |= adcx;
  ADCSRA |= 0x40;
  while ((ADCSRA & 0x40));
  return ADCL | (ADCH <<8);
}

void firstpattern() { // this is the first pattern for the led 
  for (int i = 0; i <= 7; i++) {
    PORTD |= (1 << i);   
    PORTD &= ~(1 << i);  
    delay(updateBlinkDelay()); //i use the delay here to control the delay from the potientometer
  }
}

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); //analogread for potientiometer
  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();
  }
}


This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.