Skip to main content
Try redoing the indentation again now that the question has been migrated to Arduino Stack Exchange
Source Link
per1234
  • 4.3k
  • 2
  • 24
  • 44

However, the loop keep repeating.

That's the whole point of loop(). If you want some code to run only once after the program starts put it in setup():

int motor = 9;
int fadeValue = 5;

void setup() {
  pinMode(motor, OUTPUT);
  
  for (int fadeValue = 5 ; fadeValue <= 245; fadeValue += 10) {
    analogWrite(motor, fadeValue);
    delay(100);
    if (fadeValue == 245)
    {
      fadeValue = 255;
      break;
    }
  }
}

void loop() {
}

However, the loop keep repeating.

That's the whole point of loop(). If you want some code to run only once after the program starts put it in setup():

int motor = 9;
int fadeValue = 5;

void setup() {
  pinMode(motor, OUTPUT);
  
  for (int fadeValue = 5 ; fadeValue <= 245; fadeValue += 10) {
    analogWrite(motor, fadeValue);
    delay(100);
    if (fadeValue == 245)
    {
      fadeValue = 255;
      break;
    }
  }
}

void loop() {
}

However, the loop keep repeating.

That's the whole point of loop(). If you want some code to run only once after the program starts put it in setup():

int motor = 9;
int fadeValue = 5;

void setup() {
  pinMode(motor, OUTPUT);

  for (int fadeValue = 5 ; fadeValue <= 245; fadeValue += 10) {
    analogWrite(motor, fadeValue);
    delay(100);
    if (fadeValue == 245)
    {
      fadeValue = 255;
      break;
    }
  }
}

void loop() {
}
Post Migrated Here from electronics.stackexchange.com (revisions)
Source Link
per1234
  • 4.3k
  • 2
  • 24
  • 44

However, the loop keep repeating.

That's the whole point of loop(). If you want some code to run only once after the program starts put it in setup():

int motor = 9;
int fadeValue = 5;

void setup() {
  pinMode(motor, OUTPUT);
  
  for (int fadeValue = 5 ; fadeValue <= 245; fadeValue += 10) {
    analogWrite(motor, fadeValue);
    delay(100);
    if (fadeValue == 245)
    {
      fadeValue = 255;
      break;
    }
  }
}

void loop() {
}