Pages: [1]   Go Down
Author Topic: need more a life effect...  (Read 450 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello all!

I'm working on a project called star field project

What do I have
arduino uno
bright green, blue and white LED light, each with a resistor of 180ohm
a 16x2 LCD with backpack from Adafruit
and finally, fiberglass...

I have made a code, so that the LEDs fade random soft, sometimes hard that totalle working and awesome.
My first problem was that the outputs same steps had. exmp. green, blue, white, green, blue, white, ... and go on.

Code:
void loop() {
  schermpje(); //lcd screen
 bluelight(); // blue
 witlight(); //white
 groenlight(); //green
}

So I called help on you, and change that stiff to :

Code:
int pick( int a, int b)
{
   return random(2) ? a : b ;
}

 
  void loop() {
  static int next;

  schermpje();

  switch (next) {
   case 0: witlight();   next= pick( 1,2);  break;
   case 1: bluelight();  next= pick( 0,2);  break;
   case 2: groenlight(); next= pick( 0,1);  break;
   }

Thats cool and ok, but not realistic, the led's is still waiting on the othere led's
I like as the white LED is busy in hes fading, then the other mag also begin to fade, really random faden and not wait until the other has finished fading.

but would this mean that I have to go change my code or just in my loop? and how do I accomplish this?

this is the whole code :

Code:
#include <Wire.h>
#include <LiquidTWI.h>
LiquidTWI lcd(0); // Connect via i2c, standaard adres #0 (A0-A2 not jumpered)

int ledPin = 9;  //Selecteren van pin
int witledPin = 10;  //Selecteren van pin 10
int groenled = 11;  //Selecteren van pin 11
int bl_rand_nr ; // getal waarde helder blauw
int wit_rand_nr ; // getal waarde helder wit
int gr_rand_nr ; // getal waarde helder groen
int gr_rand_nr2;
; // getal waarde helder groen
void setup(){ 
 Serial.begin(9600);
 
  lcd.begin(16, 2);// set up the LCD's number of rows and columns:
}

void bluelight(){
bl_rand_nr = random(0, 255);
for(int fadeValue = 200 ; fadeValue <= 255; fadeValue +=5) {
analogWrite(ledPin, fadeValue);
delay(10);  }

for(int fadeValue = 255 ; fadeValue >= (bl_rand_nr); fadeValue -=20) {
analogWrite(ledPin, fadeValue);
delay(100);  }
}

void witlight(){
wit_rand_nr = random(0, 255);
for(int witfadeValue = 200 ; witfadeValue <= 255; witfadeValue +=5) {
analogWrite(witledPin, witfadeValue);
delay(10);  }

for(int witfadeValue = 255 ; witfadeValue >= (wit_rand_nr); witfadeValue -=20) {
analogWrite(witledPin, witfadeValue);
delay(100);  }
}

void groenlight(){
gr_rand_nr = random(0, 255);
gr_rand_nr2 = random(50, 150);
for(int groen = (gr_rand_nr2) ; groen <= 210; groen +=5) {
analogWrite(groenled, groen);
delay(10);  }

for(int groen = 255 ; groen >= (gr_rand_nr); groen -=20) {
analogWrite(groenled, groen);
delay(100);  }
}




void schermpje(){
  lcd.clear();           //Eerst opkuisen
  lcd.setCursor(0, 0);   //plaatsen van cursor
  lcd.print("HB:");      //geen BH maar HB van HelderBlauw
  lcd.setCursor(4,0);    //plaatsen van cursor voor de getallen
  lcd.print(bl_rand_nr); //en print het getal maar!

  lcd.setCursor(0, 1);
  lcd.print("HW:");
  lcd.setCursor(4,1);
  lcd.print(wit_rand_nr);

  lcd.setCursor(9, 0);
  lcd.print("HGR:");
  lcd.setCursor(13,0);
  lcd.print(gr_rand_nr);

}

int pick( int a, int b)
{
   return random(2) ? a : b ;
}

 
  void loop() {
  static int next;

  schermpje();

  switch (next) {
   case 0: witlight();   next= pick( 1,2);  break;
   case 1: bluelight();  next= pick( 0,2);  break;
   case 2: groenlight(); next= pick( 0,1);  break;
   }

}
 
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want the LEDs to face concurrently, the preferred approach is to change the structure of your sketch to use a non-blocking design. That means that instead of stopping and waiting for something to happen (in this case, the current LED has been faded) the sketch will just chevck whether anything needs to be done (it is time to reduce the brightness of this LED). The Blink without delay example sketch shows how to use this approach to turn a simple LED on and off. I suggest you use a similar approach to control changes to the brightness of LEDs at regular intervals so that they slowly fade.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@warpie,

You will find what you need to learn here: http://gammon.com.au/blink.

Especially the example(s) of fading leds.

Cheers,
John
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello John, thx man!

It's very intresting!

But some thing that i try todo i not working...

LedFader navigation (10, 10, 200,   500);
LedFader navigation (pin, min, max, millis);

I like in the value of min an random nr between 2 value's also for the max

like this :

Code:
#include <LedFader.h>
int gr_rand_nr1;
int gr_rand_nr2;


// set up some LEDs
//                 pin  min max  millis
LedFader strobe     (9, gr_rand_nr1, gr_rand_nr2,  100);

void setup()
  {     
  strobe.begin ();

  }  // end of setup

void loop()
  {
    gr_rand_nr1 = random(10, 100);
    gr_rand_nr2 = random(200, 255);
  // update lights
  strobe.update ();

 
  // do other useful stuff here ...
 
  }  // end of loop

but that dont works....
must i change something in that Library? Or do something else?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well i was trying to change the library

That we have this: LedFader strobe     (9, 10,100,200,255 ,  100);     // LedFader strobe     (pin, min of min , max of min, min of max,max of max ,  millis);



So this is the code that is changed :
Quote
#include <LedFader.h>
int minValue_;
int maxValue_;
// constructor
LedFader::LedFader (const byte pin,
                    const byte minValueMin,
               const byte minValueMax,
                    const byte maxValueMin,
               const byte maxValueMax,
                    const unsigned long msPerCycle,
                    const bool active,
                    const bool stopWhenOn) :
         pin_ (pin), minValueMin_ (minValueMin), minValueMax_ (minValueMax), maxValueMin_ (maxValueMin),  maxValueMax_ (maxValueMax), msPerCycle_ (msPerCycle)

   {
   startTime_ = 0;
   active_ = active;
   stopWhenOn_ = stopWhenOn;
   forwards_ = true;
   }  // end of  LedFader::LedFader


// set pin to output, get current time
void LedFader::begin ()
  {
  pinMode (pin_, OUTPUT);
  digitalWrite (pin_, LOW);
  startTime_ = millis ();  
  }  // end of LedFader::begin
  
// call from loop to flash the LED
void LedFader::update ()
  {
  // do nothing if not active
  if (!active_)
    return;
    
  unsigned long now = millis ();
  unsigned long howFarDone = now - startTime_;
  if (howFarDone >= msPerCycle_)
    {
    forwards_ = !forwards_;  // change direction
    if (forwards_)
      analogWrite (pin_,  minValue_  = random(minValueMin_, minValueMax_));
    else
      analogWrite (pin_,  maxValue_ = random(maxValueMin_, maxValueMax_));
    startTime_ = now;
    
    // stop when at required brightness?
    if (stopWhenOn_)
      active_ = false;
    }  // end of overshot time for this cycle
  else
    {
    unsigned long newValue;
    byte valDifference = maxValue_ - minValue_;
  
    if (forwards_)
      newValue = (howFarDone * valDifference) / msPerCycle_;  
    else
      newValue = ((msPerCycle_ - howFarDone) * valDifference) / msPerCycle_;  
    
    analogWrite (pin_, newValue + minValue_);
      
    }  // end of still in same cycle
    
  
  } // end of LedFader::update
 
 // activate this LED
 void LedFader::on ()
   {
   active_ = true;
   startTime_ = millis ();  
   forwards_ = true;
   }  // end of LedFader::on

 // deactivate this LED
 void LedFader::off ()
   {
   active_ = false;
   digitalWrite(pin_, LOW);
   }  // end of LedFader::off
  
 // is it active?
 bool LedFader::isOn () const
   {
   return active_;
   }  // end of LedFader::isOn

But it not equel like my othere code, i do something wrong or over see stuff....

This that othere code that i like to make a library with combine millis

Quote
int ledPin = 9;
int randNumber;
void setup() {}
void loop() {
randNumber = random(0, 255);
for(int fadeValue = 200 ; fadeValue <= 255; fadeValue +=5) { // als het fadevalue kleinder is dan 255, dan zal hij de waarde "fadevalue" verhogen met 5
analogWrite(ledPin, fadeValue); // analoog geschreven(uitgang, met de aangekoppelde fadevalue waarde )
delay(10); // tijds vertraaging van 1Seconde
}

for(int fadeValue = 255 ; fadeValue >= (randNumber); fadeValue -=20) {
analogWrite(ledPin, fadeValue);
delay(100);
}
}

It's lot easy with that library, than i can build the code program like this :
Quote
#include <LedFader.h>

// set up some LEDs
//                 pin  min max  millis
LedFader strobe     (9, 10,100,200,255 ,  1000); // pin, min of min, max of min, min of max, max of max, millis


void setup()
  {     
  strobe.begin ();

  }  // end of setup

void loop()
  {
  // update lights
  strobe.update ();
 
  // do other useful stuff here ...
 
  }  // end of loop
« Last Edit: February 23, 2013, 09:11:17 pm by warpie » Logged

Pages: [1]   Go Up
Jump to: