0

I wrote my code to get a beeping sequence but for some reason when I define my count over 650, it does not work anymore. can you see what is my mistake here?

long debounceDelay = 20; // the debounce time
int count =  670;

int beepEvent = 0;

//int beepStep = 20;
//int beepDecrease = 2;
int toggleCounter = 0;
int toneFreq = 400;

// good value: 0.1 (with stepsForASec = 50 and delayTime = 20 and count 10)
float slope = 0.01;

// IMPORTANT: stepsForASec * delayTime should result in 1000 ms 
int stepsForASec = 50;
int delayTime = 20;
int maxTime = stepsForASec * count;
int oldBeepEvent = 0;


void setup() {



  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

  // setup for display
  Serial.begin(9600); // make sure speed of Serial Monitor matches this speed

  // initialize display:
  display.begin();
  display.setPowerSave(0);

  display.setFont(u8x8_font_pxplusibmcgathin_f);// determine text
  // display message on screen
  display.drawString(0, 0, "Start Timer");
  display.draw2x2String(0, 2, "Press");
  display.draw2x2String(0, 4, "Button");


}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // take out noise when button is pressed
  if ((millis() - lastDebounceTime) > debounceDelay) {

    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (buttonState == HIGH) {

      // empty lines 0-5 for timer
      for (int i = 0; i < 6; i++)
      {
        display.clearLine(i);
      }

      // start countdown
      // step is repeated by for loop to ensure a complete countdown
      for (int i = 0; i < (maxTime + 1); i++) {
        if (count >= 0 && i % stepsForASec == 0) { // if counter not finished
          snprintf(buf, 10, "%2d", count);
          display.draw2x2String(4, 3, buf);
          count = count - 1; // decrease counter
        }
        /*
        if (i == beepEvent) {
          // Decide wether to turn on or off the tone
          if (toggleCounter % 2 == 0) {
            tone(buzzerpin, toneFreq);
          }
          else {
            noTone(buzzerpin);
          }
          toggleCounter ++;
          // define the next beepEvent by decreasing number series 
          beepEvent += beepStep;
          beepStep -= beepDecrease;
          if (beepStep < 1) {
            beepStep = 1;
          }
        }
        */




        if (i == beepEvent) {
          oldBeepEvent = beepEvent;

          //Serial.println(beepEvent);
          // Decide wether to turn on or off the tone
          if (toggleCounter % 2 == 0) {
            tone(buzzerpin, toneFreq);
          }
          else {

            noTone(buzzerpin);
          }
          toggleCounter ++;
          // define the next beepEvent by a converging function
          beepEvent = 1.2* maxTime * (1 - 1.0 / float(slope * toggleCounter + 1));
          beepEvent = round(beepEvent);
        }

        // Avoid ending the beeping sequence with a pause

        if (beepEvent - oldBeepEvent <= 1) {
            tone(buzzerpin, toneFreq);
          }

        delay(delayTime);// wait one second 
      }
Share a link to this question (includes your user id)
| edit | | close | delete |
0

Your code looks incomplete... you can send the complete sketch to me by email, then I can have a look.

I would also strongly advise you to have a look at the examples given to realize a countdown timer, as your code looks unnecessary complex.

Share a link to this answer (includes your user id)
| edit | delete |

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.