SUPER basic help, just written wrong somehow....

Hello, starting with Arduino for the 5th time....
I'm trying to control an LED, to go from Pulsing, to Fully lit when a button is pressed (Motion detected actually)

These are super basic things, so I'm wondering if I'm using the wrong if/while/for thing....
All code snippets work as intended separately, they're JUST the examples for godssake!

void loop() {
int buttonState = digitalRead(sensorPin);  //it isn't even checking or it would write the LED as SOMETHING
if (buttonState = LOW) {
  
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin, fadeValue);delay(30);}
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(ledPin, fadeValue);delay(30);}
}
    else {    analogWrite(ledPin, 255); }
}

Where is your setup() ?

Buttonstate == low, maybe

TKall:
Buttonstate == low, maybe

UGH, and that's why I come here, should I delete thread after solved?

Love ya man! :stuck_out_tongue:

You're welcome. Don't delete it, it may help someone else.

one more thing?

if/else only seems to check after both for loops execute....

What do I use if I want it to check constantly if HIGH/LOW?

(Elegantly/properly I mean; I could just shove checks into the for loops :stuck_out_tongue:

If I understand your question, you will probably need to make a few changes to your code. Take a look at a "state machine." There is an excellent discussion currently taking place here: 64 yr old newby would like some help - Project Guidance - Arduino Forum

That seems a bit much....
I feel like I just dont get the syntaxes well,

doesn't a While loop monitor the initial statement the entire time?

(Vs if/else, which apparently only checks at the beginning unless the checks are inside the other loops?)

No. the way you have it written the for loops execute completely, one after the other, then control is passed back to the top. Those delays block code execution. Look at the Blink Without Delay example in the IDE.
You could probably use interrupts as well but that is a more advanced concept.

That seems a bit much....

But, it is well worth the effort to understand what code is blocking, and how to not write blocking code.

Think about what you would need to keep track of, if you wanted to change the intensity of an LED once a week, instead of every 30 milliseconds.

While you can stick your head in the sand for 30 milliseconds, your spouse will not like you standing there with your thumb up your (place where the sun don't shine) for a week at a time.

So, you write down the last time you made a change. Periodically, you see if it is time to make another change. The blink without delay example illustrates this concept.

Now, what change do you need to make, once a week? Well, the light level needs to go up or down. Which way does it need to go? By having a variable that holds the change amount, set to +n or -n, you simply add the amount in the variable to the current amount, and apply the new value to the pin. You test whether the new value is in range, first, of course. If it is, apply it. If not, you need to change the value in the change amount variable, from +n to -n, or from -n to +n. That, of course, just means multiplying by -1, regardless of whether the value is currently positive or is currently negative. The range limits are, obviously, 0 and 255.

Make some attempt to write the code using these ideas. If it works, great. If not, post your attempt, and describe what the code actually does. You'll find that it is not really that difficult.

Jack777:
doesn't a While loop monitor the initial statement the entire time?

No, it only checks at the beginning of each iteration.