Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int buttonPin = 2;
- const int buttonPin2 = 1;
- const int ledPin = 13;
- const int ledPin2 = 12;
- int buttonState = 0;
- int buttonState2 = 0;
- void setup() {
- // initialize the LED pin as an output:
- pinMode(ledPin, OUTPUT);
- pinMode(ledPin2, OUTPUT);
- // initialize the pushbutton pin as an input:
- pinMode(buttonPin, INPUT);
- pinMode(buttonPin2, INPUT);
- }
- void loop(){
- // read the state of the pushbutton value:
- buttonState = digitalRead(buttonPin);
- buttonState2 = digitalRead(buttonPin2);
- // check if the pushbutton is pressed.
- // if it is, the buttonState is HIGH:
- if (buttonState == HIGH) {
- // turn LED on:
- digitalWrite(ledPin, HIGH);
- }
- else {
- // turn LED off:
- digitalWrite(ledPin, LOW);
- }
- if (buttonState2 == HIGH) {
- wars();
- }
- else {
- // empty for now
- }
- }
- void wars() {
- int speakerPin = 3;
- int length = 70;
- String notes[] = {"G4","G4", "G4", "D#4/Eb4", "A#4/Bb4", "G4", "D#4/Eb4","A#4/Bb4", "G4", "D5", "D5", "D5", "D#5/Eb5", "A#4/Bb4", "F#4/Gb4", "D#4/Eb4","A#4/Bb4", "G4", "G5","G4","G4","G5","F#5/Gb5", "F5","E5","D#5/Eb5","E5", "rest", "G4", "rest","C#5/Db5","C5","B4","A#4/Bb4","A4","A#4/Bb4", "rest", "D#4/Eb4", "rest", "F#4/Gb4", "D#4/Eb4","A#4/Bb4", "G4" ,"D#4/Eb4","A#4/Bb4", "G4"};
- int beats[] = { 8, 8, 8, 6, 2, 8, 6 , 2 ,16 , 8, 8, 8, 6, 2, 8, 6, 2, 16,8,6,2,8,6,2,2, 2, 2,6,2,2,8,6,2,2,2,2,6,2,2,9,6,2,8,6,2,16 };
- int tempo = 50;
- int led = 13;
- void playTone(int tone, int duration) {
- for (long i = 0; i < duration * 1000L; i += tone * 2) {
- digitalWrite(speakerPin, HIGH);
- delayMicroseconds(tone);
- digitalWrite(speakerPin, LOW);
- delayMicroseconds(tone);
- }
- }
- void playNote(String note, int duration) {
- String noteNames[] = { "D#4/Eb4", "E4", "F4", "F#4/Gb4", "G4", "G#4/Ab4", "A4", "A#4/Bb4", "B4", "C5", "C#5/Db5", "D5", "D#5/Eb5", "E5", "F5", "F#5/Gb5", "G5", "G#5/Ab5", "A5", "A#5/Bb5", "B5", "C6", "C#6/Db6", "D6", "D#6/Eb6", "E6", "F6", "F#6/Gb6", "G6" };
- int tones[] = { 1607, 1516, 1431, 1351, 1275, 1203, 1136, 1072, 1012, 955, 901, 851, 803, 758, 715, 675, 637, 601, 568, 536, 506, 477, 450, 425, 401, 379, 357, 337, 318 };
- for (int i = 0; i < 29; i++) {
- if (noteNames[i] == note) {
- digitalWrite(13,HIGH);
- playTone(tones[i], duration);
- digitalWrite(13,LOW);
- }
- }
- }
- void setup2() {
- pinMode(speakerPin, OUTPUT);
- pinMode(13, OUTPUT);
- }
- void loop2() {
- for (int i = 0; i < length; i++) {
- if (notes[i] == "rest") {
- delay(beats[i] * tempo);
- } else {
- playNote(notes[i], beats[i] * tempo);
- }
- delay(tempo / 2);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement