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);
      */
    }
  • 相关阅读:
    Pulp之四:其它应用样例(1)-一般的整数规划问题 (设置目标约束函数)
    Pulp之三:官网上的应用样例(4)-Blending Problem (混合问题)
    Pulp之三:官网上的应用样例(3)-Sudoku Problem by LP (数独问题)
    Pulp之三:官网上的应用样例(2)-A Set Partitioning Problem (集合划分问题)
    list转换dict的方式以及zip的用法
    Pulp之三:官网上的应用样例(1)-The_Whiskas_Problem (猫粮配料比例问题)
    Pulp之二:Pulp中几个重要的概念
    一个简单有趣的题(4个变量找出取走的数)
    Java之父 James Gosling 发表博文 《Too Soon》纪念乔布斯。
    第01课 OpenGL窗口(1)
  • 原文地址:https://www.cnblogs.com/icoolno1/p/7302281.html
Copyright © 2011-2022 走看看