@agmue Danke für die Hilfe.
Aktuell klappt es nur nicht, wie ich es möchte und ich weiß nicht wo mein Fehler ist. Ich habe es so umgeschrieben wie du @agmue es vorgeschlagen hast. Aber optisch ist es so momentan, dass wohl currentMillis schon beim einschalten hochgezählt wird. Aber die strikten Zeit-Intervalle interhalb meiner Funktion werden nicht eingehalten.
Hier nochmals meine umgeschriebene Version:
#include <Servo.h>
#include <FastLED.h>
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_GC9A01A.h>
#include <Fonts/FreeMonoBoldOblique12pt7b.h>
#include <Fonts/FreeSerif9pt7b.h>
#include <Fonts/Org_01.h>
#define NUM_LEDS 16
#define LED_PIN 6
#define TFT_DC 9
#define TFT_CS 10
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define COLOR_ORDER GRB
Adafruit_GC9A01A tft (TFT_CS, TFT_DC);
CRGB leds[NUM_LEDS];
uint8_t paletteIndex = 0;
int servopin = 7;
int tasterpin = 2;
int servotakt = 1;
int servopos = 90;
int Stufe;
int Sequence;
int Lied;
DEFINE_GRADIENT_PALETTE( fairygarden_gp ) {
0, 55, 19,103,
51, 95, 32,133,
101, 167, 44,162,
153, 125,182,237,
204, 84,127,207,
255, 19, 40,114};
CRGBPalette16 myPal = fairygarden_gp;
Servo Servo;
SoftwareSerial softwareSerial(3, 4);
const int wirePin = 4;
const int endPin = 5;
DFRobotDFPlayerMini player;
unsigned long currentMillis;
unsigned long servoPreviousMillis = 0;
unsigned long tasterMillis = 0;
unsigned long millisLied = 0;
const unsigned long servoPeriod = 50;
const unsigned long servoPeriod2 = 75;
const unsigned long tasterPeriod = 100;
const unsigned long wartezeit = 5000;
const unsigned long Leuchten1 = 20000;
const unsigned long Leuchten2 = 25000;
const unsigned long millislied1warte = 16000;
const unsigned long millislied2warte = 32000;
const unsigned long millislied1 = 1000;
const unsigned long millislied2 = 1000;
const unsigned long interval = 50;
const unsigned long displayTime = 50;
void setup() {
Servo.attach(servopin);
Servo.write(servopos);
tft.begin();
tft.setRotation(1);
tft.fillScreen(GC9A01A_BLACK);
pinMode(tasterpin, INPUT_PULLUP);
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
softwareSerial.begin(9600);
Serial.begin(115200);
if (!player.begin(softwareSerial)) { //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."));
player.volume(3); //Lautstärke auf Maximum (15)
}
void loop() {
currentMillis = millis();
static unsigned long previousMillis = 0;
const unsigned long Ablaufzeit = 18000;
static bool Fertig = false;
static byte Instanz = 0;
bool Tasterstatus = digitalRead(tasterpin);
switch(Instanz) {
case 0:
if(Tasterstatus == LOW) {
previousMillis = currentMillis;
Instanz = 1;
tft.fillScreen(0x0000);
} else {
Tasterwarten();
}
break;
case 1:
if (!Fertig) Fertig = Display();
//Servomotor();
//LEDRing();
//Liedabspielen();
if (Fertig)
{
Fertig = false;
Instanz = 2;
}
break;
case 2:
if(currentMillis - previousMillis >= Ablaufzeit) {
Instanz = 0;
}
//FastLED.show();
}
}
bool Display() {
bool Back = false;
static unsigned long displayanzeige = 0;
const unsigned long intervalnext = 5000;
const unsigned long intervalnext2 = 15000;
const unsigned long interval2 = 50;
const unsigned long interval3 = 600;
static byte Anzeige = 0;
switch(Anzeige) {
case 0:
//Displayhintergrund();
tft.setFont(&FreeSerif9pt7b);
tft.setCursor(70, 110);
tft.setTextColor(random(0x0000, 0xF8FF));
tft.setTextSize(2);
tft.print("Happy");
tft.setCursor(5, 155);
tft.setTextColor(random(0x0000, 0xF8FF));
tft.setTextSize(2);
tft.print("Valentine's Day");
tft.setFont(&FreeMonoBoldOblique12pt7b);
tft.setCursor(63, 220);
tft.setTextColor(GC9A01A_YELLOW);
tft.setTextSize(2);
tft.print("Name");
if (currentMillis - displayanzeige >= intervalnext) {
tft.fillScreen(0x0000);
Anzeige = 1;
}
break;
case 1:
if (currentMillis - displayanzeige >= intervalnext2) {
displayanzeige = currentMillis;
tft.fillScreen(0x0000);
Back = true;
Anzeige = 0;
} else {
if (currentMillis - displayanzeige >= interval2) {
drawHeart(tft.width()/2, tft.height()/2 - 20, GC9A01A_MAGENTA);
if (currentMillis - displayanzeige >= interval3) {
tft.fillScreen(0x0000);
}
}
}
break;
}
return Back;
}
/*
void Servomotor() {
switch(Stufe) {
case 0:
servoPreviousMillis = currentMillis;
servopos += servotakt;
Servo.write(servopos);
if (servopos >= 180) {
Stufe = 1;
}
break;
case 1:
if (currentMillis - servoPreviousMillis >= wartezeit) {
servoPreviousMillis = currentMillis;
Stufe = 2;
}
break;
case 2:
if (currentMillis - servoPreviousMillis >= servoPeriod2) {
servoPreviousMillis = currentMillis;
servopos -= servotakt;
Servo.write(servopos);
if(servopos <= 90) {
Stufe = 0;
}
}
break;
}
}
void LEDRing() {
switch(Sequence){
case 0:
static unsigned long LEDMillis1 = 0;
FastLED.setBrightness(50);
fill_solid(leds, NUM_LEDS, CRGB(48, 173, 10));
if (currentMillis - LEDMillis1 >= Leuchten1) {
Sequence = 1;
LEDMillis1 = currentMillis;
}
break;
case 1:
static unsigned long LEDMillis2 = 0;
if(currentMillis - LEDMillis2 >= Leuchten2) {
Sequence = 0;
LEDMillis2 = currentMillis;
} else {
FastLED.setBrightness(40);
fill_palette(leds, NUM_LEDS, paletteIndex, 255 / NUM_LEDS, myPal, 255, LINEARBLEND);
EVERY_N_MILLISECONDS(2){
paletteIndex++;
}
}
break;
}
}
void Liedabspielen() {
switch(Lied) {
case 0:
if(currentMillis - millisLied >= millislied1) {
millisLied = currentMillis;
player.play(1);
Lied = 1;
}
break;
case 1:
if(currentMillis - millisLied >= millislied1warte) {
millisLied = currentMillis;
Lied = 2;
}
break;
case 2:
if(currentMillis - millisLied >= millislied2) {
millisLied = currentMillis;
player.play(2);
Lied = 3;
}
break;
case 3:
if(currentMillis - millisLied >= millislied2warte) {
millisLied = currentMillis;
Lied = 0;
}
break;
}
}
*/
void drawHeart(int x0, int y0, uint16_t color) {
int size = 50; // Size of the heart
// Calculate the vertices of the two triangles that form the bottom of the heart
int x1 = x0 - size + 5;
int y1 = y0 - 10;
int x2 = x0 + size - 5;
int y2 = y0 - 10;
int x3 = x0;
int y3 = y0 + size;
// Draw two triangles to form the bottom of the heart
tft.fillTriangle(x1, y1, x3, y3, x0, y0 - size/2, color);
tft.fillTriangle(x0, y0 - size/2, x3, y3, x2, y2, color);
// Draw two half-circles at the top of the heart
tft.fillCircle(x0 - size/2, y0 - size/2, size/2, color);
tft.fillCircle(x0 + size/2, y0 - size/2, size/2, color);
}
void Tasterwarten(){
static unsigned long anzeigezeit = 0;
const unsigned long anzeigePeriod = 1000;
if(currentMillis - anzeigezeit >= anzeigePeriod) {
anzeigezeit = currentMillis;
tft.fillScreen(0x0000);
tft.setCursor(50, 50);
tft.setFont(&FreeSerif9pt7b);
tft.setTextColor(GC9A01A_WHITE);
tft.setTextSize(2);
tft.print("Press me!");
tft.fillRect(107, 110, 26, 100, GC9A01A_WHITE);
tft.fillTriangle(90,185,150,185,120,245, GC9A01A_WHITE);
}
FastLED.setBrightness(50);
fill_solid(leds, NUM_LEDS, CRGB(48, 173, 10));
}
/*
void Displayhintergrund() {
static unsigned long displayanzeige1 = 0;
if (currentMillis - displayanzeige1 >= interval) {
displayanzeige1 = currentMillis;
int x = random(SCREEN_WIDTH);
int y = random(SCREEN_HEIGHT);
int x2 = random(SCREEN_WIDTH);
int y2 = random(SCREEN_HEIGHT);
int rectSize = random(3, 7);
int rectSize2 = random(3, 7);
tft.fillRect(x, y, rectSize, rectSize, GC9A01A_WHITE);
tft.fillRect(x2, y2, rectSize2, rectSize2, GC9A01A_WHITE);
static unsigned long displayEndTime = currentMillis + displayTime;
while (currentMillis < displayEndTime) {
currentMillis = displayEndTime;
}
tft.fillRect(x, y, rectSize, rectSize, GC9A01A_BLACK);
tft.fillRect(x2, y2, rectSize2, rectSize2, GC9A01A_BLACK);
}
}*/
Die anderen 3 Funktionen müssen erstmal nicht beachtet werden. Solang die eine Funktion nicht funktioniert, kann ich es mit den anderen auch knicken.
Werft mal einen Blick drüber und schreibt mir, wenn ihr den Fehler findet und was ich verbessern müsste (abgesehen von meinem Code-Chaos
).
Oder klappt vielleicht meine switch-case-Ausführung innerhalb meiner Funktion nicht und soll es lieber in einzelne Funktionen packen
?