首页/作品列表/欢迎回家
主要内容
// includes start
#include <Arduino.h>
#include <Microduino_Key.h>
#include <Microduino_ColorLED.h>
#include <U8glib.h>
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);
#include <Servo.h>
// includes end
// definition start
void strip_transition(ColorLED &strip_led, uint8_t num, long colorbegin, long colorend, int _duration);
void playUntil(int pin, int note, float duration);
uint8_t playSound(uint8_t pin, uint8_t songNum);
void playingRest();
boolean playingStop();
void servo_move(Servo &servo, uint8_t anglebegin, uint8_t angleend, int _duration);
// definition end

#define SONG_SUM  11
DigitalKey key_4(4);
ColorLED strip_A0 = ColorLED(16, A0);
uint16_t toneList[] = { 262, 294, 330, 349, 392, 440, 494, 523, 587, 659, 698, 784, 880, 988, 1046, 1175, 1318, 1397, 1568, 1760, 1967 };
uint8_t music_1[] = { 12, 10, 12, 10, 12, 10, 9, 10, 12, 12, 12, 10, 13, 12, 10, 12, 10, 9, 8, 9, 10, 12, 10, 9, 8, 9, 10, 0 };
uint8_t rhythm_1[] = { 4, 2, 4, 2, 2, 2, 2, 2, 8, 2, 4, 2, 4, 4, 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 2, 2, 8 };
uint8_t music_2[] = { 8, 9, 10, 8, 8, 9, 10, 8, 10, 11, 12, 10, 11, 12, 0 };
uint8_t rhythm_2[] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8 };
uint8_t music_3[] = { 5, 8, 8, 10, 13, 10, 12, 12, 13, 12, 10, 11, 10, 9, 6, 9, 9, 11, 14, 14, 13, 12, 11, 11, 10, 6, 7, 8, 9 , 0 };
uint8_t rhythm_3[] = { 2, 1, 2, 1, 2, 1, 4, 2, 1, 2, 1, 2, 1, 4, 2, 1, 2, 1, 2, 1, 2, 1, 4, 2, 1, 2, 4, 2, 8 };
uint8_t music_4[] = { 5, 5, 6, 5, 8, 7, 5, 5, 6, 5, 9, 8, 5, 5, 12, 10, 8, 7, 6, 11, 11, 10, 8, 9, 8, 0 };
uint8_t rhythm_4[] = { 2, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 8, 2, 2, 4, 4, 4, 4, 4, 2, 2, 4, 4, 4, 8 };
uint8_t music_5[] = { 12, 13, 12, 13, 12, 13, 12, 12, 15, 14, 13, 12, 13, 12, 12, 12, 10, 10, 12, 12, 10, 9, 11, 10, 9, 8, 9, 8, 0 };
uint8_t rhythm_5[] = { 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4 };
uint8_t music_6[] = { 8, 8, 10, 8, 8, 10, 22, 13, 13, 13, 12, 13, 12, 8, 10, 22, 15, 13, 13, 12, 13, 12, 8, 9, 22, 14, 14, 12, 10, 12, 0  };
uint8_t rhythm_6[] = { 4, 4, 8, 2, 4, 4, 4, 4, 4, 2, 2, 4, 2, 4, 4, 4, 2, 2, 2, 2, 8, 2, 4, 4, 4, 4, 2, 2, 4, 16 };
uint8_t music_7[] = { 6, 8, 9, 10, 12, 10, 8, 9, 6, 22, 8, 9, 10, 12, 12, 13, 9, 10, 22, 10, 12, 13, 12, 13, 15, 14, 13, 12, 13, 10, 8, 9, 10, 12, 8, 6, 8, 9, 10, 13, 12 , 0 };
uint8_t rhythm_7[] = { 2, 2, 2, 4, 2, 2, 2, 4, 8, 2, 2, 2, 2, 4, 2, 4, 4, 8, 4, 2, 2, 8, 4, 2, 2, 1, 1, 2, 2, 4, 2, 2, 4, 2, 4, 4, 2, 2, 2, 2, 18 };
uint8_t music_8[] = { 10, 8, 9, 6, 10, 9, 8, 9, 6, 10, 8, 9, 9, 12, 10, 7, 8, 8, 7, 6, 7, 8, 9, 5, 13, 12, 10, 10, 9, 8, 9, 10, 9, 10, 9, 12, 12, 12, 12, 12, 12, 0 };
uint8_t rhythm_8[] = { 4, 4, 4, 4, 2, 2, 2, 2, 8, 4, 4, 4, 4, 2, 2, 4, 4, 2, 2, 4, 2, 2, 4, 4, 2, 2, 4, 4, 2, 4, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 4 };
uint8_t music_9[] = { 10, 12, 15, 13, 12, 10, 12, 13, 15, 12, 15, 17, 16, 15, 16, 15, 13, 15, 12 , 0 };
uint8_t rhythm_9[] = { 2, 2, 2, 2, 8, 2, 2, 2, 2, 8, 4, 2, 4, 4, 2, 2, 2, 2, 8 };
uint8_t music_10[] = { 10, 10, 10, 8, 5, 5, 22, 10, 10, 10, 8, 10, 22, 12, 12, 10, 8, 5, 5, 5, 6, 7, 8, 10, 9, 0 };
uint8_t rhythm_10[] = { 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 4, 4, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 4 };
uint8_t music_11[] = {8, 9, 10, 8, 8, 9, 10, 8, 10, 11, 12, 10, 11, 12, 0 };
uint8_t rhythm_11[] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 4 };
const uint8_t * Pmusic[SONG_SUM] = { music_1, music_2, music_3, music_4, music_5, music_6, music_7, music_8, music_9, music_10, music_11 };
const uint8_t * Prhythm[SONG_SUM] = { rhythm_1, rhythm_2, rhythm_3, rhythm_4, rhythm_5, rhythm_6, rhythm_7, rhythm_8, rhythm_9, rhythm_10, rhythm_11 };
uint32_t playTimer = 0;
int8_t songNumC = -1;
uint8_t songIndex;
uint8_t playIndex = 0;
Servo servo_6;
void strip_transition(ColorLED &strip_led, uint8_t num, long colorbegin, long colorend, int _duration) {
	uint8_t ColorR[3], ColorG[3], ColorB[3];
	ColorR[0] = colorbegin >> 16;
	ColorG[0] = colorbegin >> 8 & 0xFF;
	ColorB[0] = colorbegin & 0xFF;
	ColorR[1] = colorend >> 16;
	ColorG[1] = colorend >> 8 & 0xFF;
	ColorB[1] = colorend & 0xFF;
	for (int i = 0; i < _duration; i++) {
		ColorR[2] = map(i, 0, _duration, ColorR[0], ColorR[1]);
		ColorG[2] = map(i, 0, _duration, ColorG[0], ColorG[1]);
		ColorB[2] = map(i, 0, _duration, ColorB[0], ColorB[1]);
		strip_led.setPixelColor(num, ColorR[2], ColorG[2] , ColorB[2]);
		strip_led.show();
		delay(1);
	}
	delay(15);
}
void playUntil(int pin, int note, float duration) {
	tone(pin, note);
	if (duration > 0) {
		delay(duration * 1000);
		noTone(pin);
	}
}
uint8_t playSound(uint8_t pin, uint8_t songNum) {
	songIndex = songNum;
	if (songNum > SONG_SUM - 1) {
		noTone(pin);
		return;
	}
	if (playIndex == 100 || songNumC != songNum) {
		playIndex = 0;
		songNumC = songNum;
	}
	if (millis() > playTimer && (*(Pmusic[songNum] + playIndex / 2))) {
		if (playIndex % 2 == 0) {
			if (*(Pmusic[songNum] + playIndex / 2) != 22) {
				tone(pin, toneList[*(Pmusic[songNum] + playIndex / 2) - 1]);
			} else {
				noTone(pin);
			}
			playTimer = millis() + *(Prhythm[songNum] + playIndex / 2) * 75;
		} else {
			noTone(pin);
			playTimer = millis() + 40;
		}
		playIndex++;
	}
}
void playingRest() {
	playIndex = 0;
}
boolean playingStop() {
	if (*(Pmusic[songIndex] + playIndex / 2)) {
		return false;
	} else {
		return true;
	}
}
void servo_move(Servo &servo, uint8_t anglebegin, uint8_t angleend, int _duration) {
	if (angleend > anglebegin) {
		for (uint8_t a = 0; a < angleend - anglebegin; a++) {
			servo.write(anglebegin + a);
			delay(_duration / (angleend - anglebegin));
		}
	} else if (angleend < anglebegin) {
		for (uint8_t a = 0; a < anglebegin - angleend; a++) {
			servo.write(anglebegin - a);
			delay(_duration / (anglebegin - angleend));
		}
	}
}

