Keep PWM signal in loop with other things

Hi, i'm working on a project with my car and currently i'm faceing a little problem:
In the loop i have multiple things, for example, scan for a RFID card, check a vibration sensor and i also want to keep an PWM signal without any interuptions depending on a button:

void loop()
{
  delay(100);
  // BEGIN OTHERS
  if (analogRead(usaPin) == HIGH) {
    analogWrite(usaoutPin, 120); //about 50% PWM
  }
  else
  {
    analogWrite(usaoutPin, 0);
  }

where usaPin will be a toggler with a resistence and usaoutPin will be the output

i'm also thinking of set this analogWrite from the setup and using the toggler to connect it to my lights (using a capacitor)

What do you think, excepting my possible solution ?

PWM will continue to "run" until it's stopped so there's no reason to re-start it every time through the loop.

You might want to set-up an AND condition with a flag that keeps track of if the PWM is running or not. That way you can start PWM only when your usaPin is high and PWM isn't already running.

There is a very small chance that analogRead() returns HIGH. analogRead() returns a value between 0 and 1023 (both included). HIGH is defined as 1; so only if analogRead() returns 1, you will set the PWM output to 120.

I do not understand what you try to achieve with a capacitor? I also do not quite understand 'toggler with resistance'.

Maybe you can draw a little schematic for both scenarios (photo/scan of hand-drawn one will do).

Tnx for your respond, do you mean like this:

int usaStatus = 0;
int ledStatus = 0;

void loop()
{
  delay(100);
  // BEGIN usalights
  if (analogRead(usaPin) == HIGH) {
    if (usaStatus = 0) {
      analogWrite(usaoutPin, map(usalight, 0, 100, 0, 255)); //set PWM of usa lights
    }
    else
    {
      analogWrite(usaoutPin, 0);
    }
  }
  else
  {
    usaStatus = 0;
  }

and by toggler with resistence i mean a pull up res to track the toggler (that will be on an analog pin because i have no more space for them)

Analog pins can be used as digital pins; just use digitalRead().

And I do not understand the rest of the code; you use a variable usaStatus whose value never changes. If you want to detect a statechange, have a look at the statechange dectection example that comes with the IDE.

And why the single '=' in * if (usaStatus = 0) {* ?

See the StateChangeDetection example, how to react on button state changes only. Whenever the button state changes, start or stop the PWM output.

I currently builded this:

and now looking into State Change detection

int usaStatus = 0;

void loop()
{
  delay(100);
  if (digitalRead(usaPin) == HIGH) { //if toggler is ON
    if (usaStatus == 0) {
      analogWrite(usaoutPin, map(usalight, 0, 100, 0, 255)); //set PWM of usa lights
      usaStatus = 1;
    }
  }
  else //if toggler is off
  {
    if (usaStatus == 1) {
      analogWrite(usaoutPin, 0); //set off usa lights
      usaStatus = 0;
    }
  }

You got it :slight_smile:

Eventually replace the delay() by non-blocking debounce code.