Create code for Arduino

What type of the code need for Arduino to turn on one time a day one LED light by using input signal of the sensor which provides digital square wave form ?

Your topic has been moved to the General Guidance category of the forum

It is not clear what you want to do. What type of sensor and is all you want to do is to turn on an LED when the sensor produces its one digital pulse a day or will it be producing many digital pulses a day?

If the latter, then what time do you want the LED to turn on ? In either case, when should the LED turn off ?

Which Arduino board do you plan to use ?

The proverbial crystal ball is still in the dishwasher...

âžś Please do yourself a favor and read How to get the best out of this forum. Post accordingly, including code with code tags, necessary documentation for your request such as your exact circuit and power supply details, links to components, etc.

  1. The sensor will make 58 analog pulses from the engine crankshaft position sensor to turn LED light on and after 58 pulses two pulses is missing to turn LED light off. 58 pulses takes 80 milliseconds.

  2. 58 pulses takes 80 milliseconds and how many milliseconds will take 1 pulse ?
    To find how many milliseconds 1 pulse takes divide the total time by the number of pulses:

Time per pulse = 80ms : 58 pulses

Performing the calculation:

So 1 pulse takes approximately 1.38 milliseconds.

  1. Two missed pulses is 1.38+1.38 =2,76 milliseconds

  2. After 58 present pulses 2 pulses is missing. In during 80 milliseconds 58 pulses LED light remains on.

  3. After two pulses 2.76 milliseconds LED light turns off and remains off but pulses continue running till next day.

  1. Are you saying that the missing pulses only occur once a day ?

Yes ones a day. Next day same task. Can Arduino adjust speed pulses calculation based on rotation of the crankshaft ? For an example if 58 pulses will takes 60 milliseconds because rotation is increased and time decreases. LED light will turn off earlier. Can Arduino handle this task ?

Always at the same time, every day?

1 Like

Certainly it is all possible. You need to begin some programming and you will learn how to do all those things.

I am interested to work with Arduino uno. I am trying to make it simple as possible based on this information. How the code will look like ?

  • Simple code should be replaced with trouble free code.
    You need to gain some experience in C++ coding, master the examples that come with the IDE.
  • Homework: :wink:

  • Some items below will be advanced at first.

1 Like

You are starting just fine by defining the program in writing. But nothing can be ambiguous, like "day". Day can mean 24 hours, can mean sunrise to sunset. Once a day could mean something can happen at 1 second before midnight and again at 1 second past midnight. So much better to use 24 hours rather than day.
So put what you want to do in writing and ask someone nearby to read it and tell you what it means to them. If not what you want, then rewrite what you want.
When all done, then you can think about writing a program to follow your description.

1 Like

If I start write how it should to be functioning then I will not realise how to write the code because it much more complicated to understand. I would understand how simple code works with Arduino uno then it will be easy for me to understand how to write complex code.

Earlier in my post I describe how the Arduino should turn on and of injector banks of 3 in the gasoline engine with 6 injectors which splits in two injector banks. One injector bank has 3 injectors wired together by negative terminal should spray fuel every engine revolution 80 milliseconds and while another injector bank of 3 is on hold for 80 milliseconds and so on depending on the engine speed. If Arduino can not handle it to adjust according engine speed then it is ok just work with one speed RPM750 in 80 milliseconds 58 pulses one injector bank on and another off. Injection time should be 5 milliseconds always if Arduino can not handle it speed timing. 5 milliseconds pulse width modulation should be good enough no matter what the crankshaft speed. Ones two missing pulses gone in first pulse injector bank should begin to inject fuel for 5 milliseconds only then next injector bank should begin operation same way.

What will see this led flash on and off so quickly?

Perhaps you do not realize that what you want is really not so simple at all?

I did ask GPT AI for code by describing how the Arduino should output signals to the injector banks to function on and off. The AI wrote the code for me but we are not sure if it going to work.

Actually you just trying to find excuses for yourself to not ever begin to understand programming at all. If you are really interested, stop all the stuff about times and days and pulses and stuff. Use the Arduino IDE to look at several of the sample programs. Find one that interests you and begin to work through it line by line and figure out what it does.

2 Likes

Let's say the quiet part out loud shall we? The OP wants to have the code written for them. First they tried ChatGPT, now they're trying here.

1 Like

I don’t know what you’re saying. I am trying to learn how to make a code with simple instructions. If artificial intelligence will make a code if it will be not working then human should do it. If human do it code based on information and that code will work then AI should repeat same code. If code will be different between human and AI then system will not work.

