Hallo zusammen, hab das Problem einen Progressbar abspielen zu lassen , ich finde aber den Fehler nicht.Die Animation erscheint ganz kurz statt 5 sek.
#include <SPI.h>
#include <MFRC522.h>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
MFRC522 mfrc522(21, 22);
MFRC522::MIFARE_Key key;
byte nuidPICC[4];
bool isProgress = false;
unsigned long previousMillis = 0;
const long interval = 5000;
int reading = 0;
#define GREEN2RED 4
void setup() {
Serial.begin(115200);
SPI.begin();
mfrc522.PCD_Init();
tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
}
void loop() {
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ) {
tft.fillScreen(TFT_BLACK);
if(printHex(mfrc522.uid.uidByte, mfrc522.uid.size)=="a2afd183"){
Serial.println(printHex(mfrc522.uid.uidByte, mfrc522.uid.size));
isProgress = true; // animation starten
}
else
{
}
mfrc522.PICC_HaltA();
delay(1000);
}
}
//---- animation progressbar --------------
if(isProgress){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
linearMeter(reading, 10, 180, 10, 35, 3, 22, GREEN2RED);
reading++;
if (reading > 22) { reading = 0;}
previousMillis = currentMillis;
}
isProgress=false;
}
//---------------------------------------------
//--------- function progressbar ---------
uint16_t rainbowColor(uint8_t spectrum)
{
spectrum = spectrum%192;
uint8_t red = 0;
uint8_t green = 0;
uint8_t blue = 0;
uint8_t sector = spectrum >> 5;
uint8_t amplit = spectrum & 0x1F;
switch (sector)
{
case 0:
red = 0x1F;
green = amplit;
blue = 0;
break;
case 1:
red = 0x1F - amplit;
green = 0x1F;
blue = 0;
break;
case 2:
red = 0;
green = 0x1F;
blue = amplit;
break;
case 3:
red = 0;
green = 0x1F - amplit;
blue = 0x1F;
break;
case 4:
red = amplit;
green = 0;
blue = 0x1F;
break;
case 5:
red = 0x1F;
green = 0;
blue = 0x1F - amplit;
break;
}
return red << 11 | green << 6 | blue;
}
// val = reading to show (range is 0 to n)
// x, y = position of top left corner
// w, h = width and height of a single bar
// g = pixel gap to next bar (can be 0)
// n = number of segments
// s = colour scheme
void linearMeter(int val, int x, int y, int w, int h, int g, int n, byte s)
{
int colour = TFT_BLUE;
for (int b = 1; b <= n; b++) {
if (val > 0 && b <= val) {
switch (s) {
case 0: colour = TFT_RED; break;
case 1: colour = TFT_GREEN; break;
case 2: colour = TFT_BLUE; break;
case 3: colour = rainbowColor(map(b, 0, n, 127, 0)); break;
case 4: colour = rainbowColor(map(b, 0, n, 63, 0)); break;
case 5: colour = rainbowColor(map(b, 0, n, 0, 63)); break;
case 6: colour = rainbowColor(map(b, 0, n, 0, 159)); break;
}
tft.fillRect(x + b*(w+g), y, w, h, colour);
}
else
{
tft.fillRect(x + b*(w+g), y, w, h, TFT_DARKGREY);
}
}
}
//----------------------------------------------------------
String printHex(byte *buffer, byte bufferSize) {
String id = "";
for (byte i = 0; i < bufferSize; i++) {
id += buffer[i] < 0x10 ? "0" : "";
id += String(buffer[i], HEX);
}
return id;
}