2
votes

I'm currently working on a slot machine for my Arduino, and one of the things I want to happen, is that when the user "pulls" the lever, a dinging sound can be heard, that slows down as time passes.

This is what I have so far, but I can't figure out how to make the delay variable with a countdown.

void ringading(){
    for (int i=10; i>10; i--)
    {
        for (int i=0; i<500; i++)
        {
            digitalWrite(BUZZER_PIN, HIGH);
            delayMicroseconds(1915);
            digitalWrite(BUZZER_PIN, LOW);
        }
        delay(1000);
    }
}

This is probably not the best way to do this, but I know it buzzes 10 times now, each with a one second delay in between. So I basically just need to get that delay to increase.

1

1 Answers

2
votes

Instead of delaying a constant number of milliseconds (1000) delay by a number of milliseconds that is a function of i, such as delay(1000*(10-i)) since i is decreasing.

Also, the larger loop should never run - are you sure you don't mean i>0?

Also also, you should use two different variable names for your two loops:

void ringading(){
  for (int i=10; i>0; i--)
    {
      for (int j=0; j<500; j++)
      {
        digitalWrite(BUZZER_PIN, HIGH);
        delayMicroseconds(1915);
        digitalWrite(BUZZER_PIN, LOW);
      }
      delay(1000*(10-i));
  }
}