Switch/Case arduino nano PWM control

Hello,
I am trying to control the PWM outputs of a NANO, depending on the analog input A1 (0-1023), and here is what I am trying to achieve:

All 5 PWM outputs (3,5,6,9,10) will be controlled just as simple output with two options - ON or OFF.
Between each of this steps, the software should adjust the 6th pwm output D11 from 0-255 and then the next pwm pin should be toggled ON or OFF, but once the next pin gets pulled HIGH, the 6th PWM should be turned off. With increasing the analog input, the next pwm should be pulled ON, and so on.

Pin 11 is the PWM output which will be controlled with PWM.

Once the duty cycle of pin 11 reach the maximum, then pin 3 should be switched ON and pin 11 OFF. Later on, with increasing the analog input voltage, the pin 11 should start controlling the duty cycle, and again, once it reaches it maximum of 255, the next pin ( PIN 5) should be switched ON and pin 11 OFF, and so till the end.

I've tried to use SWITCH/CASE instead of writing IF statement and to build SPAGHETTI CODE. My IDEA was to monitor only PWM OUTPUT PIN 11, and once it gets fully opened (255), then I can check which of the other position is OFF and to turn in ON.

But it seems to me that CASE function can only works with INT type of variable.

Is there any other function that someone might've used to control the above mentioned circuit?
If there are better function to use, please let me know!

Hello maxpower1919

Post your sketch, well formated, with comments and in so called code tags "</>" to see how we can help.

Have a nice day and enjoy coding in C++.

EDIT:
Take some time and describe the desired function of the sketch in simple words and this very simple.

Or a visual means, like a flowchart.

Or pseudocode, try it you'll like it,and so will we.

This seems like a good place to employ the IPO model of program construction, I am surprised @paulpaulson didnt say. Or maybe not.

a7

Today we are going to start slowly.

1 Like

I hope you are treating them as simple digital GPIO, then.

IF statement and to build SPAGHETTI CODE

Well, okay, but it's usually complex branching on conditions that produces that.

I see you posting. I hope it's code.

Correct! So between two switches for example 3 and 5, before pin 5 to be switched ON, the Duty cycle of pin 11 should reach 255 and then pin 5 should go HIGH, respectively the duty cycle of 11 is again 0.

You were asked to post code.

Post a timing diagram to see what shall happpens und which conditions.

This is an example of an incompletely specified requirement. It looks like there is some timing involved, like a ramp waveform. Helping will be difficult without a diagrammatic, specific, non-verbal description.

For a system like this, you should really have something like that prepared already for your own use in programming. Even if it's just on the back of an envelope.

Also you have been asked multiple times to post your code.

2 Likes


Still working on the code as it is not so clear and the comments are missing. I am working on it. Please find the diagram!

1 Like

Hello

Try this example without Switch/Case.

/* BLOCK COMMENT
  - https://forum.arduino.cc/t/switch-case-arduino-nano-pwm-control/1072229/10
  - Switch/Case arduino nano PWM control 
  - This sketch may contain traces of C++.
  - In case of indisposition:
  - https://www.learncpp.com/
  - Thanks to LarryD
  - https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
  - Tested @ Arduino: Mega[X] - UNO [ ] - Nano [ ]
*/
#define ProjectName "Switch/Case arduino nano PWM control"
// make names
enum PinNames {Three,Five,Six,Nine,Ten,Eleven};
// make variables
constexpr byte LedPins[] {3,5,6,9,10,11};
// make structures
struct TIMER {
  const unsigned long Interval;
  unsigned long previousMillis; 
};
TIMER pwmTimer {1000,0}; 
void setup()
{
  Serial.begin(115200);
  Serial.print(ProjectName), Serial.print(" in "), Serial.println(__FILE__);
  for (auto LedPin:LedPins) pinMode(LedPin,OUTPUT); 
}
void loop()
{
  unsigned long currentMillis=millis();
  if (currentMillis-pwmTimer.previousMillis >= pwmTimer.Interval) 
  {
    pwmTimer.previousMillis=currentMillis;
    static unsigned int pwmValue=0;
    static int pwmCounter=0;
    analogWrite (LedPins[Eleven],pwmValue++); 
    if (!(pwmValue%256)) pwmCounter++;
    if (pwmCounter==Eleven) pwmCounter=0;
    digitalWrite(LedPins[pwmCounter],HIGH);
  }
}

Have a nice day and enjoy coding in C++.

Thanks @paulpaulson.
When I am changing the analog input value from 0 to 1023, on the output side, the PWM controlled pin 11 and output pin 3 are in HIGH state all the time. That is the result I was able to get while testing the proposed solution.

Hello maxpower1919

Mod the example to your needs.

Thanks for the diagram. It's pretty good, but what is the time scale? For example, how long between pin 5 going high, and pin 6 going high?

Hi @anon57585045,
No need to consider the time at all. It is "P" regulator, so, depending on the input analog value, each position should be turned ON, accordingly, while the analog input increasing. Before next output pin goes from LOW to HIGH, pin 11 should reach the maximum duty cycle ( The intermediate step), for every signle ON/OFF step.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.