Little Bird Curriculum / code littlebird.com.au
_09_Buzzer3.ino Arduino (C/C++) 17 lines · 3 views ✦ Open in English IDE Raw Download Embed view

Use a for loop to sweep the buzzer's tone smoothly up and back down. Extension activity — for loops are beyond the core unit.

From the unit Crack the Code · activity PRP 4: Digital output — Buzzer
int i = 200;
const int ALARMSOUNDER = 3;     // the buzzer is on pin D3

void setup() {
  pinMode(ALARMSOUNDER, OUTPUT);
}

void loop() {
  for (i = 200; i < 700; i = i + 2) {   // sweep from a low tone up to a high tone
    tone(ALARMSOUNDER, i, 10);          // play the tone, changing every 10 ms
    delay(10);
  }
  for (i = 701; i > 200; i = i - 2) {   // sweep back down
    tone(ALARMSOUNDER, i, 10);
    delay(10);
  }
}

Embed this snippet

As an iframe:

Or drop it inline with a script tag (injects the highlighted block where included):

Shared from Little Bird Electronics curriculum.

Copied to clipboard