User Tools

Site Tools


oak:tutorials:buzzer

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Last revision Both sides next revision
oak:tutorials:buzzer [2016/03/25 23:09]
nog3 Adding base content, code to be added.
oak:tutorials:buzzer [2016/03/25 23:28]
nog3 [Circuit:]
Line 22: Line 22:
 We can make a piezo buzzer beep like this in code: We can make a piezo buzzer beep like this in code:
 <​code>​ <​code>​
- // turn pin "​on"​ by making the voltage HIGH)+ // turn pin "​on"​ by making the voltage HIGH)
   digitalWrite(1,​ HIGH);   digitalWrite(1,​ HIGH);
 </​code>​ </​code>​
Line 32: Line 32:
  
 Remember that when connecting a buzzer, you must pay attention to the (+) sign on the top of the buzzer casing. The (+) sign represents the positive connector and is where the red wire from pin 1 must go. If you get this backwards your buzzer may stop working. Remember that when connecting a buzzer, you must pay attention to the (+) sign on the top of the buzzer casing. The (+) sign represents the positive connector and is where the red wire from pin 1 must go. If you get this backwards your buzzer may stop working.
 +
 +You will also want to unplug the buzzer until your Oak has finished uploading the test code, or it may get very noisy indeed!
  
 =====Code:​===== =====Code:​=====
  
 <​code>​ <​code>​
 +//set the buzzer pin
 +int buzzer = 1;
  
 +// the setup function runs once when you reset or power the board
 +void setup() {
 +  // initialize buzzer as an output.
 +  pinMode(buzzer,​ OUTPUT);
 +  ​
 +}
 +
 +// the loop function runs over and over again forever
 +void loop() {
 +  // make 3 dots to make an S
 +  for (int a = 0; a < 3; a++) {
 +    dot();
 +  }
 +  // wait 100 miliseconds after the first S
 +  delay(100);
 +  // make 3 dashes to make an o
 +  for (int b = 0; b < 3; b++) {
 +    dash();
 +  }
 +  // wait 100 miliseconds after the o
 +  delay(100);
 +  // make 3 dots to make an S
 +  for (int c = 0; c < 3; c++) {
 +    dot();
 +  }
 +  //wait 5 seconds before playing again
 +  delay(5000);​
 +}
 +
 +// make a dot noise
 +void dot()
 +{
 +  //turn on the buzzer
 +  digitalWrite(buzzer,​ HIGH);
 +  delay(100);
 +  // turn off the buzzer
 +  digitalWrite(buzzer,​ LOW);
 +  delay(100);  ​
 +}
 +
 +// make a dash noise
 +void dash()
 +{
 +  // turn on the buzzer
 +  digitalWrite(buzzer,​ HIGH);
 +  delay(300);
 +  // turn off the buzzer
 +  digitalWrite(buzzer,​ LOW);
 +  delay(100);
 +}
 </​code>​ </​code>​
  
-Explanation of code+In this example, we create two separate functions //dot()// and //dash()// and use them in for loops to save us having to write the same code over and over again. We also use a variable //buzzer// to declare which pin we are using for the buzzer. 
 + 
 +//dot()// and //dash()// turn on and off the buzzer with different timings to simulate a morse code dot and dash using the //delay()// function which tells the Oak to pause for that many milliseconds.
  
 =====Conclusion:​===== =====Conclusion:​=====
  
-Where to go from here, potential uses, etc.+With this newfound power, perhaps you could make an internet connected morse code system ​to read out important tweets or secret messages ​from your friends.
oak/tutorials/buzzer.txt · Last modified: 2016/03/25 23:42 by nog3