Go Down

Topic: Button Debouncing in Simple Waveform Generator (Read 842 times) previous topic - next topic

suraj_150890

Apr 11, 2016, 05:50 pm Last Edit: Apr 11, 2016, 06:06 pm by suraj_150890
Hello folks,

I have performed an experiment on Simple waveform Generator.
Please refer follwing link:
https://www.arduino.cc/en/Tutorial/DueSimpleWaveformGenerator

I have come across a problem of Button Debouncing and I modified the waveform code. Please find the attachment of my code :

I know the problem is on this instruction:
waveformsTable[wave0]=! waveformsTable[wave0];  // 2D array
I have worked only for Button0 and not for Button1.

I don't know how to analogwrite 2D array for button Debouncing.

Thanks in advance.

MarkT

Can you please use code tags for the sketch.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

suraj_150890

#2
Apr 13, 2016, 11:47 am Last Edit: Apr 13, 2016, 04:00 pm by suraj_150890
Hello MarKt,

 Thanks for your response, I have overcome the solution of Button Debouncing in Simple Waveform Generator. I will post the code which will be useful to other newbies. Markt , "You can also check my code whether I have made any mistakes or Optimisation in the code is still needed."

 I will give short description about the code, Firstly I have integrated Arduino Debounce code in Simple Waveform Generator code. Code explanation is as follows:

[1] Check if Button is Pressed or not.
[2] Determine length of time button has been pressed
[3] Created a debounce function
         If Button pressed > 50ms
         Update the Buttonstate variable.
         If Buttonstate is HIGH, Function call Wave0Select.
         Analogwrite the DAC0 with corresponding waveform.
[4] Finally update the lastButtonState.

Note: I have created function Debounce only for button0 and not for button1 and I have  used         attachInterrupt function linked with debounce function and called a debounce function in loop.
         If I have made any mistakes while posting in the forum I am extremely sorry and Please let me know my mistakes.

Please find the code tag below

Code: [Select]
/*Simple Waveform generator with Arduino Due

  * connect two push buttons to the digital pins 2 and 3
    with a 10 kilohm pulldown resistor to choose the waveform
    to send to the DAC0 and DAC1 channels
  * connect a 10 kilohm potentiometer to A0 to control the
    signal frequency

 */

#include "Waveforms.h"

#define oneHzSample 1000000/maxSamplesNum  // sample for the 1Hz signal expressed in microseconds

const int button0 = 2, button1 = 3;
volatile int wave0 = 0, wave1 = 0;

int i = 0;
int sample;

// 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 long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers


void setup() {
  analogWriteResolution(12);  // set the analog output resolution to 12 bit (4096 levels)
  analogReadResolution(12);   // set the analog input resolution to 12 bit

   attachInterrupt(button0, debounce, RISING);  // Interrupt attached to the button connected to pin 2
  //attachInterrupt(button1, wave1Select, RISING);  // Interrupt attached to the button connected to pin 3

}

void loop() {
  // Read the the potentiometer and map the value  between the maximum and the minimum sample available
  // 1 Hz is the minimum freq for the complete wave
  // 170 Hz is the maximum freq for the complete wave. Measured considering the loop and the analogRead() time
  sample = map(analogRead(A0), 0, 4095, 0, oneHzSample);
  sample = constrain(sample, 0, oneHzSample);

 debounce();

  analogWrite(DAC0, waveformsTable[wave0][i]);  // write the selected waveform on DAC0

 // analogWrite(DAC1, waveformsTable[wave1][i]);  // write the selected waveform on DAC1

  i++;
  if(i == maxSamplesNum)  // Reset the counter to repeat the wave
    i = 0;

  delayMicroseconds(sample);  // Hold the sample value for the sample time
}

// function hooked to the interrupt on digital pin 2
void wave0Select() {
  wave0++;
  if(wave0 == 4)
    wave0 = 0;
}

// function hooked to the interrupt on digital pin 3
void wave1Select() {
  wave1++;
  if(wave1 == 4)
    wave1 = 0;
}
void debounce(){
  int reading = digitalRead(button0);

  // 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;

      // Function call Wave0Select
      if (buttonState == HIGH) {
       wave0Select();
      }
    }
  }

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

suraj_150890

Hello,

I have one question in the above code, that When I comment the debounce function from the  loop,I am unable to change the waveform. Actually attachinterrupt function should be directly linked to button0 and debounce function and I should be able to change the waveform but didn't got success.

Go Up