Pages: 1 ... 13 14 [15] 16 17 18   Go Down
Author Topic: Newbie needs assistance  (Read 16604 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I would seriously look at a state machine for the sequence you described (not sure on which page). Each next state (ie. turn on one more light) can be handled in a switch statement / function call, so that it gradually progresses through what you want. As well, each state could say how long to wait until the next state change.
Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 177
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will make 2 quick videos so you can see whats going on. saves people wiring up their boards and leds.

Your right about throwing out code with lots of delays, my first code was heavily based on that and was thrown away because of it.

I think the new code should do what i want it to once i get the delay removed.

Its not just one led thats doing a job, its actually 13 led doing different stuff. The code was constructed from "pieces" that controlled each function. i.e

got the navigation lights to blink when they should
got the strobe light to strobe when it should

Although that was taken from your example.

The fade sequence was put together and combined with the strobe and nav code.

then the chaser code was assembled and all the code put together.

The code you posted a few pages back i will need to take a new look at. Theres been so many replies and so on i cant remember what it was for exactly smiley.

but for now, heres the video of the lights working WITH delay()

http://www.youtube.com/watch?v=t3xeKOTS2ec&feature=youtu.be

and without

http://www.youtube.com/watch?v=3RsIyI0kzps&feature=youtu.be

the fade is supposed to work with the chaser lights not flashing violently.

the only 2 "blinking" leds are the white and red on the far right. (which always work perfectly i might add)

Hope this clears up any confusion on whats meant to be happening and where (when is questionable right now other than the fade/flashing problem haha)

as for what the enterprise looks like, its the polar lights, round 2 1/350 scale refit model haha. loads of work but want it to be as accurate as possible once the codes laid out.

state change - I did work on something like that i think, but was apparently useless. and honestly, i've got alot further with this code than that previously with fewer problems in this round. so far that is.

I was going to add each stage of the lights sequence consecutively one after another and try to get something like this.



hope that makes sense to you.

So i have that in mind everytime i move forwards but I just feel very much stuck getting this anomaly between with delay() and without() sorted. Mostly because i cant ideally move forwards until its resolved.

I know the fade code may need to be changed but i was hoping to get it to work with what i have. I did ask about how to add more pins to the "fading" example and how to add it in but i think it got brushed over. mostly because the "fading" example has alot more control over the variables. but i couldnt get more pins added to it letalone incorporate it into what i have now. Also, it has more "delays()" in the fading example. so even if i did manage to get more pins added to it and add it into the core code, i'd still be stuck here so i figured, get this hurdle over with then move along and look at the fading example again later.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you need to, don't feel too bad about throwing code away. I do that at times, and I've been programming for years.

Sometimes it's better to start from scratch, than trying to "rescue" bad code.

I think it's called the "prior investment fallacy". For example, from: http://www.wisegeek.com/what-is-a-sunk-cost-fallacy.htm

Quote
The sunk cost fallacy is a logical fallacy or flawed argument for decision-making. In the sunk cost fallacy, prior investment is cited as a reason for pursuing a course of action. The term is usually used to describe persisting in a bad investment, on the grounds that otherwise, the time, money or effort invested in the project will be wasted anyway. The expression "throwing good money after bad" is sometimes used to describe this behavior.

The fallacy is that it is better to spend days trying to improve bad code, rather than hours rewriting from scratch. Which obviously, it isn't.
Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 177
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i understand.

It's just very disheartening and extremely fustrating starting over. I've started over 4 times and now im on my 5th. Which is why i was hoping to use what i have now still.

once the startup sequence is done the majority of variables will be locked in and only a few changes made for different stages. mostly being the fade levels on the pins, some fade on while others off at some points others merely come up to full brightness and back down again later.

but again, even if i did change the code, i would still be coming up against this issue with the delay() i should imagine as i dont have the skills to write a code for the whole thing that would allow the odd use of it.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What's your current code? Because I don't understand this:

Code:
long waitUntillPrimary = 0;
...
 
   if (millis() >= waitUntillPrimary) {
      Primarystate = !(Primarystate);
   }


You never change waitUntillPrimary so what is the point of the test?
Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 177
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What's your current code? Because I don't understand this:

Code:
long waitUntillPrimary = 0;
...
 
   if (millis() >= waitUntillPrimary) {
      Primarystate = !(Primarystate);
   }


You never change waitUntillPrimary so what is the point of the test?

hmm I'm not entirely sure, it was taken from an example and expanded upon, the pin assignments changed to the names i've been using (helps me keep track of whats what) and their relating phrases changed to match.

here is the complete code anyway,

Code:
void setup(){
  setupChaser();
  setupFade();
  setupBlink();
}
void loop(){
  loopChaser();
  loopFade();
  loopBlink();
}

long sequenceDelay = 1000;
boolean Primarystate = false;
boolean Secondarystate = false;
boolean Shuttlebaystate = false;
boolean Floodlightstate = false;
long waitUntilShuttlebay = sequenceDelay*2;
long waitUntilFloodlight = sequenceDelay*3;
long waitUntillPrimary = 0;
long waitUntillSecondary = sequenceDelay;
//definitions for fade
  int torpedo = 10;
  int impulse = 6;
  int nacells = 5;
  int deflector = 3;
  int brightness = 100;
  int fadeAmount = 5;
//definitions for blink and strobe
  const byte strobe =  13;
  const byte navigation = 12;
  byte strobeState = LOW;
  byte navigationState = LOW;
  unsigned long previousMillis_1 = 0;
  unsigned long previousMillis_2 = 0;
  unsigned long nextInterval_1;
  unsigned long nextInterval_2;

void setupChaser() {
   pinMode(1, OUTPUT);
   pinMode(2, OUTPUT);
   pinMode(8, OUTPUT);
   pinMode(4, OUTPUT);
}
void loopChaser() {
   digitalWrite(1, Primarystate);
   digitalWrite(2, Secondarystate);
   digitalWrite(4, Shuttlebaystate);
   digitalWrite(8, Floodlightstate);
 
   if (millis() >= waitUntillPrimary) {
      Primarystate = !(Primarystate);
   }
   if (millis() >= waitUntillSecondary) {
      Secondarystate = !(Secondarystate);
   }
   if (millis() >= waitUntilShuttlebay) {
      Shuttlebaystate = !(Shuttlebaystate);
   }
   if (millis() >= waitUntilFloodlight) {
      Floodlightstate = !(Floodlightstate);
   }
}
void setupFade()  {
  pinMode(torpedo, OUTPUT);
  pinMode(impulse, OUTPUT);
  pinMode(deflector, OUTPUT);
  pinMode(nacells, OUTPUT);
}
void loopFade()  {
  analogWrite(torpedo, brightness);   
  brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }     
 analogWrite(nacells, brightness);
brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255){
    fadeAmount = -fadeAmount ;
  }
  analogWrite(impulse, brightness);
brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255){
    fadeAmount = -fadeAmount ;
  }
  analogWrite(deflector, brightness);
brightness = brightness + fadeAmount;
  if (brightness == 0 || brightness == 255){
    fadeAmount = -fadeAmount ;
  }
delay(10);
delay(10);
delay(10);
delay(10);
delay(10);
delay(10);
}
void setupBlink()
  {
  pinMode(strobe, OUTPUT);   
  nextInterval_1 = 3500; 
  pinMode(navigation, OUTPUT);
  nextInterval_2 = 2000;
  }  // end of setup
void loopBlink()  {
  unsigned long currentMillis_1 = millis();
  if (currentMillis_1 - previousMillis_1 > nextInterval_1)
    {
    previousMillis_1 = currentMillis_1;   
    if (strobeState == LOW)
      {
      strobeState = HIGH;
      nextInterval_1 = 70;
      }
    else
      {
      strobeState = LOW;
      nextInterval_1 = 8500;
      }
    digitalWrite(strobe, strobeState);
    }  // end if time up
  {
  unsigned long currentMillis_2 = millis();
  if (currentMillis_2 - previousMillis_2 > nextInterval_2)
    {
    previousMillis_2 = currentMillis_2;   
    if (navigationState == LOW)
      {
      navigationState = HIGH;
      nextInterval_2 = 500;
      }
    else
      {
      navigationState = LOW;
      nextInterval_2 = 3000;
      }
    digitalWrite(navigation, navigationState);
    }
   }
  }

thats everything i have and that code is what is operating the leds in the first video and the same, but without the delay() in the second video.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 177
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ah, that primarystate is related to the timings of various leds actually turning off.

There was code in there to turn them off, but it was removed due to it being redundant and not needed anymore. so it is probably tied into that. but i cant find the original code i used.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 177
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay, I've been googling around and simply cannot for the life of me, find a tutorial or example of fading leds (or cross fading) that does not use the delay() command.

If anyone knows of such a tutorial or something, can they give me a link to it please?

I know it has to do with the blink without delay example but I simply cannot work it out. I have 6 leds in 3 pairs that all fade at the same rate, but they have to fade at different times to each other. I only have 6 because i got 1 fading then added the other 5 to the code just to see if it would work. Anyway, I havent really found a good explanation of the blink without delay example so i dont understand it properly, but vaigly. But i still cant make the connection between using it and cross fading leds yet. so a tutorial on fading / cross fading without delay() would be fantastic.

I tried this:

Code:
// constants won't change. Used here to
// set pin numbers:
const int ledPin =  9;      // the number of the LED pin
int brightness = 0;
int fadeAmount = 5;

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
  analogWrite(ledPin, brightness);
  
  brightness = brightness + fadeAmount;
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  

    // if the LED is off turn it on and vice-versa:
    if (brightness == 0 || brightness == 255)
      fadeAmount = -fadeAmount ;
    else
      (brightness == 0 || brightness == 255);
      fadeAmount = -fadeAmount ;

    // set the LED with the ledState of the variable:
    long interval = 1000;
  }
}

But the led is just on, although its flickering ever so slightly, like its fading WAY to fast.
« Last Edit: December 26, 2012, 10:50:28 pm by secretreeve » Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// if the LED is off turn it on and vice-versa:
    if (brightness == 0 || brightness == 255)
      fadeAmount = -fadeAmount ;
    else
      (brightness == 0 || brightness == 255);
      fadeAmount = -fadeAmount ;

The last line is NOT executed as part of the "else" clause.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 177
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay, well i was just faffing around with it, as you do.

came up with this

Code:
/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to
// set pin numbers:
const int ledPin =  9;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 60;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
  analogWrite (ledPin, brightness);
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = brightness = brightness + fadeAmount;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

its fading it ON, returns to off (as expected) then fades on again. so i think im getting the hang of it.....am i?
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess after 15 pages somebody has already told you fading is done via analogWrite()...
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 177
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, changed the last line to analogWrite.

the only thing with the above code is i cant work out how to reverse it in the else statement.

This is quite a good example of why i asked ifthere is a good example or tutorial on this. I'm darned sure this is the MOST backwards way of doing it.

SERIOUSLY?! YOU HAVE GOT TO BE.....FUUUUUU!

i still have to change the delay() but I resolved the violet flashing by changing a statement to true....go figure.

a little less annoying now so maybe i can find that example/tutorial on fading/cross fading without delay() as they all seem to use it.
« Last Edit: December 26, 2012, 11:36:29 pm by secretreeve » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In the interests of helping anyone who might be wondering how to fade/flash LEDs without using delay, I've done a bit more work on this.

The "LED flasher" class is now a library, you can get it here:

http://gammon.com.au/Arduino/LED_flasher.zip

Install that in the usual way (unzip into your libraries folder).

