Using AI to write Arduino code

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

typ of plak hier code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

const int dutyPin = A0;      // Pot 1
const int mainFreqPin = A1;  // Pot 2
const int powerPin = A3;     // Pot 3
const int LPWM = 3;
const int RPWM = 5;

float smoothedFreq = 7.83;
int valDuty = 25;
int valPower = 127;
bool isRunning = true;
unsigned long lastPulseMicros = 0;
unsigned long lastDisp = 0;

void setup() {
  Serial.begin(9600);
  pinMode(LPWM, OUTPUT);
  pinMode(RPWM, OUTPUT);
  lcd.init();
  lcd.backlight();
}

void loop() {
  unsigned long nu = millis();
  unsigned long nuMicros = micros();

  // LEES POTMETERS
  // Duty Cycle (D) via A0 - met extra stabiliteit
  int rawA0 = analogRead(dutyPin);
  delay(1);  // Geef de ADC tijd om te schakelen
  valDuty = map(rawA0, 0, 1023, 5, 50);

  // Freq (F) via A1
  int rawA1 = analogRead(mainFreqPin);
  float f = (rawA1 / 1023.0) * 481.0 + 9.0;
  smoothedFreq = (smoothedFreq * 0.9) + (f * 0.1);

  // Power (P) via A3
  valPower = map(analogRead(powerPin), 0, 1023, 0, 255);

  // PULS GENERATOR
  unsigned long period = 1000000.0 / smoothedFreq;
  unsigned long onTime = (period * valDuty) / 100;
  unsigned long cyclePos = nuMicros - lastPulseMicros;

  if (cyclePos < onTime) {
    analogWrite(RPWM, valPower);
    analogWrite(LPWM, 0);
  } else if (cyclePos < period) {
    analogWrite(LPWM, 0);
    analogWrite(RPWM, 0);
  } else {
    lastPulseMicros = nuMicros;
  }

  // DISPLAY
  if (nu - lastDisp > 300) {
    lastDisp = nu;
    lcd.setCursor(0, 0);
    lcd.print("F:");
    lcd.print(smoothedFreq, 1);
    lcd.print(" D:");
    lcd.print(valDuty);
    lcd.print("%  ");
    lcd.setCursor(0, 1);
    lcd.print("P:");
    lcd.print((int)((valPower / 255.0) * 100));
    lcd.print("%   ");

    // Debug naar PC
    Serial.print("Raw A0: ");
    Serial.println(rawA0);
  }
}
 
the first code in my text, maybe now better by edit, make for forum

Always use < code/ > code tags:

#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);
  }
}

is the last one with code tags? No? what is it , and what to do

Yes, you have the code tags correct. Unlike Golam’s which did not include the first few lines.

Hi, @wimtttt

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Please provide links to data of powersupplies and other hardware.
Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

I have missed only the following line which is added now:
#include <Wire.h>

Than you Golam Mostafa, I include it, was this code important, do not know, was already working. Only thing I want is: the amp potmeter does 2 things. change amps till 100 %, and change pulsation til 50 %, is ok, but the last one better on my not used potmeter, 4 till 100%, if possible. And that is the code in the first code Gemini says, but the code is much shorter, tried it, and actually it does nothing, so forget the first script