Honestly I depend on Controllino code generator constantly. It's the only practical way for me, and I can usually get a code that works.
But now I seem to have a communication problem with it.
I need a 50% duty cycle output to control MOSFET triggered gate, for solenoid.
Almost whatever I say, it gives me a 14% duty cycle or 25% duty cycle.
Even when I specify a 1ms high, and 1ms low, it gives me a cycle like 20%.
Is there some library or specific terminology that I could dictate into the otherwise great code generator that might help?
Thanks in advance.
Using AI makes more work than doing it yourself. It's like a bad employee you assign a task, and they always mess it up, so your choices are to spend 50% more of your time babysitting that employee until they get it right, or spend 100% of your time finishing it right yourself. Both ways get you to the goal.
Posting your code here will ensure many views and the expert advice they have.
Yes, be smart and code yourself.
OK. Here's what I asked it;
Define pinA0
Define pin 4
Define pin 5,
Define pin 6.
When Threshold at AO is met a duty cycle is emitted. I only want a 50% duty signal. A frequency around 100 hurts is okay
The thresholds are P4 1.7v,
P5 2.6v,
P6 3.5v.
The signal emitted shall remain as long as voltage is above each corresponding threshold.
This shall update repeatedly while board is powered.
All outputs and inputs should be readable on serial monitor.
Controllini;
#define ANALOG_INPUT_PIN A0
#define OUTPUT_PIN_4 5
#define OUTPUT_PIN_5 6
#define OUTPUT_PIN_6 7
// Thresholds in voltage
#define THRESHOLD_P4 1.7
#define THRESHOLD_P5 2.6
#define THRESHOLD_P6 3.5
// Frequency and duty cycle
#define FREQUENCY 100
#define DUTY_CYCLE 50
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set the output pins
pinMode(OUTPUT_PIN_4, OUTPUT);
pinMode(OUTPUT_PIN_5, OUTPUT);
pinMode(OUTPUT_PIN_6, OUTPUT);
}
void loop() {
// Read the analog value
int sensorValue = analogRead(ANALOG_INPUT_PIN);
// Convert to voltage
float voltage = sensorValue * (5.0 / 1023.0);
// Check against thresholds and emit signal
if (voltage >= THRESHOLD_P6) {
emitSignal(OUTPUT_PIN_6);
} else if (voltage >= THRESHOLD_P5) {
emitSignal(OUTPUT_PIN_5);
} else if (voltage >= THRESHOLD_P4) {
emitSignal(OUTPUT_PIN_4);
} else {
// Turn off all outputs if below the lowest threshold
digitalWrite(OUTPUT_PIN_4, LOW);
digitalWrite(OUTPUT_PIN_5, LOW);
digitalWrite(OUTPUT_PIN_6, LOW);
}
// Print the status to the serial monitor
Serial.print("Analog Input Voltage: ");
Serial.print(voltage);
Serial.print(" V, Output States - P4: ");
Serial.print(digitalRead(OUTPUT_PIN_4));
Serial.print(", P5: ");
Serial.print(digitalRead(OUTPUT_PIN_5));
Serial.print(", P6: ");
Serial.println(digitalRead(OUTPUT_PIN_6));
// Small delay to stabilize and provide readable output
delay(10);
}
void emitSignal(int pin) {
// Emit a 50% duty cycle signal at approximately 100Hz
digitalWrite(pin, HIGH);
delayMicroseconds(5000); // High for 5ms
digitalWrite(pin, LOW);
delayMicroseconds(5000); // Low for 5ms
}
Works, except no 50% pulse, lower frequency, and only A0 on Serial Monitor.
That's... painful!
Did I say that? Painful lol.
Interesting, how many other bugs are there that you have not found. Start from scratch and write it yourself, it will probably take weeks less to get it operating and you may eliminate some of the other bugs.
Consider there are several experienced users telling you the same thing. We work from experience.
A0
is not
AO
What is the sensor raw value?
I understand that's an option, but I'm not in a position to do that right now.
Maybe someday I will be.
I don't do a lot of coding. This is part of a larger mechanical device that Ive spent hundreds of hours making with bandsaws drills, welding etc.
When its done I'm moving on to video presentation, PR and sales.
I do a full time job on the side.
I'm fascinated by Arduino and even have navy electronics training to keep me interested.
But I have things pressing from all directions.
I knew zero 3 months ago, and am proud of what I learned so far.
How long did it take you to
codewhatever you want, proficiently?
I'm like Bronson in the movie "Escape".
He asks the helicopter pilot "How long will it take untill I'm as good as you?"
The pilot replies "You'll never be as good as me." And I'm OK with that! lol
Thank you sir for your opinion.
You have a function emitSignal():
void emitSignal(int pin) {
// Emit a 50% duty cycle signal at approximately 100Hz
digitalWrite(pin, HIGH);
delayMicroseconds(5000); // High for 5ms
digitalWrite(pin, LOW);
delayMicroseconds(5000); // Low for 5ms
}
When you call this function, it generates a single pulse that is high for 5ms and then low for 5ms.
If you were calling this function continuously then the output would be nearly a 100Hz, 50% duty cycle square wave.
However after you have called the function, you do some printing to the serial monitor, and then you have a delay(10). All this adds extra time onto the 5ms that you wanted the pulse output to be low.
In the following oscilloscope traces, to show you how long these actions take:
I have turned a pin high at the start of the printing, and low again at the end of the printing. I have turned another pin high before the delay, and low again after the delay.
(I used direct port manipulation to change these pins, so it only added 3 clock cycles to the loop time.)
The oscilloscope trace shows:
- Channel 1 (yellow trace) - The required pulse.
- Channel 2 (magenta trace) - Time taken for Serial.print().
- Channel 4 (green trace) - Time taken for delay(10) .
It can be seen that the printing adds 50ms, and the delay adds 10ms, so the period of the generated pulse is 70ms, instead of 10ms.
Now look what happens if I comment out the printing and delay:
Waveform is now close to 100Hz, 50% duty cycle.
It is your printing and delay() that make your output wrong.

You have a function emitSignal():
Waveform is now close to 100Hz, 50% duty cycle.
It is your printing and delay() that make your output wrong.
Wow. Thanks,
That helps immensely.
I'm back on it.
Thanks.
Edit: A little tweaking; a new approach and it runs perfect now. Thanks.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.