Frequency increments by button press

I am stuck with a problem which uses 50% duty cycle in timer1 and I need to increment the frequency by 100hz everytime I press a button. I am using the debounce for the button press. I thought I had to divide OCR1A by 2 but it is not the case.

/*
Debounce

Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
 minimum delay between toggles to debounce the circuit (i.e. to ignore noise).

The circuit:
 - LED attached from pin 13 to ground
 - pushbutton attached from pin 2 to +5V
 - 10 kilohm resistor attached from pin 2 to ground

- Note: On most Arduino boards, there is already an LED on the board connected
 to pin 13, so you don't need any extra components for this example.

created 21 Nov 2006
 by David A. Mellis
 modified 30 Aug 2011
 by Limor Fried
 modified 28 Dec 2012
 by Mike Walters
 modified 30 Aug 2016
 by Arturo Guadalupi

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Debounce
 */

#define LEDPIN 3
 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 ledState = HIGH; // the current state of the output pin
 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 8 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);
 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");
 delay(1000);
 // 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();
 }

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
 if (buttonState == HIGH) {
 highTime = (pulseIn(4, HIGH));
 lowTime = (pulseIn(4, LOW));
 period = highTime + lowTime;
 freq = 4000000 / period; //getting frequency with totalTime is in Micro seconds
 Serial.print(freq);
 delay(10);

}
 }
 }



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

You are delaying 1000 msec every time through loop so your debounce is kind of pointless.

You never adjust the value of OCR1A so how to you expect the output pulse to change?

I'm not sure what the pulseIn() functions are doing compared to your problem description.
This code

 // Set CS12, CS11 and CS10 bits for 8 prescaler
  TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10);

does not do what you think it does. You can not OR a 0 into a value. It does nothing and leaves that bit unchanged.

Thanks for your reply. Well I had problem earlier which supposed to generate 100hz of squarewave with 50% duty cycle and I had to measure it in pin 3 and pin 4. Now in this problem I have to use debounce to tick up frequency by 100hz everytime I press button.

Here is the original code: it generates 100hz

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

//uint16_t val2= pulseIn(4, HIGH);
      
ISR(TIMER1_COMPA_vect) { digitalWrite(LEDPIN, !digitalRead(LEDPIN)); }
void setup() {
Serial.begin(9600);
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 8 prescaler
 TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10);

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

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);
delay(1000);


Serial.print("\n");

}

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