Code stoppt durch Button

Hallo zusammen,
leider bin ich sehr sehr neu auf dem Gebiet der Arduino Programmierung und habe folgendes Problem. Ich möchte einen Pulsmesser basteln, der folgende Dinge kann:

  1. Ausgabe der Pulsfrequenz in BPM auf einem Display inklusive der Pulsfrequenz
  2. einen Stopp-Button (Touch) der die Messung unterbricht und aus den vergangenen Messungen den Mittelwert, Maximum und Minimum berechnet.
    Soweit so gut, den 1. Punkt habe ich komplett progammiert, aber sobald ich den Stopp Button einfüge, stoppt die Messung unmittelbar nach Reset bzw. Upload.
#include <Adafruit_GFX.h> // Hardware spezifisch
#include <MCUFRIEND_kbv.h> // Display
#include <PulseSensorPlayground.h> // Pulssensor
#include <TouchScreen.h> // Touchscreen

#define MINPRESSURE 60
#define MAXPRESSURE 1000

MCUFRIEND_kbv tft;

// Display Kalibrierung
const int XP=8,XM=A2,YP=A3,YM=9; //320x480 ID=0x9488
const int TS_LEFT=82,TS_RT=836,TS_TOP=891,TS_BOT=92;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button off_btn;

int pixel_x, pixel_y; 
bool measuring = true;

bool Touch_getXY(void) {
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT); 
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) {
        pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); 
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
    }
    return pressed;
}

const int PulseWire = A7;
int Threshold = 750;
PulseSensorPlayground pulseSensor;

// Farben
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

// Graphen Variablen
int xPos = 0, xPosPrev = 0;
int graphHeightPrev = 0;
const int startXPos = -1; // Startposition für den Graphen (X-Achse)
const int graphTop = 240; // Y-Position des oberen Randes des Graphen
const int graphBottom = 480; // Y-Position des unteren Randes des Graphen (untere Hälfte des Displays)
const int lineThickness = 3;

// Herzfrequenz Variablen
int minBPM = 999;
int maxBPM = 0;
int sumBPM = 0;
int countBPM = 0;

void setup() {
    Serial.begin(115200);

    // Pulsfrequenz
    pulseSensor.begin();
    pulseSensor.analogInput(PulseWire);
    pulseSensor.setThreshold(Threshold);
    tft.reset();
    uint16_t id = tft.readID();
    tft.begin(id);
    tft.setRotation(2);
    tft.fillScreen(BLACK);
    tft.setTextColor(CYAN);
    tft.setTextSize(4);
    tft.setCursor(10, 5);
    tft.print("Pulsfrequenz");
    tft.setTextSize(4);
    tft.setTextColor(CYAN);
    tft.setCursor(200, 100);
    tft.println("/min");

    // Graph
    xPos = startXPos; // Initiale X-Position setzen
    xPosPrev = startXPos; // Vorherige X-Position setzen

    // Button
    off_btn.initButton(&tft, 160, 195, 100, 70, BLACK, RED, BLACK, "STOP", 2);
    off_btn.drawButton(false);

    Serial.println("Setup complete");
}

void loop() {
    if (measuring) {
        // BPM
        if (pulseSensor.sawStartOfBeat()) {
            int myBPM = pulseSensor.getBeatsPerMinute();
            if (myBPM < minBPM) minBPM = myBPM;
            if (myBPM > maxBPM) maxBPM = myBPM;
            sumBPM += myBPM;
            countBPM++;
            tft.fillRect(90, 73, 100, 76, BLACK);
            tft.setTextSize(5);
            tft.setTextColor(CYAN);
            tft.setCursor(100, 90);
            tft.println(myBPM);
            delay(150);
            tft.fillRect(10, 90, 50, 50, BLACK);
        }

        // Graph
        tft.drawRect(0, graphTop, 320, graphBottom - graphTop, WHITE);
        int sensor = analogRead(A7);

        // Begrenzung des analogRead
        sensor = constrain(sensor, 700, 1023);

        // Graphen-Skalierung
        int graphHeight = map(sensor, 700, 1023, graphBottom, graphTop);
        for (int i = 0; i < lineThickness; i++) {
            tft.drawLine(xPosPrev, graphHeightPrev + i, xPos, graphHeight + i, WHITE);
            tft.drawLine(xPosPrev, graphHeightPrev - i, xPos, graphHeight - i, WHITE);
        }
        graphHeightPrev = graphHeight;

        if (xPos >= 320) {
            xPos = startXPos; // Zurücksetzen der X-Position auf den Startpunkt
            xPosPrev = startXPos;
            tft.fillRect(startXPos, graphTop, 320 * 2, 480, BLACK); // Löschen des Graphenbereiches
        } else {
            xPosPrev = xPos;
            xPos++;
        }

        delay(16);
    }

    // Button-Stop
    bool down = Touch_getXY();
    off_btn.press(down && off_btn.contains(pixel_x, pixel_y));
    if (off_btn.justPressed()) {
        off_btn.drawButton(true);
        tft.fillScreen(WHITE);

        tft.setTextColor(BLACK);
        tft.setTextSize(2);
        tft.setCursor(10, 10);
        tft.print("Min BPM: ");
        tft.println(minBPM);

        tft.setCursor(10, 40);
        tft.print("Max BPM: ");
        tft.println(maxBPM);

        tft.setCursor(10, 70);
        tft.print("Avg BPM: ");
        if (countBPM > 0) {
            tft.println(sumBPM / countBPM);
        } else {
            tft.println("N/A");
        }

        // Stop further measurements
        measuring = false;
    }

  
}

Kann mir jemand helfen warum die Messung nicht startet? Der Touchscreen funktioniert mit dem Beispiel aus MCUfriend ohne Probleme, auch sind die Werte aus der Kalibierung im Code.
Danke im vorraus

Ich vermute mal, dass die Zeilen unterhalb von // Button-Stop gleich beim 1. Mal ausgeführt werden. MAch' da mal ein paar Serial.println() rein und schau was vor sich geht.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.