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[] = {
, , , , , , , ,,,,,,,, , , , , , , ,,,,,,,, , , , , , , ,,,,,,
};
//bool btnstat=true;
//bool playing=true;
int dl=;
int pins[]={,,,};
// 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=;i<;i++){
pinMode(pins[i], OUTPUT);
}
pinMode(, 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();
if(stat==) return;
for (int thisNote = ; thisNote < (sizeof(melody)/sizeof(int)); thisNote++) {
//setSwitch();
stat=digitalRead();
if(stat==) 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 = / noteDurations[thisNote];
long led=random(,);
digitalWrite(pins[led], HIGH);
tone(, 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();
}
/*
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 入门之小星星的更多相关文章
- Arduino入门套件 Arduino UNO R3
限时大抢购:原价5.6元超声波模块HC-SR04,现只需3元,亏本大甩卖,只赚人气,如需购买的客户请直接拍下以下链接3件=3元购买,备注超声波模块即可,每个客户ID每天仅限购一个.http://det ...
- Arduino入门学习
一直听到许多做物联网.智能家居的控制器使用的是Arduino,从师兄那里拿到了一块Arduino开发板,进行了一下午的学习,感觉这个适合小孩子们玩:) 废话少说,总结一下,便于以后可能会用得到.我主要 ...
- Arduino入门笔记(6):温度传感器及感温杯实验
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 一.本次实验所需器材 1.Arduino板 :https://item.taob ...
- Arduino入门笔记(4):用蜂鸣器演奏音乐并配有LED闪烁
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 一.本次实验所需器材 1.Arduino板 https://item.taoba ...
- Arduino入门笔记(3):单LED闪烁
转载请注明:@小五义http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 在搭建好arduino开发环境(http://www.cnblogs.com/xi ...
- Arduino入门笔记(1):认识Arduino
转载请注明:@小五义http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 各种网站论坛里经常看到网友DIY各种遥控小车.机器人什么的,一直很好奇,很想学习一 ...
- arduino入门套件学习过程-安装配置
arduino作为非常火的开源软硬件,有其巨大的优势和前景. 我呢,近期正好需要用arduino做一些小东西,借此机会,分享我的arduino学习历程,欢迎各位朋友一起讨论,指点! 现在进入正题: 这 ...
- nodejs操作arduino入门(javascript操作底层硬件)
用Javascript来操作硬件早就不是一件稀奇的事情了. 所以作为一名电子专业出身的FE,我也打算尝试一下用js来驱动arduino: 要想操作这些底层硬件,肯定是需要一些工具的,我这里介绍的工具主 ...
- Arduino入门笔记【1】
刚刚接触这个东西只知道这是类似于单片机的开发板,可以做一些单片机实现或者不能实现的东西,但是比单片机要简单得多. Arduino到底是什么? 维基百科上的描述是:Arduino是一块开发板的微控制器和 ...
随机推荐
- Java - 让1+1的结果变成3
原出处是国外某论坛某帖子中楼主提问:如何让1+1=3?于是出现了各种语言实现的各种机制的答案,当然其中也包括直接用字符串输出"1+1=3"...最后被采纳的是用Java语言实现的答 ...
- Selector#wakeup()
看thrift源码发现selector.wakeup()方法,通常在selector.select()后线程会阻塞.使用wakeup()方法,线程会立即返回.源码分析应该是用的线程中断实现的.下面是个 ...
- POJ 2923(状态集合背包)
http://www.cnblogs.com/kuangbin/archive/2012/09/14/2685430.html #include <iostream> #include & ...
- unity3d之实现各种滑动效果
一. 点击滑动页面 新建了一个带mask的prefab,加上代码只需要将图片prefab.按钮prefab和所想添加的图片 拖进去会自动生成按钮,滑动速度可以随意调time,滑动效果用itween实现 ...
- Editplus编辑器在php文件中变色显示设置
咋editplus中我们编辑时有时会遇到不变色的问题,那么怎么设置呢,从语法配置就好,如下:
- JS常见的几种数组去重方法
总结一下JS中用到的数组去重的方法 方法一: 该方法利用对象的属性值不能相同: function arrDelLikeElement (array) { const result = []; con ...
- jquery each() 方法跳出循环
1.jquery each() 方法 return false: 的时候 相当于 break; 跳出整个循环: 2.jquery each() 方法 return true: 的时候 相当于 ...
- C#打印代码运行时间
使用以下方法可以准确的记录代码运行的耗时. System.Diagnostics.Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); / ...
- Codeforces Round #413 A. Carrot Cakes
A. Carrot Cakes time limit per test 1 second memory limit per test 256 megabytes In some game ...
- 常见的浏览器兼容性问题与解决方案——CSS篇
1.不同的浏览器的标签默认的外补丁和内补丁不同 问题症状:随便写几个标签,不加样式控制的情况下,各自的margin和padding差异较大. 碰到频率:100% 解决方案:初始化CSS的默认样式,*{ ...