1

im a newcomer for arduino and sorry for any grammar error. So what im trying to do is replicating a flickering effect found on spacecraft thruster when they about to take off where the fire bursting from dim to bright with flickering,similiar to afterburner on aircraft model which use neopixel led but this time only using common led. I could mimic the flickering on the led with various brightness level using the simple analogWrite,the problem is i cant really merge them since i use delay which is affecting how fast the flickering effect is and i need 1 sequence of flickering to stay a while before going next to brighter flickering and so on. My code is quite too simple

    Int led = 9;
    void setup () {
    pinMode (led, OUTPUT);
    }

    void loop() {
    {analogWrite (led, random (10,15));
    delay (30);}

    }

Im thinking adding a new line with higher random led states (ex. 50,100) should be okay,but then the delay which affect the flickering speed are too short and making both line merging useless since its will go to the brightest in no time. Is there's any way to hold the first dim line for an amount of time then continue to the next brighter line and so on without affecting the filckering delay? Any help will be appreciated

Thank you!

5
  • 1
    first learn the BlinkWithouDelay example
    – Juraj
    Commented Oct 30, 2018 at 8:36
  • @Juraj thank you for your response juraj,already playing around with milis for a few times,but all its do is holding the line while the other line overlapping it(which is okay for cosmetic purpose,but i didnt want to wasting resource),and its become too complex if i want to address each line with each their own milis
    – user194312
    Commented Oct 30, 2018 at 8:48
  • you have only one LED?
    – Juraj
    Commented Oct 30, 2018 at 8:50
  • @Juraj i have 1 WIRE to distribute this effect from pin 9,i believe i can power as many led as i like with or without the arduino later,so yeah you can say 1 led but not really 😊
    – user194312
    Commented Oct 30, 2018 at 8:55
  • then add second analogWrite and a delay after it
    – Juraj
    Commented Oct 30, 2018 at 9:00

1 Answer 1

1

Before using millis() you can try to create the thruster effect with delay().

I think 40Hz update rate for the led should be enough. The sketch starts with a random value between 5 and 15 and ends with a random value between 80 and 256. The part in the middle that gradually increases between those values is a mathematical problem.

// Led mimic spacecraft thruster.
//
// Update rate 40Hz for the led.
// That is a delay of 25ms.

#define RATE 40  // update rate of led in Hz
const int ledPin = 9;   // it must be a PWM pin !

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

  Serial.begin(9600);

  randomSeed(analogRead(A0));
}

void loop() {
  int n;

  Serial.println("10 seconds low brightness");
  n = 10*RATE;
  for(int i=0; i<n; i++) {
    analogWrite(ledPin, random(5,15));
    delay(1000/RATE);
  }

  Serial.println("5 seconds gradually brighter");
  n = 5*RATE;
  for(int i=0; i<n; i++) {
    //   random start: from 5 to 80     (75 increase)
    //   random end  : from 15 to 256   (241 increase)
    //   The calculation is with long, it might not fit in a 16 bit integer

    int random_start = 5 + int((long(i) * 75) / long(n));
    int random_end   = 15 + int((long(i) * 241) / long(n));
    analogWrite(ledPin, random(random_start,random_end));
    delay(1000/RATE);
  }

  Serial.println("10 second full thrust");
  n = 10*RATE;
  for(int i=0; i<n; i++) {
    analogWrite(ledPin, random(80,256));
    delay(1000/RATE);
  }

  analogWrite(ledPin,0);
  delay(2000);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.