Help needed with Project: Controlling 16 Led's with 2 Input sensors, to perform specific functions

Hi, Everyone
I am new to Arduino, playing around with the basic examples. but starting to get a bit more ambitious :grinning:
I've recently started with a project involving multiple led's (not a matrix or a cube) . Most examples I've seen, involve making led's blink or light up in some or other pattern of sorts, not much involving multiple led's controlled by multiple input values from multiple sensors.

Now, I've looked at State Machines, Switch Case examples, even RTOS's like FreeRTOS, etc...
Most only to show the same effects or outcomes as mentioned above.

I don't have a sketch for this....

So I need help with my Project as follows: -
Setup:

  1. Using an Arduino Uno
  2. 16 x Led's, using the Digital Pins 3 to 13 & Analogue Pins A1 to A5. All as OUTPUTS
  3. On Digital Pin 2, I want to use as an INPUT to measure Rpm
  4. On Analogue A0, I want to measure and monitor Voltage

The Plan:
The following is required to happen:-
Step 1.
Based on a specified rpm values:=
Example:
*At 20 rpm, switch ON specific led's ON (Led's 1 , 2, 12 = ON)
*At 100 rpm, switch ON more led's AND switch OFF others (Led's 3, 5, 6 = ON; 1, 2 = OFF)
*At 300 rpm, switch ON more led's AND switch OFF others (Led's 7, 10, 11 = ON; 5, 6 = OFF)

The led's that were switched ON must remain ON until the Max rpm is reached.(Max = 500 rpm)
This cycle must NOT be repeated again.

Step 2:
*At 500 rpm, led 12 must be switched OFF, but switched back ON at 400 rpm. This must infinitely continue. (Led 12 must be switched ON at min 400 rpm & OFF at max 500 rpm)

Step 3:
This part must be based on Voltage Values (Max = 36 volts, Min = 33 volt)
*At Default state = Max 36 volt, switch ON specific led's ON (Led's 8, 9 = ON; 2, 5 = OFF) )
*IF voltage = Min 33 volt, start the following sequence:-
1) Led's 3, 5, 6 = ON; 1, 2 = OFF for 60 seconds (60000 milliseconds), then next
2) Led's 7, 10, 11 = ON; 5, 6 = OFF for 60 seconds (60000 milliseconds), then next
3) Led's 3, 13, 14, 15 = ON; 7, 10 = OFF for 60 seconds (60000 milliseconds), then next
4) Return to Default state to check if Voltage = Max 36 volt, then to repeat cycle until Max voltage = 36 volt.

 This process/cycle then only repeats when Min = 33 volt is reached.

THIS MUST ALL be happening virtually at the same time (Multitasking)

I'm not sure which would be better: - Switch Case, State Machine or RTOS to accomplish the above? or How to make this work?

Any help in this matter would be much appreciated!

Kind Regards
Erwin

start with sketch where you get RPM and voltage.

Take a look at


I just googled thinking this has been done, if not your odd lighting patterns, you do you, but at least the input effects the LEDs in the question of RPM.

I'm not sure I can get enough from section 1. and 2. to make any sense of it. It seems underspecific and raises more questions then it answers. Again, my presumption is that what you want to do is consistent and logical, and can be well enough stated to code against.

The voltage monitor thing is simple enough to code in a non blocking way. Thank you for making sure we understood about the 60 second (60000 milliseconds) thing.

With a max RPM of 500 and the battery thing, the times are so vast this would be perfect for finite state machinery. I'd just do one for each halve of the project.

HTH

a7

Just wondering if you tried this beginner level tutorial on Arduino and timers? It should change your view.

Nick Gammon's Do multiple things at the same time.

For example watch RPM, V and clocks at the same time (in a blink or less.)

"not much involving multiple led's controlled by multiple input values from multiple sensors."

Many things at once as non-blocking tasks in void loop() can watch the inputs and update the state which may involve elapsed time. Process tasks read the input states and set outputs that switch leds on/off and use time as well. All tasks run together. The tasks pass data and flags in global variables. The combination can achieve faster than eyes results.

OK, I can make some sense of you words, but maybe some kind of diagram or timing chart woukd help.

Also I find myself wondering what this thing is, can you say few words about what it is, and in what circumstances it is to be deployed?

Say a bit about what this thing will look like, too. I finally noticed you are overworking the LEDs, I hope you didn't stop at N LEDs because you ran out of pins… there are several easy and commonly employed ways to get more LEDs onto fewer pins.

a7

