zoukankan      html  css  js  c++  java
  • Arduino 入门之小星星

    原理:通过编号映射不同的频率音频信号,配合延时输出,达到演奏音乐和跑马灯的效果。

    优酷视频链接:https://v.youku.com/v_show/id_XMzcxODQ4NjA3Mg==.html?spm=a2hzp.8244740.0.0

    准备工作:Arduino开发板一块,串口线一根,电脑一台,Arduino ide,8欧喇叭一个,LED若干

    #include "pitches.h"
    /*
      Blink
      Turns on an LED on for one second, then off for one second, repeatedly.
    
      Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO 
      it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
      the correct LED pin independent of which board is used.
      If you want to know what pin the on-board LED is connected to on your Arduino model, check
      the Technical Specs of your board  at https://www.arduino.cc/en/Main/Products
      
      This example code is in the public domain.
    
      modified 8 May 2014
      by Scott Fitzgerald
      
      modified 2 Sep 2016
      by Arturo Guadalupi
      
      modified 8 Sep 2016
      by Colby Newman
    */
    
    int melody[] = {
      NOTE_C4,NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4,NOTE_G4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_D4,NOTE_D4,NOTE_C4,NOTE_G4,NOTE_G4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_D4,NOTE_G4,NOTE_G4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_D4,NOTE_C4,NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4,NOTE_G4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_D4,NOTE_D4,NOTE_C4
    };
    int noteDurations[] = {
      4, 4, 4, 4, 4, 4, 2, 4,4,4,4,4,4,2,4, 4, 4, 4, 4, 4, 2, 4,4,4,4,4,4,2,4, 4, 4, 4, 4, 4, 2, 4,4,4,4,4,4,2
    };
    //bool btnstat=true;
    //bool playing=true;
    int dl=50;
    int pins[5]={3,5,7,9};
    // the setup function runs once when you press reset or power the board
    void setup() {
       //Serial.begin(9600);
      // initialize digital pin LED_BUILTIN as an output.
      for(int i=0;i<4;i++){
        pinMode(pins[i], OUTPUT);
      }    
      pinMode(12, INPUT_PULLUP);
    }
    
    //void setSwitch(){
    //  int stat=digitalRead(12);
    //  //Serial.println(stat);
    //  if(stat==0){
    //    if(btnstat){
    //      btnstat=false;      
    //    }else{
    //      btnstat=true; 
    //    }
    //  }
    //}
    // the loop function runs over and over again forever
    void loop() {
      int stat=digitalRead(12);
      if(stat==1) return;
      for (int thisNote = 0; thisNote < (sizeof(melody)/sizeof(int)); thisNote++) {
        //setSwitch();
        stat=digitalRead(12);
        if(stat==1) return;
        // to calculate the note duration, take one second
        // divided by the note type.
        //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
        int noteDuration = 1000 / noteDurations[thisNote];
        long led=random(0,4);
        digitalWrite(pins[led], HIGH);
        tone(2, melody[thisNote], noteDuration);
    
        // to distinguish the notes, set a minimum time between them.
        // the note's duration + 30% seems to work well:
        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
        digitalWrite(pins[led], LOW);
        // stop the tone playing:
        noTone(2);
      }
      /*
      for(int i=0;i<4;i++){
        for(int j=0;j<3;j++){
          digitalWrite(pins[i], HIGH);   // turn the LED on (HIGH is the voltage level)
          delay(dl);                       // wait for a second
          digitalWrite(pins[i], LOW);    // turn the LED off by making the voltage LOW
          delay(dl); 
        }
        //delay(200);
      }
      delay(500);
      */
    }
  • 相关阅读:
    CSS优化压缩
    CSS clear both清除浮动总结
    jQuery下拉列表二级联动插件
    网站靠什么赚钱?
    程序员到高级程序员的职业生涯
    ie数组不支持indexOf 方法解决
    jquery中的each方法,$.each this.each $.fn.each
    css3写出0.5px的边框
    js 自己创建ready多个可以依次加载
    javascript中的removeEventListener失效问题
  • 原文地址:https://www.cnblogs.com/icoolno1/p/7302281.html
Copyright © 2011-2022 走看看