Advertisement
BeamNG_IRC

Arduino code

Apr 2nd, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1.  
  2. const int buttonPin = 2;
  3. const int buttonPin2 = 1;  
  4. const int ledPin = 13;
  5. const int ledPin2 = 12;
  6. int buttonState = 0;
  7. int buttonState2 = 0;
  8.  
  9. void setup() {
  10.   // initialize the LED pin as an output:
  11.   pinMode(ledPin, OUTPUT);  
  12.   pinMode(ledPin2, OUTPUT);  
  13.   // initialize the pushbutton pin as an input:
  14.   pinMode(buttonPin, INPUT);
  15. pinMode(buttonPin2, INPUT);  
  16. }
  17.  
  18. void loop(){
  19.   // read the state of the pushbutton value:
  20.   buttonState = digitalRead(buttonPin);
  21.   buttonState2 = digitalRead(buttonPin2);
  22.  
  23.   // check if the pushbutton is pressed.
  24.   // if it is, the buttonState is HIGH:
  25.   if (buttonState == HIGH) {  
  26.     // turn LED on:    
  27.     digitalWrite(ledPin, HIGH);  
  28.   }
  29.   else {
  30.     // turn LED off:
  31.     digitalWrite(ledPin, LOW);
  32.   }
  33.   if (buttonState2 == HIGH) {      
  34.     wars();
  35.   }
  36.   else {
  37.     // empty for now
  38.   }
  39. }
  40.  
  41.  
  42. void wars() {
  43.    int speakerPin = 3;
  44.   int length = 70;
  45.   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"};
  46.   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  };
  47.   int tempo = 50;
  48.   int led = 13;
  49.   void playTone(int tone, int duration) {
  50.     for (long i = 0; i < duration * 1000L; i += tone * 2) {
  51.       digitalWrite(speakerPin, HIGH);
  52.       delayMicroseconds(tone);
  53.       digitalWrite(speakerPin, LOW);
  54.       delayMicroseconds(tone);      
  55.     }
  56.   }
  57.  
  58.   void playNote(String note, int duration) {
  59.     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" };
  60.     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 };
  61.     for (int i = 0; i < 29; i++) {
  62.       if (noteNames[i] == note) {
  63.         digitalWrite(13,HIGH);
  64.         playTone(tones[i], duration);
  65.         digitalWrite(13,LOW);
  66.       }
  67.     }
  68.   }
  69.  
  70.   void setup2() {
  71.     pinMode(speakerPin, OUTPUT);
  72.     pinMode(13, OUTPUT);
  73.   }
  74.  
  75.   void loop2() {
  76.     for (int i = 0; i < length; i++) {
  77.       if (notes[i] == "rest") {
  78.         delay(beats[i] * tempo);
  79.       } else {
  80.         playNote(notes[i], beats[i] * tempo);      
  81.       }
  82.       delay(tempo / 2);
  83.     }
  84.   }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement