Space Battleship Yamato 1/500 scale
Additional Electronics
- Arduino Nano
- IR remote module
- DFplayer sound module
- 3W sound amplifier and speaker
- combination of LEDs
- small SMDs
- regular LEDs
- WS2812B addressable LEDs
//Arduino Code
#include "IRremote.h"
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <FastLED.h>
// How many leds in your strip?
#define NUM_LEDS 4
#define DATA_PIN 5
CRGB leds[NUM_LEDS];
SoftwareSerial mySoftwareSerial(13, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
int sub_engine = 4;
int main_engine1 = 2;
int main_engine2 = 3;
int main_engine3 = A5;
int hado_collect0 = A0;
int hado_collect1 = A1;
int hado_collect2 = A2;
int hado_collect3 = A3;
int receiver = A4;
int hado_red = 9;
int hado_blue = 6;
int interior_lights = 7;
int search_lights = 8;
//2 A7
//pWM 5 10
// 12
int eswitch=0;
/*-----( Declare objects )-----*/
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
/*-----( Declare Variables )-----*/
void setup() {
// put your setup code here, to run once:
mySoftwareSerial.begin(9600);
Serial.begin(115200);
FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
Serial.println();
Serial.println(F("DFRobot DFPlayer Mini Demo"));
Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
Serial.println(F("Unable to begin:"));
Serial.println(F("1.Please recheck the connection!"));
Serial.println(F("2.Please insert the SD card!"));
while(true){
delay(0); // Code to compatible with ESP8266 watch dog.
}
}
Serial.println(F("DFPlayer Mini online."));
myDFPlayer.volume(30); //Set volume value. From 0 to 30
//Play the first mp3
irrecv.enableIRIn(); // Start the receiver
pinMode(hado_collect0, OUTPUT);
pinMode(hado_collect1, OUTPUT);
pinMode(hado_collect2, OUTPUT);
pinMode(hado_collect3, OUTPUT);
pinMode(hado_red, OUTPUT);
pinMode(hado_blue, OUTPUT);
pinMode(main_engine1, OUTPUT);
pinMode(main_engine2, OUTPUT);
pinMode(main_engine3, OUTPUT);
pinMode(sub_engine, OUTPUT);
pinMode(interior_lights, OUTPUT);
pinMode(search_lights, OUTPUT);
digitalWrite(interior_lights, LOW);
digitalWrite(search_lights, LOW);
for (int inleds = 0 ; inleds<NUM_LEDS;inleds++){
leds[inleds].red = 0;
leds[inleds].green = 0;
leds[inleds].blue = 0;
FastLED.show();
}
}
void loop() {
if (irrecv.decode(&results)) // have we received an IR signal?
{
// Serial.println(results.value, HEX); UN Comment to see raw values
switch(results.value)
{
case 0xFFA25D: Serial.println("POWER");break;
case 0xFF629D: Serial.println("MODE"); break;
case 0xFFE21D: Serial.println("MUTE"); break;
case 0xFF22DD: Serial.println("PLAY/PAUSE"); break;
case 0xFF02FD: Serial.println("BACK"); break;
case 0xFFC23D: Serial.println("NEXT"); break;
case 0xFFE01F: Serial.println("EQ"); break;
case 0xFFA857: Serial.println("MINUS"); break;
case 0xFF906F: Serial.println("PLUS"); break;
case 0xFF6897: Serial.println(" 0"); break;
case 0xFF9867: Serial.println(" SHUFFLE"); break;
case 0xFFB04F: Serial.println(" USD"); break;
case 0xFF30CF: Serial.println(" 1");engine_spinup(1); break;
case 551520375: Serial.println(" 1");engine_spinup(1); break;
case 0xFF18E7: Serial.println(" 2"); engine_spindown(); break;
case 551504055: Serial.println(" 2"); engine_spindown(); break;
case 0xFF7A85: Serial.println(" 3"); go_hado(1,0); break;
case 551536695: Serial.println(" 3"); go_hado(1,0); break;
case 551495895: Serial.println(" 3"); shock_cannon_seq(1); break;
case 0xFF10EF: Serial.println(" 4"); shock_cannon_seq(1); break;
case 0xFF38C7: Serial.println(" 5"); theme_seq(0); break;
case 551528535: Serial.println(" 5"); theme_seq(0); break;
case 0xFF5AA5: Serial.println(" 6"); break;
case 0xFF42BD: Serial.println(" 7"); break;
case 0xFF4AB5: Serial.println(" 8"); break;
case 0xFF52AD: Serial.println(" 9"); theme_seq(1); break;
default:
Serial.println(results.value);
}// End Case
delay(500); // Do not get immediate repeat
irrecv.resume(); // receive the next value
}
}
void theme_seq(int eng_version){
engine_spindown();
delay(1000);
if(eng_version){
myDFPlayer.play(6);
}else{
myDFPlayer.play(1);
}
engine_spinup(0);
delay(14000);
shoot_cannon(2);
shoot_cannon(1);
shoot_cannon(0);
for(int rand_fire=0;rand_fire<=11;rand_fire++){
shoot_cannon(random(0,3));
}
delay(100);
go_hado(0,1);
}
void shock_cannon_seq(int music_play){
Serial.println("Shock Cannon Start");
int shock_cannon_dur=5500;
if(music_play){
delay(1000);
myDFPlayer.play(4);
}
delay(16900);
shoot_cannon(2);
shoot_cannon(1);
shoot_cannon(0);
for(int rand_fire=0;rand_fire<=14;rand_fire++){
shoot_cannon(random(0,3));
}
delay(100);
// Serial.println(shock_count);
}
void shoot_cannon(int can_num){
for(int red_start=0;red_start<=25;red_start++){
leds[can_num].red = red_start;
leds[can_num].green = 0;
leds[can_num].blue = 0;
FastLED.show();
delay(5);
}
leds[can_num].red = 0;
leds[can_num].green = 0;
leds[can_num].blue = 256;
FastLED.show();
delay(5);
for(int blastc=256;blastc>=0;blastc--){
//if(blastc-20>0)leds[can_num].red = blastc-20;
//if(blastc-10>0)leds[can_num].green = blastc-10;
leds[can_num].red = 0;
leds[can_num].green = 0;
leds[can_num].blue = blastc;
FastLED.show();
}
}
void go_hado(int music_play,int mini_hado){
int hado_spin=0;
int hado_gap=150;
int hado_decay=7;
int hado_duration=0;
int mod_red_cnt=0;
int brightness_counter=0;
int red_brightness=0;
int blue_brightness=0;
int green_brightness=0;
int blue_step=0;
int bluend;
int hado_stop=7125;
int flickerpnt=3600;
int powerup=6400;
int powerupcnt=0;
int startcount=0;
int fadeout=11400;
int blue_rotate=0;
if(mini_hado){
startcount=4590;
}
Serial.println("Hado Start");
if(music_play){
delay(1000);
myDFPlayer.play(3);
}
for(int hado_count=startcount;hado_count<=12800;hado_count++){
delay(6);
hado_spin++;
mod_red_cnt++;
if(hado_count<hado_stop){
if(hado_count>2800){
green_brightness++;
red_brightness++;
//blue_brightness++;
if(green_brightness>45)green_brightness=45;
if(blue_brightness>40)blue_brightness=40;
if(red_brightness>80)red_brightness=80;
}else if(hado_count>4500){
brightness_counter++;
green_brightness++;
if(brightness_counter>130)brightness_counter=130;
if(green_brightness>150)green_brightness=150;
}else{
if(mod_red_cnt>68){
brightness_counter++;
mod_red_cnt=0;
if(brightness_counter>50)brightness_counter=50;
}else{
red_brightness=brightness_counter;
}
}
if(hado_count>4500){
if(blue_rotate>200){
blue_brightness++;
blue_rotate=0;
}
blue_rotate++;
}
if(blue_brightness>10)blue_brightness=10;
analogWrite(hado_blue, blue_brightness);
leds[3].red = red_brightness;
leds[3].green = green_brightness;
leds[3].blue = 0;
if(hado_count>flickerpnt&&hado_count<flickerpnt+310){
if( random(1,6)==1){
leds[3].red = 255;
leds[3].green = 255;
leds[3].blue = 255;
}
}
if(hado_count>powerup&&hado_count<hado_stop){
if(powerupcnt<255)powerupcnt++;
if(powerupcnt>red_brightness){
red_brightness=powerupcnt;
}
leds[3].red = red_brightness;
leds[3].green = powerupcnt;
leds[3].blue = powerupcnt;
digitalWrite(interior_lights, LOW);
}
FastLED.show();
}else{
if(hado_count>hado_stop&&hado_count<(hado_stop+187)){
brightness_counter=255;
digitalWrite(search_lights, LOW);
leds[3].red = 0;
leds[3].green = 0;
leds[3].blue = 0;
FastLED.show();
analogWrite(hado_blue, 0);
}else{
if(brightness_counter>100)brightness_counter=brightness_counter-1;
if(hado_count>fadeout){
if(mod_red_cnt>10){
brightness_counter--;
mod_red_cnt=0;
if(brightness_counter<0)brightness_counter=0;
}
if( random(1,3)==1){
leds[3].red = 0;
leds[3].green = 0;
leds[3].blue = 0;
FastLED.show();
analogWrite(hado_blue, 0);
}else{
blue_brightness=brightness_counter;
if(brightness_counter<120){
blue_brightness=blue_brightness-50;
}
if(blue_brightness<0)blue_brightness=0;
leds[3].red = brightness_counter;
leds[3].green = 0;
leds[3].blue = 0;
FastLED.show();
analogWrite(hado_blue, blue_brightness);
}
}else{
if( random(1,6)==1){
//analogWrite(hado_red, 170);
analogWrite(hado_blue, 255);
}else{
// analogWrite(hado_red, brightness_counter);
analogWrite(hado_blue, brightness_counter);
}
}
}
}
Serial.println(hado_count);
if(hado_count<hado_stop){
if(hado_spin<=hado_gap){
digitalWrite(hado_collect0,HIGH);
}else{
digitalWrite(hado_collect0,LOW);
}
if(hado_spin>=hado_gap&&hado_spin<=hado_gap*2){
digitalWrite(hado_collect1,HIGH);
}else{
digitalWrite(hado_collect1,LOW);
}
if(hado_spin>=hado_gap*2&&hado_spin<=hado_gap*3){
digitalWrite(hado_collect2,HIGH);
}else{
digitalWrite(hado_collect2,LOW);
}
if(hado_spin>=hado_gap*3){
digitalWrite(hado_collect3,HIGH);
}else{
digitalWrite(hado_collect3,LOW);
}
if(hado_spin>=hado_gap*4){
hado_spin=0;
hado_gap=hado_gap-hado_decay;
if(hado_count>4200){
hado_decay=8;
}
if(hado_gap<3){
hado_gap=3;
}
}
}else{
digitalWrite(hado_collect0,LOW);
digitalWrite(hado_collect1,LOW);
digitalWrite(hado_collect2,LOW);
digitalWrite(hado_collect3,LOW);
}
}
digitalWrite(interior_lights, HIGH);
digitalWrite(search_lights, HIGH);
}
void engine_spinup(int music_play){
digitalWrite(interior_lights, HIGH);
int spin_step=0;
int spin_delay=250;
int spin_decay=1;
int spin_dur = 1955;
Serial.println("Engine Start");
if(eswitch==0){
if(music_play){
delay(1000);
myDFPlayer.play(2);
}else{
spin_dur=1200;
}
sputter(0);
for(int spinup_cnt=0;spinup_cnt<=spin_dur;spinup_cnt++){
spin_step++;
// Serial.println(spinup_cnt);
if(spin_step==1){
digitalWrite(main_engine1, HIGH);
digitalWrite(main_engine2, LOW);
digitalWrite(main_engine3, LOW);
}
if(spin_step==2){
digitalWrite(main_engine2, HIGH);
digitalWrite(main_engine3, LOW);
digitalWrite(main_engine1, LOW);
}
if(spin_step==3){
digitalWrite(main_engine3, HIGH);
digitalWrite(main_engine2, LOW);
digitalWrite(main_engine1, LOW);
}
if(spinup_cnt==20){
spin_delay=100;
}
delay(spin_delay);
if(spin_step>=3){
spin_step=0;
spin_delay=spin_delay-spin_decay;
if(spinup_cnt>15){
spin_delay=spin_delay-(spin_decay*2);
}
if(spin_delay<15)spin_delay=15;
}
}
digitalWrite(search_lights, HIGH);
eswitch=1;
Serial.println("Engine Blast");
sputter(1);
sputter(1);
sputter(1);
engine_status(1);
}
}
void engine_status(int eswitch){
//Serial.println(eswitch);
if(eswitch==1){
digitalWrite(main_engine3, HIGH);
digitalWrite(main_engine2, HIGH);
digitalWrite(main_engine1, HIGH);
digitalWrite(sub_engine, HIGH);
}else{
digitalWrite(main_engine3, LOW);
digitalWrite(main_engine2, LOW);
digitalWrite(main_engine1, LOW);
digitalWrite(sub_engine, LOW);
}
delay(1000);
}
void engine_spindown(){
int spin_step=0;
int spin_delay=0;
int spin_decay=2;
if(eswitch==1){
delay(1000);
myDFPlayer.play(5);
sputter(1);
digitalWrite(sub_engine, LOW);
for(int spinup_cnt=0;spinup_cnt<=150;spinup_cnt++){
spin_step++;
//Serial.println(spinup_cnt);
if(spin_step==1){
digitalWrite(main_engine1, HIGH);
digitalWrite(main_engine2, LOW);
digitalWrite(main_engine3, LOW);
}
if(spin_step==2){
digitalWrite(main_engine2, HIGH);
digitalWrite(main_engine3, LOW);
digitalWrite(main_engine1, LOW);
}
if(spin_step==3){
digitalWrite(main_engine3, HIGH);
digitalWrite(main_engine2, LOW);
digitalWrite(main_engine1, LOW);
}
delay(spin_delay);
if(spin_step>=3){
spin_step=0;
spin_delay=spin_delay+spin_decay;
if(spinup_cnt>100){
spin_delay=spin_delay+spin_decay*2;
}
if(spinup_cnt>130){
spin_delay=spin_delay+spin_decay*4;
}
}
if(spinup_cnt>130){
spin_delay=spin_delay+spin_decay*2;
}
}
eswitch=0;
engine_status(0);
digitalWrite(interior_lights, LOW);
digitalWrite(search_lights, LOW);
}
}
void sputter(int sputter_sub){
for(int sput=0;sput<=70;sput++){
if( random(1,3)==1){
digitalWrite(main_engine3, LOW);
digitalWrite(main_engine2, LOW);
digitalWrite(main_engine1, LOW);
if(sputter_sub==1)digitalWrite(sub_engine, LOW);
delay(random(30,35));
}else{
digitalWrite(main_engine3, HIGH);
digitalWrite(main_engine2, HIGH);
digitalWrite(main_engine1, HIGH);
if(sputter_sub==1)digitalWrite(sub_engine, HIGH);
}
}
}
void printDetail(uint8_t type, int value){
switch (type) {
case TimeOut:
Serial.println(F("Time Out!"));
break;
case WrongStack:
Serial.println(F("Stack Wrong!"));
break;
case DFPlayerCardInserted:
Serial.println(F("Card Inserted!"));
break;
case DFPlayerCardRemoved:
Serial.println(F("Card Removed!"));
break;
case DFPlayerCardOnline:
Serial.println(F("Card Online!"));
break;
case DFPlayerUSBInserted:
Serial.println("USB Inserted!");
break;
case DFPlayerUSBRemoved:
Serial.println("USB Removed!");
break;
case DFPlayerPlayFinished:
Serial.print(F("Number:"));
Serial.print(value);
Serial.println(F(" Play Finished!"));
break;
case DFPlayerError:
Serial.print(F("DFPlayerError:"));
switch (value) {
case Busy:
Serial.println(F("Card not found"));
break;
case Sleeping:
Serial.println(F("Sleeping"));
break;
case SerialWrongStack:
Serial.println(F("Get Wrong Stack"));
break;
case CheckSumNotMatch:
Serial.println(F("Check Sum Not Match"));
break;
case FileIndexOut:
Serial.println(F("File Index Out of Bound"));
break;
case FileMismatch:
Serial.println(F("Cannot Find File"));
break;
case Advertise:
Serial.println(F("In Advertise"));
break;
default:
break;
}
break;
default:
break;
}
}
Comments