1

I am trying to put together a button press counter with my argon device. I have a couple sounds I want to alternate between but can not get a counter to work for me to change songs A and B. I wanted to set song A as counter evens and song B on counter odds. I currently have very little and do not know where to go from where I am at. Not sure how to upload what I have currently.

int button = A0;

int buzzer = A2;

// Defining Sound
int sound1[] = {1700,2500,2800,2000,2500,1500,2000,1800};

int sound2[] = {3800,3600,3400,3200,2400,2600,2800,3000};

//Defining Duration Of Notes

int Duration1[] = {4,2,4,2,4,2,4,2};

int Duration2[] = {2,4,2,4,2,2,4,2};

//Setting Button Count
int bcount = 0;
#define MAX 2

//Creates The Setup Of Code
void setup()
{
    //Creates The Input Of Button Being Pressed
    pinMode (button, INPUT);
    //Creates The Output Of The Buzzer 
    pinMode (buzzer, OUTPUT);
}

void loop() {
    //When Button Is Pressed 
    if (digitalRead(button) == 1) {
        bcount = bcount + 1;
        }
        else if 
                (bcount = 0);
        for (int i = 0; i < bcount; i++) {
            digitalWrite(buzzer, 1);

    if (bcount == MAX) {
        bcount = 0;
        break;
    }
        //Reads Notes
        for (int Note = 0; Note < 8; Note++){
            //Note Duration
            int Duration = 1000/Duration1[Note];
            //Sends Notes To Speaker
            tone(buzzer, sound1[Note], Duration);
            //Sets Delay Between Notes
            int pauseBetweenNotes = Duration * 1.50;
            //Delay Itself
            delay(pauseBetweenNotes);
            //Stop To The Sound
            noTone(buzzer);
        }
    }
        //When Button Is Pressed 2nd Time
      if(digitalRead(button) == 2) {
        //Reads Notes
        for (int Note = 0; Note < 8; Note++){
            //Note Duration
            int Duration = 1000/Duration2[Note];
            //Sends Notes To Speaker
            tone(buzzer, sound2[Note], Duration);
            //Sets Delay Between Notes
            int pauseBetweenNotes = Duration * 1.50;
            //Delay Itself
            delay(pauseBetweenNotes);
            //Stop To The Sound
            noTone(buzzer);
}
}
}
Ashham93
  • 33
  • 5
  • 2
    Please read [ask] with a [mcve] – Richard Critten Feb 15 '20 at 20:45
  • What's an argon? Do you mean this? https://docs.particle.io/argon/ If so, have you seen this? https://docs.particle.io/quickstart/argon/ – DeducibleSteak Feb 15 '20 at 20:45
  • @Ashham93 You need to be specific. If you can provide your code in a way that others can run that is most helpful. – mccurcio Feb 15 '20 at 21:10
  • @DeducibleSteak I did look through the guide and was not able to find what I was needing. – Ashham93 Feb 15 '20 at 22:14
  • @RichardCritten I finally figured out how to upload what I have so far. I have the first sound playing but the second one wont play the sound it just keeps going to the first one – Ashham93 Feb 15 '20 at 22:14
  • @oaxacamatt Here's what I have so far. I am a green hat when it comes to C++. One of the first time I've played around with it – Ashham93 Feb 15 '20 at 22:15
  • @Ashham93 Excellent restatement of the question. By doing that you received a helpful answer, Excellent work. – mccurcio Feb 17 '20 at 03:45

2 Answers2

1

Try this:

int counter = 0;
void loop() {
    if (digitalRead(button) == HIGH) {
        counter = counter + 1;
        if (counter % 2 == 1) {
            // ... place your code for sound#1 here.
        } else {
            // ... place your code for sound#2 here.
        }
    }
}

What's going on here is: we're incrementing a counter every time the button is pressed. If the number of times the button has been pressed is odd, we play the first sound, if it's even, we play the second sound.

It's important to store the counter outside the loop function, so that it does not lose it's value between calls of loop.

Also, notice that digitalRead returns HIGH if the button is pressed, or LOW if it is not. Any counting of presses beyond that you have to do yourself.

https://docs.particle.io/reference/device-os/firmware/argon/#digitalread-

DeducibleSteak
  • 1,398
  • 11
  • 23
1

Thanks to the help of @DeducibleSteak the code works now to swap between the two sounds.

int button = A0;

int buzzer = A2;

// Defining Sound
int sound1[] = {1700,2500,2800,2000,2500,1500,2000,1800};

int sound2[] = {3800,3600,3400,3200,2400,2600,2800,3000};

//Defining Duration Of Notes

int Duration1[] = {4,2,4,2,4,2,4,2};

int Duration2[] = {2,4,2,4,2,2,4,2};

//Setting Button Count
int bcount = 0;
#define MAX 2

//Creates The Setup Of Code
void setup()
{
    //Creates The Input Of Button Being Pressed
    pinMode (button, INPUT);
    //Creates The Output Of The Buzzer 
   pinMode (buzzer, OUTPUT);
}
//Counter Setup
int counter = 0;

void loop() {
    if (digitalRead(button) == HIGH) {
        ++counter;

        if (counter % 2 == 1) 
        {
            for (int Note = 0; Note < 8; Note++){
            //Note Duration
            int Duration = 1000/Duration1[Note];
            //Sends Notes To Speaker
            tone(buzzer, sound1[Note], Duration);
            //Sets Delay Between Notes
            int pauseBetweenNotes = Duration * 1.50;
            //Delay Itself
            delay(pauseBetweenNotes);
            //Stop To The Sound
            noTone(buzzer);
        }
            } 
        else {
                for (int Note = 0; Note < 8; Note++){
                //Note Duration
                int Duration = 1000/Duration2[Note];
                //Sends Notes To Speaker
                tone(buzzer, sound2[Note], Duration);
                //Sets Delay Between Notes
                int pauseBetweenNotes = Duration * 1.50;
                //Delay Itself
                delay(pauseBetweenNotes);
                //Stop To The Sound
                noTone(buzzer);
            }
        }
    }
}
Ashham93
  • 33
  • 5