码迷,mamicode.com
首页 > 其他好文 > 详细

Arduino 入门之小星星

时间:2017-08-08 00:41:42      阅读:543      评论:0      收藏:0      [点我收藏+]

标签:under   spec   git   tns   att   fit   connect   div   uil   

准备工作:Arduino开发板一块,串口线一根,电脑一台,Arduino ide

#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);
  */
}

 

Arduino 入门之小星星

标签:under   spec   git   tns   att   fit   connect   div   uil   

原文地址:http://www.cnblogs.com/icoolno1/p/7302281.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!