i do not know if :edit-copy to forum, worked out, anyway, this is the second code from my text:
https://arduino.cc/`Gebruik code tags om code te formatteren voor het forum`#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
// --- PINS ---
const int fijnFreqPin = A0;
const int mainFreqPin = A1;
const int powerPin = A3; // De potmeter voor D en P
const int schuPin = 7;
const int stopPin = 8;
const int pausePin = 9;
const int LPWM = 3;
const int RPWM = 5;
// --- VARS ---
float smoothedFreq = 4.0;
int dutyCycle = 25; // Startwaarde
int actuelePower = 127; // Startwaarde
bool isSystemRunning = true;
bool richting = true;
int currentPhase = 0;
unsigned long phaseStartTime = 0;
unsigned long displayMillis = 0;
unsigned long vorigePulsMicros = 0;
unsigned long lastSerial = 0;
float powerMultiplier = 1.0;
const unsigned long WORK_PHASE = 120000;
const unsigned long TRANSITION_TIME = 5000;
const unsigned long BREAK_PHASE = 60000;
unsigned long lastStopPress = 0;
unsigned long lastPausePress = 0;
const unsigned long debounceTime = 300;
bool showStopMessage = false;
unsigned long stopMessageStart = 0;
void setup() {
Serial.begin(9600);
pinMode(schuPin, INPUT_PULLUP);
pinMode(stopPin, INPUT_PULLUP);
pinMode(pausePin, INPUT_PULLUP);
pinMode(LPWM, OUTPUT);
pinMode(RPWM, OUTPUT);
lcd.init();
lcd.backlight();
lcd.clear();
phaseStartTime = millis();
}
void startPuls() {
if (!isSystemRunning) return;
int outputPower = (float)actuelePower * powerMultiplier;
outputPower = constrain(outputPower, 0, 255);
if (richting) {
analogWrite(RPWM, outputPower);
analogWrite(LPWM, 0);
} else {
analogWrite(LPWM, 0);
analogWrite(RPWM, outputPower);
}
}
void stopPuls() {
analogWrite(LPWM, 0);
analogWrite(RPWM, 0);
}
void handlePhaseTransitions() {
if (!isSystemRunning) return;
unsigned long phaseElapsed = millis() - phaseStartTime;
if (currentPhase == 0 && phaseElapsed >= WORK_PHASE) {
currentPhase = 1;
phaseStartTime = millis();
} else if (currentPhase == 1) {
powerMultiplier = 1.0 - ((float)phaseElapsed / TRANSITION_TIME);
if (powerMultiplier <= 0) {
powerMultiplier = 0;
currentPhase = 2;
phaseStartTime = millis();
}
} else if (currentPhase == 2 && phaseElapsed >= BREAK_PHASE) {
currentPhase = 3;
phaseStartTime = millis();
richting = !richting;
} else if (currentPhase == 3) {
powerMultiplier = (float)phaseElapsed / TRANSITION_TIME;
if (powerMultiplier >= 1.0) {
powerMultiplier = 1.0;
currentPhase = 0;
phaseStartTime = millis();
}
}
}
void loop() {
unsigned long nuMillis = millis();
unsigned long nuMicros = micros();
handlePhaseTransitions();
// Knoppen checken
if (digitalRead(stopPin) == LOW && (nuMillis - lastStopPress > debounceTime)) {
lastStopPress = nuMillis;
isSystemRunning = !isSystemRunning;
if (!isSystemRunning) {
stopPuls();
showStopMessage = true;
stopMessageStart = nuMillis;
lcd.clear();
lcd.print("STOPPED");
} else {
showStopMessage = false;
phaseStartTime = nuMillis;
currentPhase = 0;
lcd.clear();
}
}
// Frequentie berekening
float doelFreq;
if (digitalRead(schuPin) == LOW) {
doelFreq = 7.83;
} else {
doelFreq = (analogRead(mainFreqPin) / 1023.0) * 481.0 + 9.0;
doelFreq += ((analogRead(fijnFreqPin) - 512.0) / 512.0) * 5.0;
}
smoothedFreq = (smoothedFreq * 0.9) + (doelFreq * 0.1);
// --- HIER WORDT DE POTMETER A3 UITGELEZEN ---
int potA3 = analogRead(powerPin);
dutyCycle = map(potA3, 0, 1023, 5, 50); // D: 5% tot 50%
actuelePower = map(potA3, 0, 1023, 20, 255); // P: 20 tot 255 PWM
// Puls generatie
if (isSystemRunning && currentPhase != 2) {
unsigned long pulsInterval = 1000000.0 / smoothedFreq;
unsigned long aanTijd = (pulsInterval * dutyCycle) / 100;
if ((nuMicros - vorigePulsMicros) < aanTijd) {
startPuls();
} else if ((nuMicros - vorigePulsMicros) < pulsInterval) {
stopPuls();
} else {
vorigePulsMicros = nuMicros;
}
} else {
stopPuls();
}
// Display update
if (nuMillis - displayMillis > 250) {
displayMillis = nuMillis;
if (!showStopMessage && isSystemRunning) {
lcd.setCursor(0, 0);
lcd.print("F:");
lcd.print(smoothedFreq, 1);
lcd.print(" D:");
lcd.print(dutyCycle);
lcd.print("% ");
lcd.setCursor(0, 1);
lcd.print("P:");
lcd.print((int)((actuelePower / 255.0) * 100));
lcd.print("% Ph:");
lcd.print(currentPhase);
lcd.print(richting ? " R " : " L ");
}
if (showStopMessage && (nuMillis - stopMessageStart > 1500)) {
showStopMessage = false;
lcd.clear();
}
// Debug naar Seriële monitor: draai aan de potmeter en kijk hier
Serial.print("Potmeter A3: ");
Serial.print(potA3);
Serial.print(" | Duty: ");
Serial.print(dutyCycle);
Serial.print(" | Power: ");
Serial.println(actuelePower);
}
}
i do not understand what thing does in the answering block