I made a similar class to fade up and down LEDs connected to the PWM pins, here:

http://gammon.com.au/Arduino/LED_fader.zip

Install that in the usual way.

This code demonstrates flashing some LEDs:

Code:
// Example of flashing multiple LEDs at different rates without delays
// Author: Nick Gammon
// Date: 23 December 2012

#include <LedFlasher.h>

// set up some LEDs
LedFlasher floodLight (8, 200, 300);
LedFlasher shuttleBayDoors (9, 300, 600);
LedFlasher impuleEngine (10, 900, 100);
LedFlasher strobe (11, 500, 1000);
LedFlasher navigation (12, 1000, 2000);
LedFlasher torpedoes (13, 250, 500);

void setup()
  {      
  floodLight.begin ();
  shuttleBayDoors.begin ();
  impuleEngine.begin ();
  strobe.begin ();
  navigation.begin ();
  torpedoes.begin ();
  }  // end of setup

void loop()
  {
  // update lights
  floodLight.update ();
  shuttleBayDoors.update ();
  impuleEngine.update ();
  strobe.update ();
  navigation.update ();
  torpedoes.update ();
  
  
  // do other useful stuff here ...
 
  }  // end of loop

This code illustrates fading some LEDs:

Code:
// Example of fading multiple LEDs at different rates without delays
// Author: Nick Gammon
// Date: 27 December 2012

#include <LedFader.h>

// set up some LEDs
//                 pin  min max  millis
LedFader strobe     (3, 10, 200,  1000);
LedFader navigation (5, 10, 200,   500);
LedFader torpedoes  (6, 10, 200,   250);

void setup()
  {      
  strobe.begin ();
  navigation.begin ();
  torpedoes.begin ();
  }  // end of setup

void loop()
  {
  // update lights
  strobe.update ();
  navigation.update ();
  torpedoes.update ();
  
  // do other useful stuff here ...
 
  }  // end of loop



State machine


This code illustrates using a state machine to turn LEDs on in sequence.

Code:
// The Enterprise
// Author: Nick Gammon
// Date:   27 December 2012

#include <LedFader.h>
#include <LedFlasher.h>

// pin assignments
const byte PrimaryLightsPin = 13;
const byte SecondaryLightsPin = 12;
const byte DeflectorPin = 11;   // PWM
const byte NacellesPin = 10;    // PWM
const byte ImpulsePin = 9;      // PWM
const byte ShuttleBayLightPin = 8;
const byte ShuttleBayLandingPin = 7;
const byte RCSPin = 6;   // PWM
const byte FloodlightsPin = 5;  // PWM
const byte NavigationPin = 4;
const byte StrobesPin = 3;   // PWM
const byte TorpedoPin = 2;

// Faders

//                           pin       min   max   millis  on?    stop?
LedFader impulseFader     (ImpulsePin,  0,   40,  3000,   false,  true);
LedFader RCSFader         (RCSPin,      0,   40,  3000,   false,  true);

// Flashers

//                 pin          off-time  on-time   on?
LedFlasher strobe (StrobesPin,  100,      100,     false);

// states for the state machine
typedef enum
  {
  initialState,
  wantPrimaryLights,
  wantSecondaryLights,
  wantDeflector,
  wantNacelles,
  wantImpulse,
  wantShuttleBayLight,
  wantShuttleBayLanding,
  wantRCS,
  wantFloodlights,
  wantNavigation,
  wantStrobes,  
  wantTorpedo,
  
  // more states here
  
  } states;

// state machine variables
states state = initialState;
unsigned long lastStateChange = 0;
unsigned long timeInThisState = 1000;

void setup ()
  {
  pinMode (PrimaryLightsPin, OUTPUT);
  pinMode (SecondaryLightsPin, OUTPUT);
  pinMode (DeflectorPin, OUTPUT);
  pinMode (NacellesPin, OUTPUT);
  pinMode (ShuttleBayLightPin, OUTPUT);
  pinMode (ShuttleBayLandingPin, OUTPUT);
  pinMode (FloodlightsPin, OUTPUT);
  pinMode (NavigationPin, OUTPUT);
  pinMode (StrobesPin, OUTPUT);
  pinMode (TorpedoPin, OUTPUT);

  // set up faders, flashers  
  impulseFader.begin ();
  RCSFader.begin ();
  strobe.begin ();
  }  // end of setup

        
void doStateChange ()
  {
  lastStateChange = millis ();    // when we last changed states
  timeInThisState = 1000;         // default one second between states

  switch (state)
   {
    case initialState:
         state = wantPrimaryLights;
         break;
        
    case wantPrimaryLights:
         digitalWrite (PrimaryLightsPin, HIGH);
         state = wantSecondaryLights;
         break;
        
    case wantSecondaryLights:
         digitalWrite (SecondaryLightsPin, HIGH);
         state = wantDeflector;
         break;
        
    case wantDeflector:
         digitalWrite (DeflectorPin, HIGH);
         state = wantNacelles;
         break;
        
    case wantNacelles:
         digitalWrite (NacellesPin, HIGH);
         state = wantImpulse;
         break;
        
    case wantImpulse:
         impulseFader.on ();
         state = wantShuttleBayLight;
         break;
        
    case wantShuttleBayLight:
         digitalWrite (ShuttleBayLightPin, HIGH);
         state = wantShuttleBayLanding;
         break;
        
    case wantShuttleBayLanding:
         digitalWrite (ShuttleBayLandingPin, HIGH);
         state = wantRCS;
         break;
        
    case wantRCS:
         RCSFader.on ();
         state = wantFloodlights;
         break;
        
    case wantFloodlights:
         digitalWrite (FloodlightsPin, HIGH);
         state = wantNavigation;
         break;
        
    case wantNavigation:
         digitalWrite (NavigationPin, HIGH);
         state = wantStrobes;
         break;
        
    case wantStrobes:
         strobe.on ();
         state = wantTorpedo;
         break;
        
    case wantTorpedo:
         digitalWrite (TorpedoPin, HIGH);

         timeInThisState = 5000;         // no rush

         // what next?
         break;
  
  
   }  // end of switch on state
    
  }  // end of doStateChange


void loop ()
  {
  
   if (millis () - lastStateChange >= timeInThisState)
     doStateChange ();

   // update faders, flashers
   impulseFader.update ();
   RCSFader.update ();
   strobe.update ();
 
  // other stuff here like testing switches
    
  }  // end of loop

Example output:



The first 4 LEDs turn on one second apart. The 5th one fades on over 3 seconds. The 8th one likewise (it's a bit hard to see, the LEDs are so bright). The second-last one flashes.

All this is done without using delay at all. And millis is only used in two places in the whole sketch (plus a couple of times in each library).

loop is very short. It doesn't block. You could test switches and have them respond immediately.

The state machine, implemented in doStateChange changes state every second (however you can alter the time between state changes). At each state change you turn on, or off, whatever you want to. Then you tell it what the next state is.
« Last Edit: November 25, 2013, 05:17:20 pm by Nick Gammon » Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 177
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

completely overwhelmed and shell shocked.

That is incredible. lots of functions to read up on and so on now.

so, the fade part of it. How would you go about fading the strobe from off to 50% then later on bringing it up to 100% and cross fading it even later on with another led which would be off?

I've been up all night so far so im going to have a ciggy and get some sleep, but yeah, the above is a fleeting thought as no thoughts remain in my head for very long after 20 hours being awake  smiley-red

im still rather shell-shocked at all that though haha. incredible nick, truely incredible
« Last Edit: December 27, 2012, 12:21:02 am by secretreeve » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Let's worry about the fine details later. I certainly can't think if I've been awake for 20 hours.
Logged


Pages: 1 ... 13 14 [15] 16 17 18   Go Up
Jump to: