Go Down

Topic: Combining Codes (Read 1 time) previous topic - next topic


i made the code as simple as i could. the only thing that he need to change is to the declaration on the top to make it work. other then that i think its the same as his spec i think


Ash - I am sure that your code works but I still prefer to use functions for the reasons that I gave even if a function is only called once.  The problem is not writing the code but maintaining and changing it, particularly if that is not being done by the original author.  That particularly relates to function names. 

Seeing a function called RGB would indicate to me that it does something with the RGB LEDs whereas the in-line code says nothing about what is going on and it can be difficult to see where in the code a set of actions is complete.  I should probably have called the function RGBsequence for even better readability. You can use comments to help understand what is going on but by using a function it can easily be tested on its own unlike the in-line code.  Once you have a function that works it can be treated as a black box.  That is, of course, how the intrinsic Arduino functions work.

There is a school of thought that says that each function, including loop(), should be no longer than one screen long in order to aid readability but that may be going too far !
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


ye UKHeliBob.... i truly believe in what you say, i just want to give a working code though i forget to add in comment so that the user can understand what it does.
urm yes function would make it a better and easier to understand.


wow theres a flaw in my code


I noticed that. I'm looking now to see what it is.

It doesn't seem to stop when the photocell senses light, and it doesn't flash all of the colors.

I'm a total noob so I've just been playing around with the variables and just going over the code. To me what would be really interesting is if it would be possible to randomize the color output each time the photocell turns the RGB LED on. I'm sure that's possible, but I'd bet the code may be a lot more complex.

I do like how for whatever reason the RGB LED seems to fade with your code rather than just flash between colors.


If you want it responsive so the leds turn off immediately, you need to remove all the delays and handle the sequence in a non-blocking way using state variables. You can't check a sensor value during the delay, so the code keeps on going until the next time it reads the sensor. You could also have the sequence going in the background nonstop and just AND either 0xFF or 0x00 to the variable depending on the amount of light. 0xFF is all bits on, 0x00 off. Or you could dim the led by flicking the most significant bit and modifying the dimmed state to give values from 0 to 127 instead of all the way to 255 or something similar.


The only thing I'm not understanding yet is why it seems to cycle through five times before it stops. I know it won't stop immediately because it has to finish the cycle, but why five times?

I can look at the code to see if I can figure it out, but the color part of the code confuses me the most.

Go Up