Help please w.r.t <Do - While> instruction

I am using the Fade routine supplied with the basic IDE.
I want to perform the <fade in - fade out> one time only.
I use a variable x preset to 0
At the end of the fade sequence I increment x by making x=1
the While condition is (x=0)
My problem is it continues to loop endlessly.
Any help really appreciated as I have looked at endless tutorials and resources online and still can't make it work.
Many thanks in advance.

declare variables
|
v
|
v

int x = 0; //set up for 1 loop only

// the setup routine runs once:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {

do {
// set the brightness of pin 9:
analogWrite(led, brightness);

// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
x = x + 1;}
while (x = 0);
}

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

So it runs over and over… that's what loop() does.

a7

while (x = 0);

Whoops !
That assigns the value of 0 to x rather than testing the value of x

Haha, that too. :expressionless:

a7

I based my code on:

do...while

[Control Structure] example

int x = 0;
do {
delay(50); // wait for sensors to stabilize
x = readSensors(); // check the sensors
} while (x < 100);

instead of x being a variable measured from a sensor input i want it to just be a 'counter'. The 'while' command seems to be testing the value rather than assigning it. I'd sure appreciate your help. Thanks.

If you call the fade routine in setup() it will run once.

Thanks for that. I like that option BUT depending on other conditions, I'd like it to perform 'fade' again... once.

Is this about not knowing how to compose a function or not knowing how to use do_while (or both)?

Thanks RP. I want to perform the routine 'Fade' once. Then say if I press a button, it runs again, once. Waits for the button press, and runs again, once... etc

Because

  • do-while loops always execute the body code once

and

  • while (x = 0) is always false (you meant x == 0, we are sure!)

essentially you are getting one step of the fade each time loop() is called.

loop() is called over and over forever, so you are seeing fade up and down over and over.

If you want ONE fade up and down, you have to let the fade run for 512 steps, then stop it.

This is equal to what you posted, functionally:

# define led	13

int brightness;
int fadeAmount = 1;

void setup() {

  pinMode(led, OUTPUT);
}


void loop() {

    analogWrite(led, brightness);

    brightness = brightness + fadeAmount;
    
    if (brightness <= 0 || brightness >= 255) {
      fadeAmount = -fadeAmount;
    }

    delay(30);
}

With some assumptions.

In the future, don't cut off parts of you program and make us guess at the variables, their types and initial values and where you may have wired an LED.

a7

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

byte xx = 0;

void setup()
{
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() 
{
  if(xx == 0)
  {
    goFade();
    xx = 1;
  }
}

void goFade ()
{
  analogWrite(led, brightness);
  brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  delay(30);
}

okay... i follow that... but how do i kick it off? say i want it to start if an analog input greater than 500 triggers it? many thanks...

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

int kickoff;

void setup()
{
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() 
{
  kickoff = analogRead(A0);
  if(kickoff >= 500)
  {
    goFade();
  }
}

void goFade ()
{
  analogWrite(led, brightness);
  brightness = brightness + fadeAmount;
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  delay(30);
}

@runaway_pancake your goFade takes exactly one step.

Your control logic makes it run continuously up and down by calling it as long as the threshold is met.

Since you are writing the code, make it go the right number of steps, then stop until triggered again.

a7

My go-to trigger is Serial. Then I can code for user text commands.

I didn't look that closely at it really.
This fades down - once

int led = 9;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
byte xx = 0;

void setup()
{
  pinMode(led, OUTPUT);
  Serial.begin(19200);
}

void loop() 
{
  if(xx == 0)
  {

    goFade();
    xx = 1;
  }
}

void goFade ()
{ 
  Serial.println("Fading");
  brightness = 255;
  do
  {
    analogWrite(led, brightness);
    delay(10);
    brightness = brightness -2;
  }while(brightness > 4);
  digitalWrite(led,LOW);
}

OMG... I seem to be making a pigs ear out of this. Maybe if I explain exactly what I am trying to do, you guys will be more in a position to help me.

i have a chook house. My chooks are as blind as bats when it's dark. The coop is small and it's not really feasible to have windows. As soon as the chooks march into the coop they cannot really get themselves organised cos it's darker in the coop than outside... so they end up all over the place... whereas they'd really like to be all huddled together on the perch. I usually end up having to open the coop and manually place them on the perch. SO... what I want to do is as follows:

  1. When it is dark enough for the chooks to go inside I want the light to come on. LDR connected to A2 - (input range is 0-1023 I believe). Say when (A2 input <200)
  2. I want the light initially to be fully on (255). Say Pin 9.
  3. I want to fade the light over about 30 minutes (once) until the output is off - (0)
  4. I do not want it to do anything again until the morning.
  5. In the morning it will acknowledge that it's getting light again, say when (A2 input >250) - still it should not do anything.
  6. Upon the sun going down, and A2 input <200 the program should repeat itself from step (1)

I feel very frustrated that I cannot work this out for myself... but there you go. I sure appreciate your help. Many thanks
PS I really want to learn coding the Arduino... and feel sure if you can give me some help with this, it will enable me to learn more quickly. FWIW I now appreciate the REAL meaning of 'hacking'.

There are many finished chook house projects in the forum already. There are differences between them and most open and close the door. You might be able to find the persons and ask how it's worked in the long term.

Not at all. I think it was clear what you were asking to accomplish with your small test program, and it is good idea for you to get the fading working with just such a small program.

Your prose description is useful and does make it unambiguous.

I think your test program will

  • fade up when a button becomes pressed.
  • fade down when another or the same button becomes pressed.

A simple fade up (or down) over thirty minutes will blind your program to additional or changed inputs that would inform alternate activity.

This may not be a bad thing! If the goal is to fade up, and you don't care if, say, the sun goes behind a load, a simple fading routine that takes all the steps before being able to even realize the sun has gone away will do fine.

Similarly for fade down.

If you need to be able to interrupt the fading sequence(s) due to chaining conditions, it gets a wee bit more complicated.

Right now, @GoForSmoke has written something that could be expanded a bit, even naively by using it to create two functions, fadeUp and fadeDown.

And responding once to a button only when it becomes pressed is fairly straightforward.

One thing I see in some code somewhere above is the possibility that the lights will go full on instantly, let us be sure the anything yo deploy is very clearly not gonna do that.

So first, will blind "Imma finish this" dedicated fading work? If so, or even if not, maybe starting with such (in isolation! as you are doing) would be instructive.

Meanwhile, take a look at the basic examples in the IDE. In particular, button and debounce under the digital submenu of the example menu item of the File menu. And maybe state change detection.

edit: I see you do want the light to just go on, so never mind about that, and there’s no fade up that I see, but I’ve been having trouble seeing lately, so.

You may find that an LDR alone may not be enough to assure the operations you want. It would not be at all difficult to plan that your circuit and program include an RTC module. They are cheap and easy to work with, this would let you do sunrise and sunset by time perhaps also assisted by ambient light conditions.

But maybe first things first.

a7