void setup() {
	// setup define start
	key_4.begin(INPUT_PULLUP);
	strip_A0.begin();
	servo_6.attach(6);
	// setup define end
}

void loop() {
	if((key_4.readVal(KEY_RELEASED))) {
		for (int i = 0; i < 5; i++) {
			strip_transition(strip_A0, 0, 0x000000, 0xd41313, 0.2 * 1000);
			delay(0.1 * 1000);
			strip_transition(strip_A0, 1, 0x000000, 0xff7614, 0.2 * 1000);
			delay(0.1 * 1000);
			strip_transition(strip_A0, 2, 0x000000, 0xffda13, 0.2 * 1000);
			delay(0.1 * 1000);
			strip_transition(strip_A0, 3, 0x000000, 0x14d4c7, 0.2 * 1000);
			delay(0.1 * 1000);
			strip_transition(strip_A0, 4, 0x000000, 0x0021ff, 0.2 * 1000);
			delay(0.1 * 1000);
			strip_transition(strip_A0, 5, 0x000000, 0xc50a8e, 0.2 * 1000);
			delay(0.1 * 1000);
		}
		for (int i = 0; i < 2; i++) {
			playUntil(2, 880, 0.3);
			playUntil(2, 880, 0.15);
			playUntil(2, 784, 0.15);
			playUntil(2, 659, 0.3);
			playUntil(2, 587, 0.3);
			playUntil(2, 523, 1.2);
			playUntil(2, 659, 0.3);
			playUntil(2, 659, 0.15);
			playUntil(2, 587, 0.15);
			playUntil(2, 523, 0.3);
			playUntil(2, 440, 0.3);
			playUntil(2, 392, 1.2);
			u8g.setFont(u8g_font_9x15);
			u8g.firstPage();
			do {
				u8g.setPrintPos(10, 10);
				u8g.print("welcome");
			} while (u8g.nextPage());
			while (!playingStop()) {
				playSound(2, 3-1);
			}
			playingRest();
			for (int i2 = 0; i2 < 6; i2++) {
				servo_move(servo_6, 30, 120, 1 * 1000);
				servo_move(servo_6, 120, 30, 1 * 1000);
			}
		}
	}
}




代码展示

1. 欢迎回家

编程语言: Scratch3

软件工具:mDesigner 3


附件下载
  • 资料展示
  • 欢迎回家.zip
0
大牛,别默默的看了,快登录帮我点评一下吧!

立即注册