import [Link].
*;
Serial myPort;
float voltage = 0;
ArrayList<Float> voltageHistory = new ArrayList<Float>();
int maxHistory = 200;
// Variables pour l’animation du cœur
float heartSize = 100;
float heartBeat = 1.0;
long lastBeatTime = 0;
boolean beating = false;
// Variable pour gérer l’état de la connexion
boolean portConnected = false;
String connectionStatus = “Déconnecté”;
void setup() {
size(1200, 600);
// Initialisation de l’historique
for (int i = 0; i < maxHistory; i++) {
[Link](0.0);
textAlign(CENTER, CENTER);
textSize(16);
// Configuration du port série
setupSerialPort();
void setupSerialPort() {
try {
// Liste des ports série disponibles
println(”Recherche des ports série disponibles...”);
String[] ports = [Link]();
if ([Link] == 0) {
println(”Aucun port série détecté !”);
connectionStatus = “Aucun port série détecté”;
return;
println(”Ports série disponibles (” + [Link] + “ trouvés):”);
for (int i = 0; i < [Link]; i++) {
println(i + “: “ + ports[i]);
// Essai de connexion automatique
boolean connected = false;
for (int i = 0; i < [Link]; i++) {
try {
println(”Tentative de connexion sur: “ + ports[i]);
myPort = new Serial(this, ports[i], 9600);
[Link](’\n’);
// Attendre un peu pour voir si la connexion fonctionne
delay(1000);
if (myPort != null) {
portConnected = true;
connectionStatus = “Connecté à: “ + ports[i];
println(”✓ Connexion réussie sur: “ + ports[i]);
connected = true;
break;
catch (Exception e) {
println(”✗ Échec de connexion sur “ + ports[i] + “: “ + [Link]());
if (myPort != null) {
[Link]();
if (!connected) {
println(”Aucun port série valide trouvé !”);
connectionStatus = “Aucun port valide - Vérifiez la connexion”;
}
}
catch (Exception e) {
println(”Erreur lors de l’initialisation série: “ + [Link]());
connectionStatus = “Erreur d’initialisation”;
void draw() {
background(30);
// Affichage en deux parties
drawHeartAnimation();
drawGraph();
// Affichage de la valeur numérique et statut
fill(255);
text(”Tension mesurée: “ + nf(voltage, 1, 2) + “ V”, width/2, 30);
// Affichage du statut de connexion
fill(portConnected ? color(0, 255, 0) : color(255, 0, 0));
text(”Statut: “ + connectionStatus, width/2, 55);
// Instructions
fill(200);
textSize(12);
text(”Appuyez sur R pour réessayer la connexion | ESPACE pour effacer l’historique”, width/2,
height - 20);
}
void drawHeartAnimation() {
pushMatrix();
translate(width/4, height/2);
if (portConnected) {
// Animation du battement
float currentTime = millis();
if (currentTime - lastBeatTime > 500 && voltage > 1.0) {
beating = true;
lastBeatTime = currentTime;
if (beating) {
heartBeat = sin((currentTime - lastBeatTime) * 0.02) * 0.3 + 1.0;
if (currentTime - lastBeatTime > 300) {
beating = false;
heartBeat = 1.0;
// Dessin du cœur
fill(255, 0, 0, 150);
stroke(255, 100, 100);
strokeWeight(2);
beginShape();
for (float angle = 0; angle < TWO_PI; angle += 0.01) {
float r = heartSize * heartBeat * heartFunction(angle);
float x = r * cos(angle);
float y = -r * sin(angle);
vertex(x, y);
endShape(CLOSE);
// Indicateur de battement
if (voltage > 1.0) {
fill(0, 255, 0);
text(”❤ BATTEMENT DÉTECTÉ ❤”, 0, heartSize * 1.5);
} else {
fill(150);
text(”En attente de signal...”, 0, heartSize * 1.5);
} else {
// Affichage quand déconnecté
fill(100, 100, 100, 100);
stroke(150);
strokeWeight(2);
beginShape();
for (float angle = 0; angle < TWO_PI; angle += 0.01) {
float r = heartSize * heartFunction(angle);
float x = r * cos(angle);
float y = -r * sin(angle);
vertex(x, y);
endShape(CLOSE);
fill(255, 0, 0);
text(”❌ DÉCONNECTÉ”, 0, heartSize * 1.5);
text(”Appuyez sur R”, 0, heartSize * 1.5 + 20);
popMatrix();
float heartFunction(float angle) {
return (1 - sin(angle)) * (1 - 0.5 * abs(cos(angle))) * (0.5 + 1.5 * abs(cos(angle)));
void drawGraph() {
pushMatrix();
translate(width/2, 0);
// Fond du graphique
fill(40);
stroke(100);
rect(0, 50, width/2 - 20, height - 100);
// Échelle verticale (tension)
stroke(150);
for (int i = 0; i <= 5; i++) {
float y = map(i, 0, 5, height - 100, 50);
line(30, y, width/2 - 20, y);
fill(200);
text(i + “V”, 15, y);
if (portConnected && [Link]() > 1) {
// Courbe de tension
stroke(0, 255, 255);
strokeWeight(2);
noFill();
beginShape();
for (int i = 0; i < [Link](); i++) {
float x = map(i, 0, [Link]() - 1, 30, width/2 - 20);
float y = map([Link](i), 0, 5, height - 100, 50);
vertex(x, y);
endShape();
// Ligne de la valeur actuelle
stroke(255, 255, 0, 100);
float currentY = map(voltage, 0, 5, height - 100, 50);
line(30, currentY, width/2 - 20, currentY);
}
// Affichage des informations du graphique
fill(255);
text(”Historique de la tension”, (width/2 - 20)/2, 20);
text(”Temps →”, (width/2 - 20)/2, height - 30);
popMatrix();
void serialEvent(Serial myPort) {
try {
if (myPort != null && [Link]() > 0) {
String inString = [Link](’\n’);
if (inString != null) {
inString = trim(inString);
// Vérifier que c’est un nombre valide
if ([Link]() > 0 && [Link](”[0-9.]+”)) {
voltage = float(inString);
// Limiter la tension à une plage raisonnable
voltage = constrain(voltage, 0, 5);
// Ajout à l’historique
[Link](voltage);
if ([Link]() > maxHistory) {
[Link](0);
}
}
catch (Exception e) {
println(”Erreur de lecture série: “ + [Link]());
// Tentative de reconnexion
portConnected = false;
connectionStatus = “Erreur de lecture - Appuyez sur R”;
void keyPressed() {
if (key == ‘ ‘) {
// Réinitialisation de l’historique
[Link]();
for (int i = 0; i < maxHistory; i++) {
[Link](0.0);
println(”Historique réinitialisé”);
if (key == ‘r’ || key == ‘R’) {
// Réessayer la connexion série
println(”Tentative de reconnexion...”);
if (myPort != null) {
[Link]();
}
setupSerialPort();
// Fonction pour gérer la fermeture propre
void exit() {
if (myPort != null) {
[Link]();
[Link]();