What must happen when the RPM is between 20 and 100 AND the voltage is ar default state? You seem to have a conflict between the two quoted texts; there might be more possible conflicts.

I suggest that you make a table (something like below as a guideline) and fill it in completely.

image

simple checking if voltage = XXX and rpm == YYY
then
execute a function that switches on/off the LEDs with your pattern

If these LED-pattern in the sense of which LEDs are on and which LEDs are off
have a a pattern in itself with certain intervals

it is possible to transfer this into more compact code by using additional variables.

You haven't described your knowledge-level precise enough for me to be able to assess whether this program is really suitable as the next step.

my guessing about your knowledge is: very ambitious you should not try it in one step
It is very unlikely to find code that comes close to your special requirements to learn from existing code.
It is much more likely to have success by doing easier tasks in the sense of having less complex conditions

best regards Stefan

Thanks for the reply.
I have done Rpm and Voltage separately, but I'm struggling to get these to work together at the same time.

Most sketches I've seen only make led's blink at different rates without sensor inputs.

Thank you!

Thank you for the advice.

Thank you for your interest.
Simply put:-
Each Led represents a switch/relay that needs to be activated at specific times, sequences and voltage values as described.

The first part is a start-up sequence based on the rpm. Switches turned ON stay on until the other functions turn them ON or OFF as required.
This only happens once at start-up, where after only 1 switch is controlled by the rpm threshold between say 400 and 500 rpm (infinitely)
The rest of the other switches are then controlled based on voltage thresholds between say min 33 and max 36 volts. At the min 33 volt threshold, a sequence is triggered and repeated until the max 36 volt threshold is reached, then returns to the default state until the voltage drops again to the min 33 volt threshold, then re- activates the said sequence.....there should be no end to this process.

Switching led's ON and OFF based on voltage or rpm values is no problem, when done separately, but having them do this at the same time is more complicated.
I am aware of Finite State Machines, Multitasking, Switch Case....etc... All examples I've seen only blink led's......So I'm struggling to implement my idea using these....making the dots connect...

As I mentioned...I'm new to programming (learning fast :grin: though) Nothing wrong with the imagination....

I'm sorry I can't say too much about the project itself, as it I'm still researching all the possibilities to see how viable it could be. However, I believe this is quite possible and could pave the way to controlling many similar applications/projects..

So thank you to all for your contributions, advice and help!!

Sorry for the confusing info.(Typo errors!!)...this happens when you copy/paste :crazy_face:
This could also be any random number....Just see the Led's mentioned in brackets.

Specific actions are required to trigger at the mentioned rpm values, nothing in between. If an led/switch is switched ON it must stay ON until otherwise switched OFF or ON as required.

Thanks for the Table idea.

Just remember, ALL led's/switched are individually controlled in the project, not as a pattern or any other function(blinking), only ON/OFF.

Thanks Stefan for your input.

Sometimes the only way to learn how to swim is to be thrown onto the deep end :crazy_face:

But, at least I'm learning to eat this elephant, 1 piece at a time....bit by bit :grinning:

Hi guys,

I tried ChatGPT and got this Code (1 of many variants).
Kindly comment on what you think based on the requirements of the project.

The Code:

// Pin assignments
const int rpmPin = 2;
const int voltagePin = A0;

// LED pin assignments
const int ledPins[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, A1, A2, A3, A4, A5, 13};

// RPM thresholds and LED configurations
const int rpmThresholds[] = {20, 100, 300, 500};
const int rpmLedConfigs[][16] = {
  {1, 2, 12},              // 20 RPM
  {3, 5, 6, 1, 2},         // 100 RPM
  {7, 10, 11, 5, 6},       // 300 RPM
  {7, 10, 11, 5, 6, 12}    // 500 RPM
};
const int numRpmConfigs = sizeof(rpmThresholds) / sizeof(rpmThresholds[0]);

// Voltage thresholds and LED configurations
const int voltageThresholdMin = 33;
const int voltageThresholdMax = 36;
const int voltageLedConfigs[][16] = {
  {8, 9, 2, 5},                           // Default state (Max voltage)
  {3, 5, 6, 1, 2},                        // Voltage = Min 33V, sequence 1
  {7, 10, 11, 5, 6},                      // Voltage = Min 33V, sequence 2
  {3, 13, 14, 15, 7, 10}                  // Voltage = Min 33V, sequence 3
};
const int numVoltageConfigs = sizeof(voltageLedConfigs) / sizeof(voltageLedConfigs[0]);

