Go Down

Topic: Night street lights (Read 3122 times) previous topic - next topic

tberg

HI
I work with model trains and I want to turn on the street lights X 50+ in a random way and fade up in a random way until 255.
I'm sure there is an easier way to do this than my code Attached.
it works OK but would like to be different ever time I hit switch one which is on (starts loop)
If you put code into a simulator with a mega and click switch one to turn on.
and then switch 2 to randomly turn off.

As I said works OK just like to see if there is a better way
Thanks for you time
Tom

JChristensen

#1
May 28, 2014, 03:29 am Last Edit: May 28, 2014, 03:39 am by Jack Christensen Reason: 1
Learn to use variables in loops.  Rather than 52 pinMode() statements to set the mode for each LED pin:

Code: [Select]
   for ( int led = 1; led <= 52; led++ ) {
       pinMode(led, OUTPUT);
   }


Hard to believe that the code works OK because it doesn't even compile.

Not sure what statements like

Code: [Select]
 for(int FadeVaule = 60);

are meant to do, but they do not compile.

ledsupply


Learn to use variables in loops.  Rather than 52 pinMode() statements to set the mode for each LED pin:

Code: [Select]
   for ( int led = 1; led <= 52; led++ ) {
       pinMode(led, OUTPUT);
   }


Hard to believe that the code works OK because it doesn't even compile.

Not sure what statements like

Code: [Select]
 for(int FadeVaule = 60);

are meant to do, but they do not compile.


great , Do not understand  :smiley-roll-sweat:

Simpson_Jr


There are a lot of programming errors in your sketch and you should check what's possible technically with an arduino. For a human it's easy to recognize you want to turn a light off in the next line as example.

Code: [Select]
analogWrite(led11,brightness = 0);

A microcontroller does need exact/correct commands though, otherwise it will result in an error.
It will expect something like this

Code: [Select]

brightness = 0;
analogWrite(led11,brightness);


One would expect the next lines to work and dim led14 for 50% after that last remark, but they won't.

Code: [Select]

brightness = 127;
analogWrite(led14,brightness);


An Arduino mega only has 15 pins (2 to 13 and 44 to 46) capable of supporting the analogWrite() function. It won't dim other pins using that function.

This doesn't mean it's impossible to fade 50+ leds though, you could even fade several 100 leds  in millions of colours using an arduino, but you'll need to know how you can do that.

Best is probably to learn about & experiment with the programming language and possibilities of an arduino first.

You can by following the tutorial/examples.

You didn't tell how you want to connect your leds by the way, keep in mind your arduino is the "brain" and can handle some power, but requires extra components if you want to handle a lot of power.

Even when all Leds are just using 20 milli Ampere/piece for example, it will use 1 Ampere when all 50 are on at the same time if you connect them directly, which will certainly blow up your arduino.


JChristensen


great , Do not understand  :smiley-roll-sweat:


Suggest spending some time in C++ tutorials, get up to speed on the language. Here is one that I refer to often but there is any number out there. GIYF.

http://www.cplusplus.com/doc/tutorial/

dave-in-nj


it works OK but would like to be different ever time I hit switch one which is on (starts loop)


just offering an idea for your start point.   if you measure the voltage on the track and bring that into the ADC and used that value as a start point, it would be pretty random.

or maybe use a microphone ?  the value would be random so that it would start somewhere different every time.

Paul__B


There are a lot of programming errors in your sketch and you should check what's possible technically with an arduino. For a human it's easy to recognize you want to turn a light off in the next line as example.
Code: [Select]
analogWrite(led11,brightness = 0);
A microcontroller does need exact/correct commands though, otherwise it will result in an error.


Someone correct me by all means, but as far as I know that is correct, if odd, "C++" syntax.

Go Up