Hallo zusammen.
Mit eurer Hilfe habe ich es bereits geschafft, im folgenden Lichteffekt-Code erfolgreich die Funktion einzufügen, dass gleichzeitig zu den Button-Funktionen auch ein Sound abgespielt wird.
Nun möchte ich den Code erweitern, so dass die "Torpedo"-Funktion 3 mal hintereinander ausgeführt wird. Also sozusagen 3 Torpedo-"Schüsse" nacheinander.
Habe es bereits mit einer for-Schleife versucht. Kompilierung funktioniert aber das Board macht nichts anderes als vorher. So als hätte die for-Schleife gar keine Auswirkung.
-> siehe Abschnitt "Torpedo Firing" im Sketch
PS: der Code wurde zwar von mir erweitert und leicht angepasst, ist aber nicht von mir
#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(20); //Set volume value (0~30).
myDFPlayer.EQ(DFPLAYER_EQ_NORMAL);
myDFPlayer.outputDevice(DFPLAYER_DEVICE_SD);
}
// ===================== End Setup ============================================================ //
// ===================== Main Loop ============================================================ //
void loop() {
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)){
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
for (int i = 0; i < 3; i++){
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 ========================================== //