My_arduino(2)小项目代码1
主要依靠这里Michael McRoberts 着的《Arduino从基础到实践》进行总结,在本博文中,给出代码,希望能帮助大家减少学习过程中一些琐碎代码的写作。以后会更新后续代码。
项目3 -----交通信号灯
//项目3 -----交通信号灯 //5秒红灯,然后闪3秒灭;2秒黄灯,灭;3秒绿灯,闪3秒灭 void setup() {
// put your setup code here, to run once: pinMode(8,OUTPUT); pinMode(12,OUTPUT); pinMode(13,OUTPUT); } void flash(int pin){
digitalWrite(pin,LOW); delay(500); digitalWrite(pin,HIGH); delay(500); digitalWrite(pin,LOW); delay(500); digitalWrite(pin,HIGH); delay(500); digitalWrite(pin,HIGH); delay(500); digitalWrite(pin,LOW); delay(500); digitalWrite(pin,HIGH); delay(500);
return ;
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(13,HIGH);
delay(5000);//亮5秒
flash(13);
digitalWrite(13,LOW);
digitalWrite(12,HIGH);
delay(3000);
digitalWrite(12,LOW);
digitalWrite(8,HIGH);
delay(3000);
flash(8);
digitalWrite(8,LOW);
delay(10000);
}
project4(待补充)
project5 跑马灯
//project5 跑马灯
byte pin[10] = {
4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
byte count = 0;
int ledDelay = 100;//这个变量控制住了跑马灯的速度
int direction = 1;
unsigned long changeTime;
void setup() {
// put your setup code here, to run once:
for (int i = 0; i < 10; i++)
pinMode(pin[i], OUTPUT);
changeTime = millis();
}
void loop() {
// put your main code here, to run repeatedly:
if (millis() - changeTime > ledDelay) {
changeLed();
changeTime = millis();
}
}
void changeLed() {
for (int i = 0; i < 10; i++) {
digitalWrite(pin[i], LOW);
}
digitalWrite(pin[count], HIGH);
count += direction;
if (count == 9) {
direction = -1;
}
if (count == 0) {
direction = 1;
}
}
project6 电位器控制跑马灯
//project6 电位器控制跑马灯
byte pin[10] = {
4,5,6,7,8,9,10,11,12,13};
byte count = 0;
int ledDelay;//这个变量控制住了跑马灯的速度
int direction = 1;
unsigned long changeTime;
int potPin = 2;//电位计的输入引脚
void setup() {
// put your setup code here, to run once:
for (int i=0;i<10;i++)
pinMode(pin[i],OUTPUT);
changeTime = millis();
}
void loop() {
ledDelay = analogRead(potPin);//从电位计中读值。
// put your main code here, to run repeatedly:
if (millis() - changeTime > ledDelay){
changeLed();
changeTime = millis();
}
}
void changeLed(){
for(int i = 0;i<10;i++){
digitalWrite(pin[i],LOW);}
digitalWrite(pin[count],HIGH);
count += direction;
if(count == 9){
direction = -1;}
if(count == 0){
direction = 1;}
}
project7 控制小灯的亮度
//project7 控制小灯的亮度。
int pin = 11;
float sinVal;
int ledVal;
void setup() {
// put your setup code here, to run once:
pinMode(pin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
for (int x = 0; x < 180; x++) {
sinVal = (sin(x * 3.1412 / 180)); //从0到1嘛
ledVal = int(sinVal * 255); //从0 - 255 嘛
analogWrite(pin, ledVal);
delay(25);
}
}
project8 彩灯
//project8 彩灯
float RGB1[3];
float RGB2[3];
float INC[3];
int red,green,blue;
int redpin =11;
int greenpin = 10;
int bluepin = 9;
void setup() {
// put your setup code here, to run once:
randomSeed(analogRead(0));
RGB1[0] = 0;
RGB1[1] = 0;
RGB1[2] = 0;
RGB2[0] = random(256);
RGB2[1] = random(256);
RGB2[2] = random(256);
}
void loop() {
// put your main code here, to run repeatedly:
randomSeed(analogRead(0));
for (int i =0;i<3;i++){
INC[i] = (RGB1[i] - RGB2[i])/256;}
for (int i=0;i<256;i++){
red = int(RGB1[0]);
green = int(RGB1[1]);
blue = int(RGB1[2]);
analogWrite(redpin,red);
analogWrite(greenpin,green);
analogWrite(bluepin,blue);
delay(100);
RGB1[0] -= INC[0];
RGB1[1] -= INC[1];
RGB1[2] -= INC[2];
}
for (int i=0;i<3;i++){
RGB2[i] = random(556)-300;
RGB2[i] = constrain(RGB2[i],0,255);
delay(1000);
}
}
project9 LED火焰效果
//project9 LED火焰效果
int pin1 = 9;
int pin2 = 10;
int pin3 = 11;
void setup() {
// put your setup code here, to run once:
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
pinMode(pin3,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(pin1,random(120)+135);
analogWrite(pin2,random(120)+135);
analogWrite(pin3,random(120)+135);
delay(random(100));
}
project10 串口控制灯
//project10 串口控制灯
char b[18];
int r,g,b;
int rpin = 11;
int gpin = 10;
int bpin = 9;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.fulsh();
pinMode(rpin,OUTPUT);
pinMode(gpin,OUTPUT);
pinMode(bpin,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available() > 0){
int index = 0;
delay(100);//等待缓冲区填满
int num = Serial.available();
if (num >15){
num = 15;
}
while (num--){
b[index++] = Serial.read();
}
splitString(butter);
}
}
void splitString(char* data){
Serial.print("Data enterd:");
Serial.println(data);
char* p;
p = strtok (NULL,",");
while ( p != NULL){
setLED(p);
p = strtok (NULL,",");
}
//清除窗口缓存区中的脚本
for (int i = 0;i<16;i++){
b[i] = '\0';
}
Serial.flush();
}
void setLED(char* data){
if ((data[0] == 'r') || (data[0] =='R')){
int Ans = strtol(data+1,NULL,10);
Ans = (constrain(Ans,0,255);
analogWrite(rpin,Ans);
Serial.print("Red is set to :");
Serial.println(Ans):
}
if((data[0] == 'g') || (data[0] =='G')){
int Ans = strtol(data+1,NULL,10);
Ans = constrain(Ans,0,255):
analogWrite(gpin,Ans);
Serial.print("Green is set to :");
Serial.println(Ans):
}
if((data[0] == 'b') || (data[0] =='B')){
int Ans = strtol(data+1,NULL,10);
Ans = constrain(Ans,0,255):
analogWrite(bpin,Ans);
Serial.print("Blue is set to :");
Serial.println(Ans):
}
}
project11 压电声音报警器
//project11 压电声音报警器
float sinVal;
int toneVal;
void setup() {
// put your setup code here, to run once:
pinMode(8, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < 180; i++) {
//当使用sin函数时转化角度到弧度
sinVal = (sin(i * (3.1412 / 180))); //0-1的数字
toneVal = 2000 + (int(sinVal * 1000));
tone(8, toneVal);
delay(2);//因为for循环时间很短,所以延时让再次改变声音之前持续至少2ms
}
}
project12 压电扬声器的音乐演奏
//project12 压电扬声器的音乐演奏 //这个时定义的音符,相当于都如艾米发馊拉稀 #define NOTE_C3 131 #define NOTE_CS3 139 #define NOTE_D3 147 #define NOTE_DS3 156 #define NOTE_E3 165 #define NOTE_F3 175 #define NOTE_FS3 185 #define NOTE_G3 196 #define NOTE_GS3 208 #define NOTE_A3 220 #define NOTE_AS3 233 #define NOTE_B3 247 #define NOTE_C4 262 #define NOTE_CS4 277 #define NOTE_D4 294 #define NOTE_DS4 311 #define NOTE_E4 330 #define NOTE_F4 349 #define NOTE_FS4 370 #define NOTE_G4 392 #define NOTE_GS4 415 #define NOTE_A4 440 #define NOTE_AS4 466 #define NOTE_B4 494 //这个是定义的音调 #define WHOLE 1 #define HALF 0.5 #define QUARTER 0.25 #define EIGHTH 0.125 #define SIXTEENTH 0.0625 //这个就是谱了,,,牛逼。。。 int tune[] = { NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_B3, NOTE_G3, NOTE_A3, NOTE_C4, NOTE_C4, NOTE_G3, NOTE_G3, NOTE_F3, NOTE_F3, NOTE_G3, NOTE_F3, NOTE_E3, NOTE_G3, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_C4, NOTE_A3, NOTE_B3, NOTE_C4, NOTE_D4 }; //几拍 float duration[] = { EIGHTH