push buttons to change frequency

how would I increase or decrease PWM pulse length by using push buttons instead of a potentiometer? im trying to make a good quality strobescope and when I use a pot the pulse is very inconsistent and super sensitive. like for example have a pot to do a rough tune then use the buttons to increase the frequency by 1hz every time you press it or something like that. could anybody post some code or tell me what to do?

okay heres the code

int potentiometer_pin = 2;

int led_pin = 11;

int on_time = 80;

int analog_value_multiplier = 8;

int minimum_delay = 200;

int strobe_delay = 0;

void setup() {

pinMode(led_pin, OUTPUT);
}

void loop() {

strobe_delay = minimum_delay + analogRead(potentiometer_pin) * analog_value_multiplier;

digitalWrite(led_pin, HIGH);

delayMicroseconds(on_time);

digitalWrite(led_pin, LOW);

delayMicroseconds(strobe_delay - on_time);
}

I suggest that you post the code that you have that uses the pot.
Presumably moving the pot causes a variable to change. If so then using buttons to change the variable should be easy.

Untested...

static unsigned long Previous;

void setup( void )
{
  pinMode( 13, OUTPUT );
}

void loop( void )
{
  unsigned long Interval;
  unsigned long Current;

  Interval = analogRead( A0 );

  if ( Interval > 0 )
  {
    Current = millis();

    if ( Current - Previous >= Interval )
    {
        digitalWrite( 13, ! digitalRead( 13 ) );
        Previous += Interval;
    }
  }
  else
  {
    digitalWrite( 13, LOW );
  }
}

Frequency is between 1/1023 Hz and 1/1 Hz.

I assume the reading you're getting from the pot is noisy/unstable and that's causing the strobe timing to be inconsistent.

I had this problem in a project recently. To solve it, I added a "deadzone" to the pot where the value that's determined by the pot won't change unless the reading from the pot changes by over a certain amount. This greatly reduces the precision of the pot but, lucky for you, that seems to be a goal rather than a problem.

I also added some code for the buttons with some debouncing.

int potentiometer_pin = 2;  
int led_pin = 11;  
int on_time = 80;  
int analog_value_multiplier = 8;  
int minimum_delay = 200;  
int strobe_delay = 0;

// Added for pot// 
int prev_temp_strobe_delay
int deadZone = 20; // Change deadzone as needed.
//--------------//

// Added for buttons //
int downButton = 4;
int upButton = 5;
long buttonTimer;
long buttonInterval = 500; // The button being pressed will only have an effect once every X milliseconds.
//-------------------//

void setup() 
{ 
  pinMode(led_pin, OUTPUT);  
}

void loop() 
{
  int temp_strobe_delay = analogRead(potentiometer_pin);
  if ((prev_temp_strobe_delay + deadZone) < temp_strobe_delay || (prev_temp_strobe_delay - deadZone) > temp_strobe_delay) 
  {
    prev_temp_strobe_delay = temp_strobe_delay;
    strobe_delay = temp_strobe_delay;
  }
  
  if (digitalRead(upButton) == 1)
  {
    if (millis() > buttonTimer + buttonInterval)
    {
      strobe_delay = strobe_delay + 1;
      prev_temp_strobe_delay = prev_temp_strobe_delay + 1;
      buttonTimer = millis();
    }
  }
    if (digitalRead(downButton) == 1)
  {
    if (millis() > buttonTimer + buttonInterval)
    {
      strobe_delay = strobe_delay - 1;
      prev_temp_strobe_delay = prev_temp_strobe_delay - 1;
      buttonTimer = millis();
    }
  }
  
  strobe_delay = minimum_delay + strobe_delay * analog_value_multiplier;
    
  digitalWrite(led_pin, HIGH);
  delayMicroseconds(on_time);
  digitalWrite(led_pin, LOW);
  delayMicroseconds(strobe_delay - on_time); 
}

Keep in mind that I didn't have a project to test this on so there very well may be some mistakes in there. You should be able to get the general idea though.

Also, something to think about is that, in reality, your delay after writing the LED low is (strobe_delay - on_time + the time it takes to read the the inputs and do any calculations). It's probably so small that it doesn't matter but now you know.

Also, I just realized that with the code I posted above, once

strobe_delay > temp_strobe_delay - deadZone

(or)

strobe_delay < temp_strobe_delay + deadZone

due to button presses, strobe_delay will reset back to the value read from the pot. Some modification of the code should fix that pretty easily though.

Snowman815901:
I assume the reading you're getting from the pot is noisy/unstable and that's causing the strobe timing to be inconsistent. I had this problem in a project recently. To solve it, I added a "deadzone" to the pot where the value that's determined by the pot won't change unless the reading from the pot changes by over a certain amount.

Averaging the previous N values would probably work better.

Determining the source of the noise and eliminating it would probably work better.

Averaging the previous N values would probably work better.

Determining the source of the noise and eliminating it would probably work better.

True.

In my project at least, I didn't know how to eliminate the noise and even with averaging I was getting small but unacceptable variations. I came up with this and worked for me.

Snowman815901:
In my project at least, I didn't know how to eliminate the noise...

There are general solutions and specific solutions. Specific solutions require measurement. I suspect you do not have the means.

General solutions can often be had by posting a schematic and a description of the problem here...
General Electronics

I suspect you just need to Debounce the button presses, so that one press results in one change, and not multiple changes as the button contacts mechanically bounce when pressed.
There is a good picture of a signal from button bounce here

An example that uses software to debounce button pushes
http://playground.arduino.cc/Learning/SoftwareDebounce

There are many ways to resolve this using software,