BREAKING NEWS

05 November 2015

Light games with Arduino

The continuous strand Christmas holidays with this first simple game of lights for your Christmas tree or crib. In future articles you will find examples of different Christmas games and who knows that it can not realize the play of light radio-controlled:-)

I'll show you a simple sketch, leaving you the ability to define both what type of lights to use, perhaps by recycling the old series of lights now disused because static or before realizing the series with the LEDs.
Remember that the outputs Arduino not deliver high currents and for this you will need to study an external power supply, it could be done with the triac, if you want to create a control lights to 220v, or transistor if you directly CONNECTING series low voltage.
Let's move on to the sketch:
#include <SoftPWM.h>
  
 int minpin = 2;
 int maxpin = 13;
 int fadeUp = 1000;
 int fasdeDown = 1000;
  
 void setup () {
   SoftPWMBegin ();
   for ( int i = minpin; i <= maxpin; i ++)
   {
     SoftPWMSet (i, HIGH);
   }
   for ( int i = minpin; i <= maxpin; i ++)
   {
     SoftPWMSet (i, LOW);
     SoftPWMSetFadeTime (i, fadeUp, fasdeDown);
   }
 }
  
 void loop () {
    
  game_one ();
  
 }
  
 void game_one () {
   for ( int l = minpin; l <= maxpin; l ++)
   {
     SoftPWMSetPercent (l, 100);
     if (l> = (minpin + 1)) {SoftPWMSetPercent ((l-1), 0); }
     if (l == minpin) {SoftPWMSetPercent ((maxpin), 0); }
     delay (fadeUp);
   }
 }
To allow maximum flexibility I planned to use the digital outputs of the Arduino, leaving the possibility of reducing them in number if you occorressero less set of lights.
I have also planned to use the digital outputs in PWM mode in this way you can use fade effects on all the lights;uses only the foresight to make sure that your series can be driven in dssolvenza worth their break before the end of the festivities, and the effect would be unpleasant. Remember also to pay close attention to high voltages (220v) if you decide to implement a control lights to 200v.
We begin to see how it behaves the sketch.
Line 10: Include the library SoftPWM , which allows you to use all the digital outputs with PWM signals;
Lines 12-15: Define the variables of type integer needed to run your game of light, in particular minpin and maxpindefine the first and last pin of Arduino you'll use, are set respectively to 2:13 so you exclude first 2 pins (0 and 1) Tx and Rx usually intended for serial communications; variables fadeUp and fadeDown define the fade times for switching on and off;
Line 17: Define the function setup () whose purpose is clear you;
Line 18: initializes the library SoftPWM, indicating the sketch you intend to use for this program;
lines 19-22: perform a first round of StartUp, ie a cycle in which all light up, is to make sure everything works and there are number of burned out bulbs; It is also very useful to know when the circuit receives for the first time voltage. At line 21 uses the SoftPWMSet (pin, value) for check, in each cycle for the value of the increments of one from minpin up to the maxpin, for each pin sets the value of the pin to HIGH ie turn on the connected series;
lines 23-27: perform the same operation of the lines 19-22 but setting the value of each pin to LOW ie turning off the series; also using the SoftPWMSetFadeTime (pin, fadeUp, fadeDown) imposed the timing with which the library softPWM must turn on and off each line of light bulbs, their values ​​are defined on lines 14 and 15; WARNING: to obtain a fluid and can truthfully vary at will these two values ​​but do not recommend to set the fadeUp of lower value than the fadeDown would get that the switching off phase lasts longer than that of ignition, making the effect not balanced;
Lines 30-34: In this sketch the function loop () is limited to launch the first play of light, I wanted to just give this function because so we will add new plays of light in this same sketch;
line 36: define a new function game_one () that performs the first play of light and that is invoked by the loop () to each eseguzione cycle Arduino;
Line 37: starts a cisclo the minimum value of the pin (minpin) to the maximum value of the pin (maxpin) you'll run the operations for each pin;
Line 39: utiliza function SoftPWMSetPercent (pin, percentage) where imposed for each pin, the first value, the percentage who want to arrive the PWM signal, in practice, a setting of 100 indicates the SoftPWM to increase the value of the pin set from 0 % to 100% in the time indicated by fadeUp (see line 26), the Seller will send considerations values ​​from 0 to 255 to the corresponding output, in the set time;
Line 40: check that the value of the sum is exceeded (minpin + 1) This means that the sketch executes instructions in brackets only when it assumes a value of 3 (2 + 1) This trick allows you to turn off the series earlier on, in practice with l = 2 (l = minpin) turn on the number of lights connected to pin 2; with l> = 3 turn on the series of lights connected to the pins from 3 on and turn off the previous, to do this using the same function SoftPWMSetPercent by setting to 0 the percentage of the previous pin (l-1);
Line 41: is very similar to the previous line, but allows you to turn off the last set on when l = minpin, comes into play immediately but its effect you see only the second loop Arduino as the first loop you lit in sequence series connected to pins 2,3,4,5,6,7,8,9,10,11,12,13 off whenever the previous one, but you will still burning the last line (13 or maxpin) for which the next loop provvederai extinguish propio that while the first (or second minpin) comes on, the effect is continuous rotation;
Line 42: wait the time necessary to achieve what you have set in Arduino, or wait pin in power has reached the maximum level and that any shutdown is ended, to make only games of its time, knowing that is the fadeUp time in which the SoftPWM increments the value in ignition and which fadeDown is the time to decrement by setting the delay, waiting time, as fadeUp the effect will be contiguous series of alternating on and off. CAUTION: for this reason, we do not recommend to set a fadeDown exceeding fadeUp, would ruin the effect;

Share this:

Post a Comment

 
Back To Top
Distributed By Blogger Templates | Designed By OddThemes