frequency generator with a switch that seems to stop working at random

Hi all,

I have currently got the Arduino to produce 3 different square waves along with a master timer. A 3 way switch changes the frequency along with the master timing.

#include <DueTimer.h>

unsigned int count = 0;

unsigned int count1 = 809; //1512Hz -- close to 990.1Hz
unsigned int count2 = 529; //2520Hz -- close to 1512Hz
unsigned int count3 = 317; //990Hz  -- close to 2523Hz

unsigned int counttrue = 0;

const int inpin1 = 7; 
const int inpin2 = 8; 

volatile int buttonState1 = LOW;
volatile int buttonState2 = LOW;

bool D_Toggle = 0;

void setup(){
	pinMode(13, OUTPUT);
        pinMode(12, OUTPUT);
        pinMode(inpin1, INPUT_PULLUP);// choose the input pin (for a pushbutton) //_Pullup
        attachInterrupt( inpin1, counter, CHANGE);
        pinMode(inpin2, INPUT_PULLUP); 
        attachInterrupt( inpin2, counter, CHANGE);
	Timer3.start(1); // Calls every 1 micro seconds   

void counter(){  

       buttonState1 =!!(PIOC->PIO_PDSR & (1<<22));
       buttonState2 =!!(PIOC->PIO_PDSR & (1<<23));
        if (buttonState1 == LOW && buttonState2 == HIGH) {
              counttrue = count1;  // 1512Hz
         } else if (buttonState1 == HIGH && buttonState2 == HIGH) {
              counttrue = count2;  // 2523Hz 
         } else if (buttonState1 == HIGH && buttonState2 == LOW) {
             counttrue = count3;  // 990.1Hz 
void loop(){
         //I'm stuck in here! help me...

void myHandler(){

        if(count == 1){
          REG_PIOB_CODR = 0x1 << 27;   //sets pin 13 LOW
         else if(count == 5){ 
            REG_PIOB_SODR = 0x1 << 27; //sets pin 13 HIGH
            REG_PIOD_SODR = 0x1 << 8; //toggles pin 12 
            REG_PIOD_CODR = 0x1 << 8;
         else if(count == counttrue){ 
          REG_PIOB_SODR = 0x1 << 27;  //pin 13 stays high till counter counts to max value counttrue
         count = 0;                   //count set to 0

For some reason ( i thought it was debouncing but after trying a few different debouncing code such as delaymicroseconds() and a debounce check) the wave flat lines after switching between the different frequency’s a few times.

Is there an issue with the code that causes it to crash randomly after checking the input from the button or something else going on.



Timer3.start(1); // Calls every 1 micro seconds

There's no way you can call an ISR that frequently!

Quite heavily loaded as MarkT has pointed out. If you just need 3 independently configured square wave outputs (non-synchronized to a timer) then this is possible without using any interrupts at all ... your code could configure the SAM3X hardware to do all the work transparently. For example, if you have several unused serial ports, they could be configured as programmable pulse generators and a timer could be configured to provide the 3rd programmable square wave output.

Regarding debouncing, this feature is also available in hardware as it's built in to the SAM3X.

Take a look at my post from last summer, search for “Piano tones micros”.
Uses micros() and blink without delay to create independently configured square wave outputs (non-synchronized to a timer). Due should be able to handle the 4200 Hz that I used for the highest piano note.
Instead of making a tone when a key is pressed, just set the flag to be always on and let it free run.

piano13keyBurstFix.ino (11.2 KB)