Does Arduino can work with both type of signals ? Analog signals and digital signals ?

Are you sure this code from AI will work ?

To make this project manageable and functional, let’s break it into steps and describe how the Arduino Uno could control the injector banks under the specified conditions. I’ll also provide a simplified code example for operation at a fixed RPM of 750.

Key Requirements:
1. Injector Banks: Two banks of 3 injectors each.
2. Timing:
• At 750 RPM, the crankshaft rotates 12.5 revolutions per second.
• Each injector bank operates alternately every 80 milliseconds (1/12.5 = 80 ms per revolution). and 360 degrees of the crankshaft.
3. Pulse Width:
• Each injector pulse lasts 5 milliseconds (fixed).
4. Operation:
• Bank A operates for 80 ms, including 5 milliseconds pulse width every time after two missing pulses then switches off. The crankshaft position sensor could send two type of the signals. One signal is analog and another is digital. If we choose analog signal then Arduino should count properly 58 teeth is equal to 58 pulses. Two pulses is missing and that time takes as same for two pulses. One pulse 1.38 millisecond. Two missed pulses is 1.38+1.38 =2,76 milliseconds
Every time after two missing pulses time to switch injector bank. What is wrong with AI to write correct code based on correct information.
• Bank B starts immediately after Bank A finished 80 ms 360 degrees crankshaft rotation at the engine crankshaft idle speed RPM750. We do not need speed adjustment for now if we can not fix it code with this information. If crankshaft speed 750 then revolusion is 80 millisecond. If crankshaft rotates faster Arduino might get lost that is why we have to make default 5 millisecond pulse width in any speed. This is going to be same as in original engine ECU. We call it LOS limited operating strategy. In original engine ECU LOS limited operating strategy all 6 injectors injecting fuel at the same time with fixed pulse wide modulation time is 5 milliseconds in any crankshaft speed. From 0 RPM up to 7000 RPM on the cluster gauge.
5. Fixed Speed:
• Simplify the logic for a constant engine speed of 750 RPM.

Simplified Pseudocode:
1. Initialize pins for the two injector banks.
2. Use a timer to generate 5 ms pulses for each injector bank.
3. Alternate between the two banks every 80 ms.

Arduino Code:

// Pin definitions for injector banks
const int bankA = 2; // Injector Bank A pin
const int bankB = 3; // Injector Bank B pin

// Timing parameters
const unsigned long cycleDuration = 80; // Duration for each bank in milliseconds
const unsigned long pulseDuration = 5; // Duration of each injection pulse in milliseconds
const int pulsesPerCycle = 58; // Number of pulses in 80 ms at 750 RPM

void setup() {
pinMode(bankA, OUTPUT);
pinMode(bankB, OUTPUT);

// Ensure both banks are off initially
digitalWrite(bankA, LOW);
digitalWrite(bankB, LOW);
}

void loop() {
// Operate Bank A
for (int i = 0; i < pulsesPerCycle; i++) {
digitalWrite(bankA, HIGH); // Turn on injectors in Bank A
delay(pulseDuration); // Wait for pulse duration
digitalWrite(bankA, LOW); // Turn off injectors
delay((cycleDuration / pulsesPerCycle) - pulseDuration); // Wait for remaining time in cycle
}

// Operate Bank B
for (int i = 0; i < pulsesPerCycle; i++) {
digitalWrite(bankB, HIGH); // Turn on injectors in Bank B
delay(pulseDuration); // Wait for pulse duration
digitalWrite(bankB, LOW); // Turn off injectors
delay((cycleDuration / pulsesPerCycle) - pulseDuration); // Wait for remaining time in cycle
}
}

Explanation:
1. Timing Calculation:
• Each cycle is 80 ms, divided into 58 pulses.
• Each pulse includes a 5 ms “on” time and a remaining “off” time of approximately 1.379 ms.
• The code ensures accurate timing using delay().
2. Bank Switching:
• The loop alternates operation between Bank A and Bank B.
• Each bank operates for a complete 80 ms before switching.
3. Fixed Speed:
• The code assumes a fixed crankshaft speed of 750 RPM for simplicity.

Limitations:
• The code uses delay(), which can block other operations. If you need more flexibility (e.g., adjusting to engine speed dynamically), you may need to use timers or interrupts.
• The logic assumes perfect timing for simplicity. In real-world applications, precise synchronization with the crankshaft position sensor would be required.