1

enter image description hereI have to up the frequency by 100 hz by button press for upto 1khz; I am stuck on using the debounce source code. The problem is every time I press button the value for "i" goes to random number instead of going from 400 to 600 to 800 to 1000...to 2000. I am using prescaler 1. I have another question regarding the frequency. How can I get the frequency stable at 100hz per button press.

#define LEDPIN 7
float highTime;
float lowTime;
int freq;
float period;

//uint16_t val2= pulseIn(4, HIGH);

ISR(TIMER1_COMPA_vect) {
  digitalWrite(LEDPIN, !digitalRead(LEDPIN));
}
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 5;    // the number of the pushbutton pin
//const int ledPin = 13;      // the number of the LED pin

// Variables will change:

int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {

  Serial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  //pinMode(ledPin, OUTPUT);

  // set initial LED state
  //digitalWrite(ledPin, ledState);

  pinMode(LEDPIN, OUTPUT);
  // initialize Timer1 (the 16-bit timer) using channel A output compare

  TCCR1A = 0; // set entire TCCR1A register to 0
  TCCR1B = 0; // same for TCCR1B
  TCNT1 = 0; // initialize counter value to 0
  // set compare match register for 100 Hz increments
  OCR1A = 9999; // = 16000000 / (8 * 100) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS12, CS11 and CS10 bits for 1 prescaler
  TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10);

  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);
  sei(); // allow interrupts
  interrupts(); // enable global interrupts:
}


void loop() {

  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);
  int MIN = 400;
  int MAX = 2000;
  int i = 0;
  int change = 200;
  int var = 16000000 / (1 * i) - 1 ;
  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }
  for (int i = 400; i <= 2000; i += change) {
    if ((millis() - lastDebounceTime) > debounceDelay) {




      // whatever the reading is at, it's been there for longer than the debounce
      // delay, so take it as the actual current state:

      // if the button state has changed:
      if (reading != buttonState) {
        buttonState = reading;

        // only toggle the LED if the new button state is HIGH
        //for(int ii=MIN; ii<MAX; ii++){
        //


        if (buttonState == HIGH) {
          Serial.print("Value of i: ");
          Serial.println(i);

          //for(int i=MIN; i <= MAX; i+=change){
          //int var = 16000000 / (1 * (ii*2)) - 1 ;
          buttonState != buttonState;
          TCCR1A = 0; // set entire TCCR1A register to 0
          TCCR1B = 0; // same for TCCR1B
          TCNT1  = 0; // initialize counter value to 0
          OCR1A = 16000000 / (1 * i) - 1; // =  (must be <65536)
          // turn on CTC mode
          TCCR1B |= (1 << WGM12);
          // Set CS12, CS11 and CS10 bits for 1 prescaler
          TCCR1B |= (0 << CS12) | (0 << CS11) | (1 << CS10);
          // enable timer compare interrupt
          TIMSK1 |= (1 << OCIE1A);
          highTime = (pulseIn(4, HIGH));
          lowTime = (pulseIn(4, LOW));
          period = highTime + lowTime;
          freq = 1000000 / period;   //getting frequency with totalTime is in Micro seconds
          Serial.print(freq);
          Serial.print("\n");
        }
        //}
        //  }

      }
    }
  }


  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;

}
3
  • add a layer of separation between button press detection and the frequency adjustment ... use a flag variable ... check for button press at top of loop() ... if a valid button press is detected, then set the flag ... do no other response to button press ... further down in loop(), check for the flag ... if it is set, then adjust frequency and clear the flag
    – jsotola
    Commented Feb 25, 2021 at 21:39
  • can you show me example? Commented Feb 25, 2021 at 21:40
  • 1
    use the Bounce2 library for button handling
    – Juraj
    Commented Feb 26, 2021 at 7:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.