Go Down

Topic: Button activated square wave very strange behavior (Read 182 times) previous topic - next topic


HARDWARE: Arduino MEGA1280 made by www.arduino.cc and MEGA2560 r3 made by inland.

SCHEMATIC: Arduino Button Example found here: https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button  Please note I attached an O Scope probe (Tektronix 2205 - yes its old but I got it for free from work) to Pin 13 and ground.

EXPECTED BEHAVIOR: When the button is pushed a square wave should get generated with a period of 2x "toggler_time"  for "square_wave_counter" times. (Please see source code). 

ACTUAL BEHAVIOR: On both Arduino boards (listed above), if I pressing the button right after uploading the code to the board the square wave forms instantly, if I press the button in short succession, everything is OK.  However, if I upload the code and wait 10 seconds or longer or wait between pushing the button the O scope does not show a square wave rather it shows two lines.  After pressing the button for a length of time (the longer the interval between pushing the button the long the length of time needed to push the button)  the square wave finally starts.  I attached screen shots from the O scope of the screen when no button is pressed, of the square wave and the problem state described here.

Any help would be greatly appreciated!

Code: [Select]
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
const long toggle_time = 1100;  // toggle pin every X number of us (MICROSECONDS, square wave is 2x
int square_wave_counter = 1840; //square wave counter, has to start high to wait for pushbutton to zero it.
const int cycle_counter = 1840; //number of  half cycles to run per each button push.

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

void loop() {
  static long last_toggle; //microseconds counter
  static int pin_state = LOW;
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    square_wave_counter = 0;
  //create square wave is counter is below cycle_counter
  if (((long) micros() - last_toggle >= toggle_time) && (square_wave_counter < cycle_counter)) {
    pin_state = !pin_state;       // invert pin state
    digitalWrite(13, pin_state);
    last_toggle += toggle_time;
  } // if

  if (square_wave_counter >= cycle_counter) {
    digitalWrite(ledPin, LOW);


Have you an external pull down resistor on pin 2 ? It could otherwise float and cause maybe the strange effects you have reported.
It is best to use unsigned long for variables which hold micros() values.


I have a 10K resister between ground and pin 2 (button).  Is that what you are thinking?


I have a 10K resister between ground and pin 2 (button).  Is that what you are thinking?
Yes that is it. I see the link to the schematic now works.

Try changing this:

last_toggle += toggle_time ;


last_toggle = (long) micros() ;

and see if there is an improvement. This is because time is not the only criterion conditioning the preceding 'if' statement.


BINGO!!! That was it.  The code works like it should now.  THANK YOU!!!

Go Up