// Timing
const unsigned long sequenceDuration = 60000;  // 60 seconds in milliseconds

// Variables
unsigned long previousMillis = 0;
int currentRpmConfig = -1;
int currentVoltageConfig = -1;

void setup() {
  // Set LED pins as outputs
  for (int i = 0; i < 16; i++) {
    pinMode(ledPins[i], OUTPUT);
  }

  // Set RPM pin as input
  pinMode(rpmPin, INPUT);

  // Start serial communication
  Serial.begin(9600);
}

void loop() {
  // Read RPM and voltage values
  int rpm = getRpm();
  int voltage = getVoltage();

  // Check RPM thresholds and update LED configuration
  for (int i = 0; i < numRpmConfigs; i++) {
    if (rpm <= rpmThresholds[i]) {
      if (currentRpmConfig != i) {
        currentRpmConfig = i;
        updateLedConfiguration(rpmLedConfigs[i], 16);
      }
      break;
    }
  }

  // Check voltage thresholds and update LED configuration
  if (voltage <= voltageThresholdMin) {
    if (currentVoltageConfig != 0) {
      currentVoltageConfig = 0;
      updateLedConfiguration(voltageLedConfigs[0], 4);
    }
  } else if (voltage >= voltageThresholdMax) {
    if (currentVoltageConfig != -1) {
      currentVoltageConfig = -1;
      updateLedConfiguration(voltageLedConfigs[0], 4);
    }
  } else {
    int sequenceIndex = (voltage - voltageThresholdMin - 1) % numVoltageConfigs;
    if (currentVoltageConfig != sequenceIndex + 1) {
      currentVoltageConfig = sequenceIndex + 1;
      updateLedConfiguration(voltageLedConfigs[sequenceIndex + 1], 16);
    }
  }

  // Handle LED 12 switching on/off at 400/500 RPM
  if (rpm >= 400 && rpm < 500) {
    digitalWrite(ledPins[11], HIGH);  // Turn on LED 12
  } else {
    digitalWrite(ledPins[11], LOW);   // Turn off LED 12
  }

  // Check if a sequence should be executed
  if (currentVoltageConfig != -1) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= sequenceDuration) {
      previousMillis = currentMillis;
      int nextSequenceIndex = (currentVoltageConfig - 1) % numVoltageConfigs;
      updateLedConfiguration(voltageLedConfigs[nextSequenceIndex + 1], 16);
    }
  }
}

int getRpm() {
  // Replace this with your actual RPM measurement code
  // Return the measured RPM value
  return analogRead(rpmPin);
}

int getVoltage() {
  // Replace this with your actual voltagemeasurement code
  // Return the measured voltage value
  return analogRead(voltagePin);
}

void updateLedConfiguration(const int ledConfig[], int numLeds) {
  // Turn off all LEDs
  for (int i = 0; i < 16; i++) {
    digitalWrite(ledPins[i], LOW);
  }

  // Turn on specified LEDs
  for (int i = 0; i < numLeds; i++) {
    digitalWrite(ledPins[ledConfig[i] - 1], HIGH);
  }
}

As long as you describe your wanted functionality in such general terms like in the qoutation above. The advice how to code this must stay in general terms still pretty far away from real code.

I take this sentence from you:

Based on this description the advice is:
code an if-condition according to the required rpm beeing between 400 and 500 rpm. Take a rm-measuring library of your choice to measure rpm.
End of advice.

Does this help writing code? I'm pretty sure it does not help writing real .
code

Suggestions with real lines of code can only be made if you specify with much more precision what you want.

example:
startup-sequence:
if rpm is below 400 rpm IO-pins 2,3,4,5 shall stay switched off
if rpm is inbetween 400 rpm and 500 rpm switch on IO-pin 2 and 3

You are talking about switches. What exactly in terms of microcontroller-hardware do you mean. The microcontroller has no switches. The microcontroller has IO-pins.

You should be much much more precise in your description. You will finish your project 3 times faster if you learn to use the standard terms used in microcontroller-programming. Should be no problem for you as you stated

best regards Stefan

I will

not

even read a single line of code that is created by chatGPT.

The reason is:
using chatGPT creats the situation that you do not understand the code and now you want other to analyse the code for you while depending on the precision that you have made as the chat-prompt chatGPT might even use libraries that are inadequate.
This means you try to force other people to correct an even more buggy code than a real beginner would write.

See my explanation given here

:rofl:
Well how does it look?
Could it actually work?

Thanks!

I don't know I don't look at it

upload the code to your microcontroller and see what happends