Pages: 1 2 3 [4] 5   Go Down
Author Topic: Help with LED Lamp concept.  (Read 6666 times)
0 Members and 1 Guest are viewing this topic.
Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

Let's see if I can tell you this in a way that makes sense.  Apologies if I fail first time  smiley

The problem isn't the delay, the problem is the for loop that's in the RGB function.  What happens with all the other states is you test the switch and do something then immediately return to loop().  With RGB though, there's that for loop which traverses from one colour to the other.  While the code is in that loop, you're not testing the state of the switch so it gets ignored.  When the traverse is complete it will come back to loop() to start testing the switch again.  There may be more wrong with this logic, but that's the first that hits me.

One solution would be to check for the button state each time around that for loop, but that's not the way I'd do it.  Rather than duplicating the switch testing code, make RGB not complete the entire loop before returning, but just complete one more step in the traverse.  That way your logic will return to loop() to test your switch value and exit from the traverse if needed in between each step. 

Hope that makes sense, Geoff
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Geoff,

Thanks for the reply, I now understand  why its not working. But because I am still learning I have no clue how to fix it.

  So far in this little project I have learned a lot. I now know more about running LED's In parallel, in series and in series/parallel. I have learned about PNP and NPN transistors. I have played with the code to do a few different things. But because I did not wright the code I really don't fully understand it.  That all being said I have no clue how to fix it.

I am thinking once I get this project done and see how it all works it will help me with future projects.

I have just spent a grand on a laser engraver and upgrades to is. I have planes to incorporate the Laser and the wood working that I do with some of the lighting I have been playing with using the Arduino.

I know I am asking a lot, but if someone is willing to not only show me how to fix it but explain to it me so I can learn.  I would be willing to make it worth their time   

Thanks
Logged

Offline Offline
Newbie
*
Karma: 4
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I took a quick look at your code and noticed that your "traverse" process uses a nested for-loop structure.  Currently, there are no conditions in place to "break out" of these loops when the buttons are pressed.

Since you've already declared your button state variables globally, I would suggest making a new function that you can call that checks the states of those buttons.  Creating this function would make it re-usable and accessible by other parts of your program.  I would call this new function at the top of the loop()-(replace the button checking code already there).  Then you can also call this new function from within those for-loops to check if the buttons have been pressed, and "break;" out of the loop as needed.  Keep in mind you would want to break out of both loops on a single button press so you may want to create new variable to manage this cascading breaking so that it completes before the button is released.

http://arduino.cc/en/Reference/Break


Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Skorn

I really appreciate your input but I have no clue on what I am doing. Like I said I did not write the code that transition the colors.

On a side note, Anyone know of any clubs in the West Palm area of Florida. I have gotten a few books and have been reading all I can online. But I learn better by seeing it done and then trying to do it.


 
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Am I on on the right path.

It now will turn off, but its also not transitioning the colors

Code:
//RGB w/Tarnistion

#define RED 4
#define GREEN 0
#define BLUE 1
#define delayTime 70

int switchPin = 3;         
long previousMillis = 0;       
long interval = 0;         
int val;
int val2;
int buttonState;
int mode = 0;

/*
 RGB LED - Automatic Smooth Color Cycling

 Marco Colli
 April 2012
 Uses the properties of the RGB Colour Cube
 The RGB colour space can be viewed as a cube of colour. If we assume a cube of dimension 1, then the
 coordinates of the vertices for the cubve will range from (0,0,0) to (1,1,1) (all black to all white).
 The transitions between each vertex will be a smooth colour flow and we can exploit this by using the
 path coordinates as the LED transition effect.
*/
// Tranistion Setup


// Constants for readability are better than magic numbers
// Used to adjust the limits for the LED, especially if it has a lower ON threshold
#define  MIN_RGB_VALUE  10   // no smaller than 0.
#define  MAX_RGB_VALUE  255  // no bigger than 255.



// Slowing things down we need ...
#define  TRANSITION_DELAY  70   // in milliseconds, between individual light changes
#define  WAIT_DELAY        500 // in milliseconds, at the end of each traverse
//
// Total traversal time is ((MAX_RGB_VALUE - MIN_RGB_VALUE) * TRANSITION_DELAY) + WAIT_DELAY
// eg, ((255-0)*70)+500 = 18350ms = 18.35s

// Structure to contain a 3D coordinate
typedef struct
{
  byte  x, y, z;
}
coord;

static coord  v; // the current rgb coordinates (colour) being displayed


const coord vertex[] =
{
  //x  y  z      name
  {
    0, 0, 0    }
  , // A or 0
  {
    0, 1, 0    }
  , // B or 1
  {
    0, 1, 1    }
  , // C or 2
  {
    0, 0, 1    }
  , // D or 3
  {
    1, 0, 0    }
  , // E or 4
  {
    1, 1, 0    }
  , // F or 5
  {
    1, 1, 1    }
  , // G or 6
  {
    1, 0, 1    }  // H or 7
};

const byte path[] =
{
  0x01, 0x23, 0x76, 0x54, 0x03, 0x21, 0x56, 0x74,  // trace the edges
  0x13, 0x64, 0x16, 0x02, 0x75, 0x24, 0x35, 0x17, 0x25, 0x70,  // do the diagonals
};

#define  MAX_PATH_SIZE  (sizeof(path)/sizeof(path[0]))  // size of the array





void setup() {

  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);

  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(RED, OUTPUT);
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  digitalWrite(RED, HIGH);
  buttonState = digitalRead(switchPin);
}



void Red(){
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  digitalWrite(RED, LOW);
}

void Green(){
  digitalWrite(GREEN, LOW);
  digitalWrite(BLUE, HIGH);
  digitalWrite(RED, HIGH);
}

void Blue(){
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, LOW);
  digitalWrite(RED, HIGH);
}

void White(){
  digitalWrite(GREEN, LOW);
  digitalWrite(BLUE, LOW);
  digitalWrite(RED, LOW);


void off(){
  digitalWrite(GREEN, HIGH);
  digitalWrite(BLUE, HIGH);
  digitalWrite(RED, HIGH);


void Button(){
   val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds
  val2 = digitalRead(switchPin);
}

void traverse(int dx, int dy, int dz)
// Move along the colour line from where we are to the next vertex of the cube.
// The transition is achieved by applying the 'delta' value to the coordinate.
// By definition all the coordinates will complete the transition at the same
// time as we only have one loop index.
{
  if ((dx == 0) && (dy == 0) && (dz == 0))   // no point looping if we are staying in the same spot!
    return;

  for (int i = 0; i < MAX_RGB_VALUE-MIN_RGB_VALUE; i++, v.x += dx, v.y += dy, v.z += dz)
  {
    // set the colour in the LED
    analogWrite(RED, 255-v.x);
    analogWrite(GREEN, 255-v.y);
    analogWrite(BLUE, 255-v.z);

    delay(TRANSITION_DELAY);  // wait fot the transition delay
  }

  delay(WAIT_DELAY);          // give it an extra rest at the end of the traverse
}


void RGB()

{
  int    v1, v2=0;    // the new vertex and the previous one

  // initialise the place we start from as the first vertex in the array
  v.x = (vertex[v2].x ? MAX_RGB_VALUE : MIN_RGB_VALUE);
  v.y = (vertex[v2].y ? MAX_RGB_VALUE : MIN_RGB_VALUE);
  v.z = (vertex[v2].z ? MAX_RGB_VALUE : MIN_RGB_VALUE);

  // Now just loop through the path, traversing from one point to the next
  for (int i = 0; i < 2*MAX_PATH_SIZE; i++)
  {
    // !! loop index is double what the path index is as it is a nybble index !!
    v1 = v2;
    if (i&1)  // odd number is the second element and ...
      v2 = path[i>>1] & 0xf;  // ... the bottom nybble (index /2) or ...
    else      // ... even number is the first element and ...
    v2 = path[i>>1] >> 4;  // ... the top nybble

    traverse(vertex[v2].x-vertex[v1].x,
    vertex[v2].y-vertex[v1].y,
    vertex[v2].z-vertex[v1].z);
   break;
   Button();
  }
 
}



void loop(){
 Button();

  if (val == val2){
    if (val != buttonState){
      if (val == LOW){

        if (mode == 0){
          White ();
          mode = 1;
        }
        else if (mode == 1){
          Red();
          mode = 2;
        }
        else if (mode == 2){
          Green();
          mode = 3;
        }
        else if (mode == 3){
          Blue();
          mode = 4;
        }
        else if (mode == 4){
          RGB ();
          mode = 5;
        }
        else if (mode == 5){
          off();
          mode = 0;
        }
      }


      buttonState = val;
    }
  }

}











Logged

Offline Offline
Newbie
*
Karma: 4
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Kculm,

It looks like you're on the right track.  Im going to suggest some preliminary changes to try a few things and we could take it from there...

BTW, strykeroz, I didn't mean to hijack this thread.  I've been lurking on this forum for a long time now and have gleened much, I would like to give back to this community...

It looks as though you've implimented variables  val and val2 as some kind of debouncing routine, which is fine.  This leads me to ask what the buttonstate variable is meant to do or handle?  Perhaps it is redundant now?

I don't want to make assumptions so please confirm that you would like each button press switch to the next mode.  If thats the case you could consider that the button() function can incriment the mode++ for you.  If you impliment this, you can simplify your loop() function to directly execute a mode based on the variable mode value.   

To break out of a loop you would need to call the button() function,  then evaluate if mode has changed, if so break;:

Code:
// within a for loop where 4 is the mode you want to break out from
button();
if(mode!=4) break;









Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(looks like we were both posting at the same time Skorn - FWIW here's my thoughts)

Kculm,

You have a break statement inside your loop now and it's not tied to a result from a switch read - it just breaks the loop each time - therefore that loop won't have a chance to complete the transition.  What Skorn posted was that you could have a function to read the switch and act on it, that would also fire off the break statement if a switch press were detected.  Since val and val2 are global in your code, you can do this by giving your button function a return value, something like this:
Code:
boolean Button() {
   val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds
  val2 = digitalRead(switchPin);

  if(val == val2 && val == LOW) return true;
  else return false;
}
Then, in your traverse loop it looks something like this:
Code:
 // Now just loop through the path, traversing from one point to the next
  for (int i = 0; i < 2*MAX_PATH_SIZE; i++)
  {
    // !! loop index is double what the path index is as it is a nybble index !!
    v1 = v2;
    if (i&1)  // odd number is the second element and ...
      v2 = path[i>>1] & 0xf;  // ... the bottom nybble (index /2) or ...
    else      // ... even number is the first element and ...
    v2 = path[i>>1] >> 4;  // ... the top nybble

    traverse(vertex[v2].x-vertex[v1].x,
    vertex[v2].y-vertex[v1].y,
    vertex[v2].z-vertex[v1].z);
   if (Button()) break;      // <-- this replaces the two lines currently at the end
  }
Can't vouch for the rest of the logic, but this should step you forward some.

Cheers ! Geoff
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Offline Offline
Newbie
*
Karma: 4
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(strykeroz, I'll back out of this discussion to avoid creating any confusion.)

Just a final thought. Kculm,  Since your RGB() function calls the traverse function every iteration in a for-loop.  You'll need to add another conditional break statement there.

Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(strykeroz, I'll back out of this discussion to avoid creating any confusion.)
Please don't.  There's always multiple ways to skin a cat in code  smiley
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(strykeroz, I'll back out of this discussion to avoid creating any confusion.)
Please don't.  There's always multiple ways to skin a cat in code  smiley

I also ask that you keep adding your input, every little bit helps. 

I am getting to the point where I feel that Arduino Programming is not for me. I have been playing with the Arduino for almost 3 months and I am not as far a long as I would like to be. Not that I have not been trying.

I put a side at a minimum 2hr (More on the weekend) each night to mess with the Arduion.  I Search the Web, watch YouTube and read this forum a long with a few others. I picked up a few books a long the way. But nothing helps me as much as the input I get from this Forum and it members. So please keep the post coming.

But guys I do ask one thing. I am nowhere on the same level as you all and some of the things that are said go over my head.

So to Fungus, Skorn and Strykeroz

Logged

Valencia, Spain
Online Online
Faraday Member
**
Karma: 150
Posts: 5666
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Learning to code is something that needs proper study, not just hacking around.

If you're serious about Arduino you're going to have to read some non-Arduino coding tutorials.
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Newbie
*
Karma: 4
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

kculm,

Working with the Arduino and electronics gets pretty frustrating for anyone at times.  Patience is the key and 3 months is really not alot of time when you are coming in with little electronics and programming knowledge and experience. 

I was going to suggest that you take a step back. The code you are using for your traverse routine was developed by someone else and it is fairly advanced for a beginner.  I suggest "shelving" that part of the code and come back to it down the road when you can understand it better. Refocus your effort on developing your own code to control the LEDs in a simpler fashion,  such as blinking and pulsing the leds.

I agree with fungus that its essential to spend some time studying general programming in any language.  C or C++ is probably your best bet to learn the Ardiuno...
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Learning to code is something that needs proper study, not just hacking around.

If you're serious about Arduino you're going to have to read some non-Arduino coding tutorials.


I am very serious, Can you recommend something.

Thanks
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 209
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I agree with fungus that its essential to spend some time studying general programming in any language.  C or C++ is probably your best bet to learn the Ardiuno...

Skorn,

I totally agree with both of you, the code I am using is way above my Pay grade. But its the only code I could find that does what I need it to do. (nice And  Smooth).

Like have said I am making a custom acrylic lamp. So far I have spent over 200 on custom cut and polished Acrylic and the last part of the puzzle is the code.

I will be sure to post a pic/video once it is done.

Now that being said. I have done all the examples I could find on how to make LED’s blink, fade, and all the rest. But most of the stuff I find show you how to do them and not why.  Another reason I picked the code I am using is because I knew it would be a challenge. Just did not know how big of a challenge it would turn out to be. 

I am going to take your advice and look in to C or C++ , I have played with C# in the past but just for fun.

Also if any of you know of good books or sites please let me know.

I am going to go back over the posts and see what I can figure out from all the input.

Thanks Guys.
Logged

Michigan
Offline Offline
Newbie
*
Karma: 1
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Are you trying to do anything like what I posted?

http://arduino.cc/forum/index.php/topic,152268.0.html
Logged

Pages: 1 2 3 [4] 5   Go Up
Jump to: