Automobile light sequencer-controller

Arduino noob looking for a constructive critique:

This is my first attempt at programming for the Arduino, and I have to say I'm very happy with how easy it was to work with and how well it came out. I usually program for Windows in Delphi, so it was hard to readjust to having my compiled code come out at 1814 BYTES. How large is the typical first program anyway ?

I've been needing to make a light controller for a show car I'm building, and after lurking here for a while I figured this would be the way to go. My coding style is probably not what anyone here is used to seeing , but I would be very glad to get any critique of my circuit or the programming.

I wrote the program so that it would be very responsive (even with built in delays) and so that I could easily add or change new light sequences without having to change the code (only variables). I realize I could have used interrupts for the input, but what I came up with is super fast and very tiny, and again, its my first try

I tried a couple of times but I cant seem to post the code here. I guess it's off to the FAQ again

The web page for my project with YouTube, photos and code

http://www.raymondandrews.com/zx-lights

Hi Ray, nice job with the video and the code and documentation. You may want to move connection on D1 (its used by the Serial port) to D14 (A0). Also, you could use internal pull-ups instead of R9-R13 (just digitalWrite an input pin HIGH to enable them). Ideally pots connected to analog inputs should be between 10k or 20k but in this application it probably doesn't matter.

have fun!

Hi Ray,

ths lighting looks nice and smooth. I also like the idea about having multiple profiles for increased flexibility. Good work!

You could have a look at the code of people who program POV-(persistence of vision)devices, it has a lot in common with your coding (especially storing light sequences in arrays for different symbols(letters).

For example this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1234005825 (which is slightly different but could give you inspirations I think)

Looking over your code quickly I found one thing:

// set all of the pins to input just to be safe
  for (x = 0; x <= 13; x++)  pinMode(x, INPUT);

This sets pins 0 to 14 to input. I don’t know what Arduino does with the 14 (as there’s no pin 14), I think I read somewhere that pin numbers 14-19 refer to the analog pins when used as digital I/O pins. I also remember having read, that setting pin 1 (the TX pin) to input can cause problems when communicating directly over the TX/RX pins. If you don’t use them it should be OK.

As I’m quite a programming noob I#ve never seen this kind of constant declarations at the beginning of your code in Arduino sketches:

const int first_light_pin = 6;          // starting point for led output pins...

Does it work fine? Which effect has setting those constants for your sketch?

Edit: Sorry for redundancy, mem, you were just posting as i wrote :wink:

This sets pins 0 to 14 to input.

Nope it doesn't, the range is [0,13].

As I'm quite a programming noob I#ve never seen this kind of constant declarations at the beginning of your code in Arduino sketches:

I'm not sure about Delphi, but as a C++ programmer, it's always been considered good practice to store constant values in a const variable because it makes it much easier to debug your program. You're right though, it doesn't really do much help as far as Arduino code goes.

? it's always been considered good practice to store constant values in a const variable because it makes it much easier to debug your program. You're right though, it doesn't really do much help as far as Arduino code goes.

Not sure what you mean by that last statement. Arduino uses a C++ compiler, by declaring variables as const, the optimizer can imbed the values directly in the machine code instead of storing them as memory variables. This makes the code slightly smaller and faster than if they were not constants and easier for the compiler to detect errors than if they were #defined.

Thanks mem for explanation, that sounds good to me.

As for the pin 14 problem I’m sorry, I thought in a for loop it checks for the condition to be true and if so, adds the increment. Furthermore I thought it would go up to x == 14, as the condition is x <= 13. So it would add another one if x == 13, which would lead to pin x ( == pin 14) being set as Input as well.

Probably I’m wrong :slight_smile:

Yeah I guess you said it better than I did! lol. All I meant to say was that const variables make it easier to spot problems, when say you're stepping through a program using a debugger.

Thank you for the kind words and tip’s

mem:
Actually I’ve thought about moving all of the inputs over to the Analog side, and probably will later. To be honest, I thought of this after I had to remove pin 1 and comment some code to do some serial debugging (stupid “=” in an if statement)

Thanks for the internal pull-up info, I was wondering what would happen if I did that, glad its something useful. It would be nice if there were a book, other than the 36 page notebook.

With the 100K pot, I wasn’t sure what to use, so I started with the biggest and it happened to work.

Otacon:
(is your id as in the convention in Baltimore, I’ll be there)

You’re partially right, x will in fact go to 14, but at the end of the loop before its checked by “x <= 13” so the for-loop never executes with that value

Constants make the code a little smaller and faster, but I do it mostly for the readability. This way I can go back months later and know what the “6” actually means. I added a copy of the code on the web page without comments, try reading that version and you can see how it might help.

Thanx again

Excellent job and beautifully commented code. After studying your code, I'll be able to learn lots of better ways to improve my own code.

Thanks lots for sharing.

It would be nice if there were a book, other than the 36 page notebook.

There is Massimo Banzi's “Getting Started with Arduino”: http://oreilly.com/catalog/9780596155513/

It's a great introduction to Arduino hardware and software for people with no experience with either or both.

If you're looking for something more advanced, I am currently writing the Arduino Cookbook for O'Reilly Media. This will cover hundreds of solutions to common problems encountered by Arduino users. It won't be finished till later in the year and I will say more about it when its closer to the date.

That's some really pretty code. :)

Constants make the code a little smaller and faster, but I do it mostly for the readability. This way I can go back months later and know what the "6" actually means. I added a copy of the code on the web page without comments, try reading that version and you can see how it might help.

Is there much difference between constants and DEFINE for things like this? I've been mostly using defines, IE:

#define UP 1
#define DOWN 0

vs constants:

const int up = 1;
const int down = 0;

easier for the compiler to detect errors than if they were #defined

How does it make it easier to detect errors?

How does using it (const instead of #define) make it easier to detect errors?

define uses the preprocessor to substitute values into the source code before it is compiled. The preprocessor does not understand or follow the language rules for C/C++ and this can make it more error prone.

Using a const is better then #define because a const variable has a type and the compiler will check and report if the variable is being used in ways not appropriate for that type. The compiler will also respect C rules for the scope of a const variable. This can prevent errors where a #defined value is affecting more code then you intended.

const is a relatively new addition to the C languge so many traditional example still use #define, including the standard Arduino example sketches. But const is the better choice for new code.

I made some small (tiny) changes. Changed the processing of the hazard lights so they wouldn’t take precedence over the brake lights (oops). Also removed the line that set all the pins to input in setup, apparently the controller does that all by itself (web page updated)