looking for a Fade loop explanation

Good afternoon. I am new to Arduino and am slowly learning how to use it. I am playing with the basic Fade example provided with the software. I understand the the code fades from max to min, then min to max, but why it is in an infinity loop? I am interested in using the the max to min fade function in one of my projects but would like to fully understand the code first.

thanks.

/*

  • Fading*

  • This example shows how to fade an LED using the analogWrite() function.*

  • The circuit:*

    • LED attached from digital pin 9 to ground.*
  • created 1 Nov 2008*

  • by David A. Mellis*

  • modified 30 Aug 2011*

  • by Tom Igoe*

  • This example code is in the public domain.*

  • http://www.arduino.cc/en/Tutorial/Fading*
    */

int ledPin = 9; // LED connected to digital pin 9

void setup() {

  • // nothing happens in setup*
    }

void loop() {

  • // fade in from min to max in increments of 5 points:*

  • for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {*

  • // sets the value (range from 0 to 255):*

  • analogWrite(ledPin, fadeValue);*

  • // wait for 30 milliseconds to see the dimming effect*

  • delay(30);*

  • }*

  • // fade out from max to min in increments of 5 points:*

  • for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {*

  • // sets the value (range from 0 to 255):*

  • analogWrite(ledPin, fadeValue);*

  • // wait for 30 milliseconds to see the dimming effect*

  • delay(30);*

  • }*
    }

... because the loop() function is an "infinity loop".

The way arduino works is that every time the board powers up or is reset, it calls the setup() function one time and then calls the loop() function repeatedly. So every time your fade for() loops end, the loop() function ends and then gets called again. This repeats as long as the board has power.

thanks! so then to not have an infinity loop, one needs to add a counter?

burrism:
thanks! so then to not have an infinity loop, one needs to add a counter?

Generally, to have a program that stops one puts everything in setup() and leaves loop() empty. But I think you're asking how to limit the number of times you wish to cycle. In that case, you're right.

Alright, so based on the Fade loop comments, I have tried to write a counter that includes some nested "IF" statements. The intent is to check if the Turn Signals are on for longer than 90 seconds. If they are, a buzzer will sound until Pin 4 goes LOW. I am simulating the turn signal "on" by applying 5vdc to pin 4 to make it go HIGH. But, obviously, it is not working. I am guessing my understanding of "IF" statements is not accurate or complete.

int turnPin=4; // input from turn signals
int buzzerPin=6; // output to buzzer
int turnDelay=0;

void setup() {
pinMode(turnPin,INPUT); // input from turn signal
pinMode(buzzerPin,OUTPUT); // buzzer output
}
void loop() {
// TURN SIGNAL if on for more than 90 seconds a buzzer will sound
digitalRead(turnPin);
if (turnPin==HIGH) { //if turn signal pin is (+5v), start 90 second counter
for (int turnDelay; turnDelay <90; turnDelay +1);
delay(1000);
if (turnDelay==90) { // buzzer high low sound if counter is at 90
digitalWrite (buzzerPin,HIGH);
delay(500);
digitalWrite (buzzerPin,LOW);
delay(250);
} // end of buzzer sound loop
} // end of TURN SIGNAL code

} //end of all code

Time to take a moment and read the sticky post at the top of the forum and learn how to post your code properly using code tags. It helps people help you...

Your for loop is not correct. You need to increment turnDelay, not add one to it and then throw away the result. If you are not applying 5V to pin 4, it is not connected to anything which is referred to as "floating" which is bad since it can read either value at any time. The better way to it declare that pin as INPUT_PULLUP which means it will get pulled up to 5V internally when not connected to anything and then you ground it when you want it to be active.

You also are not using digitalRead() correctly. It returns the result of reading a pin and you need to assign that to a variable. Since you only want the buzzer to sound if it has been on for 90 seconds, you need to detect the transition of the turn signal (HIGH -> LOW or LOW -> HIGH), not the current state. There is an example in the IDE. File->examples->02.Digital -> StateChangeDetection. There is also an example called BlinkWithoutDelay that shows you how to keep track of elapsed time which is what you should be doing for your 90 second test.

You can also use the serial monitor to help debug your program.

const int turnPin = 4;     // input from turn signals
const int buzzerPin = 6;   // output to buzzer

int previousTurnState;
unsigned long startTime;
const unsigned long duration = 90000; // 90 seconds

void setup() {
  Serial.begin(9600);
  while( !Serial );
  
  pinMode(turnPin, INPUT_PULLUP);    // input from turn signal
  pinMode(buzzerPin, OUTPUT); // buzzer output
  digitalWrite(buzzerPin, LOW);
  previousTurnState = digitalRead(turnPin);
}

void loop() {
  int currentTurnState = digitalRead(turnPin);

  if ( currentTurnState != previousTurnState ) {
    // something has changed, see which transision happened
    if ( currentTurnState == LOW ) {
      // turn signal just got turned on so record time
      startTime = millis();
      Serial.println( "Turn Signal has been activated." );
    }
    else {
      // turn signal got turned off
      startTime = 0;
      Serial.println( "Turn Signal has been turned off." );
    }
    delay(50);  // debounce
  }
  previousTurnState = currentTurnState; // remember for next time through loop

  if ( startTime > 0 && millis() - startTime >= duration ) {
    // TURN SIGNAL if on for more than 90 seconds a buzzer will sound

    digitalWrite (buzzerPin, HIGH);
    delay(500);
    digitalWrite (buzzerPin, LOW);
    delay(250);
  }
}

blh64 - Thanks so much for the explanations (and sorry for the poor code posting - I will do better next time).

I see you completely removed the FOR loop in your sample. I guess I don't really understand how they work. I thought I could take an integer, look at it, if it is less than 90, add one to it and move on. I tried to apply the analog Fade example code to the buzzer function. I will try to write some more IF loops to get an LED to fade from max to min one time to help me understand it better. Either way, I will have to read more on that function.

the "!=" threw me for a loop for a bit, but I read up on it. I guess not equal is a variation of less than.

I thought about the "floating" Pin 4 but (clearly) didn't do anything about it. thanks for the PULL_UP suggestion.

The digitalRead() error was just bad coding.

And finally, I just learned how to use the serial monitor!

Thanks again!