Poste mal deinen aktuellen Code, Schaltplan und das iso der sd-Karte. Vielleicht packt mich ja mal der Ehrgeiz und ich baue es nach und schaue es mir genauer an.
Und wenigstens eine Beschreibung, für was das ganze ist und ggf. der Link, wo es herkommt.
Danke für deine Mühe. Hier der Code:
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
SoftwareSerial mySoftwareSerial(10, 11); // RX, TX
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);
// ===================== Variables =============================================================//
// ------- Declaring Pins ----------------------------------------------//
static byte disruptorPin = 9;
static byte enginePin = 6;
static byte pushButtonPin = 12;
static byte torpPin[] = { 2, 3 };
// ------- User Inputs ------------------------------------------------//
static unsigned long torpUpTime = 1680;
static unsigned long torpFlashTime = 200;
static unsigned long torpDownTime = 500;
static long double torpIdleBrightness = 20;
static long double torpMaxBrightness = 255;
static unsigned long engineFadePeriod = 3000;
static unsigned long disruptorFiringTime = 2400;
// ------- For the Button Pushing -------------------------------------//
unsigned long debounce = 50;
unsigned long holdTime = 1000;
// ------- Bookeeping ------------------------------------------------//
unsigned long currentMillis;
boolean buttonValue = false;
boolean buttonLast = true;
boolean ignoreUp = false;
unsigned long buttonUpTime;
unsigned long buttonDownTime;
unsigned int torpedoPushes = 0;
unsigned int disruptorPushes = 0;
unsigned long torpedoTime;
unsigned long disruptorTime;
unsigned long torpedoMillis;
unsigned long disruptorMillis;
unsigned long disruptorFlashTime;
boolean disruptorState = 0;
// ==================== End Variables ========================================================= //
// ===================== Setup ================================================================ //
void setup(){
pinMode(disruptorPin,OUTPUT);
pinMode(enginePin,OUTPUT);
pinMode(pushButtonPin,INPUT_PULLUP); // Enables internal pull-up resistor
pinMode(torpPin[0],OUTPUT);
pinMode(torpPin[1],OUTPUT);
mySoftwareSerial.begin(9600);
Serial.begin(115200);
if (!myDFPlayer.begin(mySoftwareSerial)) { //Use softwareSerial to communicate with mp3.
while(true);
}
myDFPlayer.setTimeOut(500); //Set serial communictaion time out 500ms
myDFPlayer.volume(5); //Set volume value (0~30).
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
}
// ===================== End Setup ============================================================ //
// ===================== Main Loop ============================================================ //
void loop() {
unsigned long currentMillis = millis(); // get current time
// ---------- Push Buttoon Code --------------------------------------------------------//
buttonValue = digitalRead(pushButtonPin);
// Record Time of Push
if (buttonValue == false && buttonLast == true && (millis() - buttonUpTime) > debounce){
buttonDownTime = millis();
}
// Record Time of Release
if (buttonValue == true && buttonLast == false && (millis() - buttonDownTime) > debounce){
buttonUpTime = millis();
if (ignoreUp == false){
disruptorTime = millis(); // If released early, fire disruptor!
disruptorPushes++;
myDFPlayer.playMp3Folder(2);
}
else{
ignoreUp = false;
}
}
// If push time is longer than threshold, fire torpedo!
if (buttonValue == false && (millis() - buttonDownTime) > long(holdTime)){
myDFPlayer.playMp3Folder(6);
delay (2300);
buttonDownTime = millis();
torpedoTime = millis();
torpedoPushes++;
ignoreUp = true;
myDFPlayer.playMp3Folder(1);
}
// Record the button state for comparison in the next cycle
buttonLast = buttonValue;
// -------------------------------------------------------------------------------------//
// ---------- Torpedo Firing ----------------------------------------------------------//
// Ramp Up Red LED, Flash White LED, then a Ramp Down of Red LED
// Ramp Up
if ((currentMillis-torpedoTime) < torpUpTime && torpedoPushes > 0){
analogWrite(torpPin[1],torpIdleBrightness+(torpMaxBrightness-torpIdleBrightness)/torpUpTime*(currentMillis-torpedoTime));
digitalWrite(torpPin[0], LOW);
}
// Flash
else if (((currentMillis-torpedoTime) > torpUpTime) && ((currentMillis-torpedoTime) < (torpUpTime+torpFlashTime)) && torpedoPushes > 0){
analogWrite(torpPin[1], 0);
digitalWrite(torpPin[0], HIGH);
}
// Ramp Down
else if (((currentMillis-torpedoTime) > (torpUpTime+torpFlashTime)) && ((currentMillis-torpedoTime) < (torpUpTime+torpFlashTime+torpDownTime)) && torpedoPushes > 0){
analogWrite(torpPin[1],torpMaxBrightness+(torpIdleBrightness-torpMaxBrightness)/torpDownTime*((currentMillis-torpedoTime)-(torpUpTime+torpFlashTime)));
digitalWrite(torpPin[0], LOW);
}
// Idle
else{
analogWrite(torpPin[1], torpIdleBrightness);
digitalWrite(torpPin[0], LOW);
}
// ---------- Engine Fading ----------------------------------------------------------- //
// Slow Up and Down Fade with Shorter Pusations
int fadeValue = int(127.0+127.0/2.0*(1+(0.75*sin(2*PI*currentMillis/engineFadePeriod)+0.25*cos(14*PI*currentMillis/engineFadePeriod))));
analogWrite(enginePin,fadeValue);
// ---------- Disruptor Firing -------------------------------------------------------- //
// Get Random On or Off Time Using getRandom() Function Below
disruptorFlashTime = fmod(getRandom(), 4000UL) + 60UL;
if (((currentMillis - disruptorTime) < disruptorFiringTime) && (disruptorPushes > 0)){
if(currentMillis - disruptorMillis > disruptorFlashTime) {
disruptorMillis = currentMillis;
digitalWrite(disruptorPin, disruptorState = !disruptorState);
}
}
else{
analogWrite(disruptorPin,5);
}
// ------------------------------------------------------------------------------------ //
}
// ===================== End Main Loop ======================================================== //
// ===================== Pseduo-Random Generator ============================================== //
// Found This Online. I Take No Credit for It.
unsigned long m_w = 1;
unsigned long m_z = 2;
unsigned long getRandom()
{
m_z = 36969L * (m_z & 65535L) + (m_z >> 16);
m_w = 18000L * (m_w & 65535L) + (m_w >> 16);
return (m_z << 16) + m_w; /* 32-bit result */
}
// ===================== End Pseduo-Random Generator ========================================== //
Du kannst x-beliebige kurze MP3´s nehmen. Ob der Sound zum Licht passt ist ja egal. Hab den Sound auch an die Dauer der Lichteffekte angepasst.
Einen Schaltplan gibts nicht. Ist aber auch nichts größeres. Wie vor den Variablen angegeben Pin 10 und 11 fürs Serial (an TX und RX vom DFPlayer). Der DFPLayer bekommt normal Spannung und einen kleinen <3W 8Ohm Lautsprecher.
disruptorPin = 9 ist PWM
enginePin = 6 ist PWM
torpPin[] = { 2, 3 } 3 ist PWM
Hier das Originalvideo:
https://www.youtube.com/watch?v=Lgc1hYGJDoIEs geht um Effekte für ein Modell aus Star Trek. enginePin 6 ist ein Flackern mit auf und runterglühen für Triebwerke. Der Disruptor ist das Aufblitzen einer Waffe, wie eine Schuss-Salve beim kurzen Betätigen des Tasters. Und beim Torpedo glimmt die LED in "Idle" nur leicht. Nach längeren gedrückt halten wird diese LED bis zum Maximum heller, gefolgt von einem weißen Blitz, gefolgt vom dunkler werden der LED (Duo-LED).
Das funktionert bisher auch. Habe die Zeiten etwas angepasst und auch den Tasterbefehl getauscht. Ebenfalls das zeitgleiche Abspielen von einem Sound für die Disruptor und einem anderen Sound für die Torpedos hatte ich nachträglich eingefügt und funktioniert.
Nun wollte ich noch einen Sound für das Betätigen des Tasters haben. Als akustische Bestätigung beim Drücken. Danach sollte dann der jeweilige Lichteffekt mit Sound abgespielt werden.