Arduino Neo Pixel

 Arduino Neo Pixel



For this tutorial, we'll assume that you're working with LED strips – the simplest way to light up your bike, bar, CPU tower, and so forth.

You can cut the strips to any length you like; to do so, make sure you cut across the center of the oblong copper pads. That leaves you with solderable pads on each end, which will allow you to chain them together after the fact.

Solder some male-male headers to the end, with the little arrows on the LED strip pointing away. The arrows should point away from your headers. Put your headers behind the arrows. (It helps to chant this in your head, if you're doing lots of them...)

Note: Depending on where you buy your "NeoPixels", you may end up with something that has the pins in a different arrangement. Pay attention to the labels, as well as the schematic :)

THE CODE

Go download Adafruit's NeoPixel library to get started. You can just download the.zip file with the library, unzip it on your computer, and drag the contents into your Arduino libraries folder. (The "libraries" folder is usually created in the same "Arduino" folder where you save your sketches. If you don't have one yet, go ahead and create it.) Also, restart the Arduino IDE if you already had it open.

Once it's up again, you'll have some new example sketches. Let's take a look!

File > Examples > Adafruit NeoPixel > simple

This guy will light up your LEDs green, one at a time. How does it work? We're gonna use another for loop!

As we saw before, for loops are useful for nudging a number higher. We can use that to modify each LED in a sequence – or every other LED, or whatever you like.

  • So, first, we tell the sketch to include Adafruit's library.
  • Then, we define two variables: the data pin we're using (any PWM pin), and the number of pixels in the strip.
  • In the next bit, we initialize the strip as a new object, pixels. (You can call it whatever you want, of course.)
  • Then, we set a delay value, which will be used later to pause after lighting up each LED.

There's a little bit of board-specific code in the setup code, and then we tell the NeoPixel library to start communicating with this strip.

Loop time!

We've got another for loop, which is kicked off when the variable i is equal to 0.

Note that this is actually where we're defining the variable, as well – so it starts as 0. You can tell because of the int marker, and also because there's only a single = sign. (One = sets the first thing equivalent to the second, as in i = 0; two == checks to see whether the two sides are equivalent.)

So, this for loop says:

  • If i is 0 when you hit this block of code (and it is), then as long as i is less than the declared number of pixels, bump it up by 1...
  • then, set the color of the first pixel in the pixels strand to green...
  • then, actually push that color to the pixel...
  • then, wait 500ms (from the delayval setting)...
  • and repeat for each successive pixel, until you max out the strand.

It's a simple but fun animation. :)

Hit upload and deploy this code to your strand.




මෙම නිබන්ධනය සඳහා, ඔබ LED තීරු සමඟ වැඩ කරන බව අපි උපකල්පනය කරමු - ඔබේ බයිසිකලය, බාර්, සීපීයූ කුළුණ සහ යනාදිය ආලෝකමත් කිරීමේ සරලම ක්‍රමය.


ඔබ කැමති ඕනෑම දිගකට තීරු කපා ගත හැකිය; එසේ කිරීමට, ඔබ දිගටි තඹ පෑඩ් මධ්‍යයේ කැපීමට වග බලා ගන්න. එමඟින් ඔබට එක් එක් කෙළවරේ විකුණා දැමිය හැකි පෑඩ් ඇත, එමඟින් ඔබට ඒවා එකට සම්බන්ධ කර ගත හැකිය.


සමහර පිරිමි-පිරිමි ශීර්ෂයන් අවසානය දක්වා විසුරුවා හරින්න, LED තීරුවේ කුඩා ඊතල ඉවතට යොමු කරයි. ඊතල ඔබේ ශීර්ෂ වලින් ඉවතට යොමු විය යුතුය. ඔබේ ශීර්ෂ ඊතල පිටුපස තබන්න. (ඔබ මෙය බොහෝ දේ කරන්නේ නම් මෙය ඔබේ හිසෙහි ගායනා කිරීමට උපකාරී වේ ...)


සටහන: ඔබ ඔබේ "නියෝ පික්සෙල්" මිලදී ගන්නේ කොතැනින්ද යන්න මත පදනම්ව, ඔබට වෙනත් සැකැස්මක් තුළ අල්ෙපෙනති ඇති යමක් අවසන් විය හැකිය. ලේබල මෙන්ම ක්‍රමලේඛය කෙරෙහි අවධානය යොමු කරන්න :)


කේතය

ආරම්භ කිරීම සඳහා Adafruit හි NeoPixel පුස්තකාලය බාගන්න. ඔබට පුස්තකාලය සමඟ .zip ගොනුව බාගත කර ඔබගේ පරිගණකයේ එය ඉවත් කර අන්තර්ගතය ඔබේ Arduino පුස්තකාල ෆෝල්ඩරයට ඇදගෙන යා හැකිය. . .


එය නැවත ක්‍රියාත්මක වූ පසු, ඔබට නව උදාහරණ සටහන් කිහිපයක් ලැබෙනු ඇත. අපි බලමු!


ගොනුව> උදාහරණ> ඇඩෆ්‍රූට් නියෝ පික්සෙල්> සරලයි


මේ මිනිහා එකවරම ඔබේ LED කොළ පැහැපත් කරයි. එය ක්‍රියාත්මක වන්නේ කෙසේද? අපි ලූප් සඳහා තවත් එකක් භාවිතා කරන්නෙමු!


අප කලින් දුටු පරිදි, ලූප සංඛ්‍යාවක් ඉහළින් තල්ලු කිරීම සඳහා ප්‍රයෝජනවත් වේ. සෑම LED එකක්ම අනුපිළිවෙලින් වෙනස් කිරීමට අපට එය භාවිතා කළ හැකිය - හෝ වෙනත් සෑම LED එකක් හෝ ඔබ කැමති ඕනෑම දෙයක්.


ඉතින්, පළමුව, අපි සිතියමට කියනවා ඇඩෆ්රූට්ගේ පුස්තකාලය ඇතුළත් කරන්න.

ඉන්පසුව, අපි විචල්යයන් දෙකක් නිර්වචනය කරමු: අප භාවිතා කරන දත්ත පින් (ඕනෑම PWM පින්), සහ තීරුවේ ඇති පික්සල් ගණන.

ඊළඟ බිට් වලදී, අපි පික්සල් නව වස්තුවක් ලෙස තීරුව ආරම්භ කරමු. (ඇත්ත වශයෙන්ම ඔබට අවශ්‍ය ඕනෑම දෙයක් ලෙස හැඳින්විය හැකිය.)

ඉන්පසුව, අපි ප්‍රමාද අගයක් නියම කරමු, එය පසුව සෑම LED එකක්ම දැල්වීමෙන් පසුව විරාමයක් ලබා දෙනු ඇත.

සැකසුම් කේතයේ පුවරු-විශේෂිත කේත ටිකක් තිබේ, පසුව අපි නියෝ පික්සල් පුස්තකාලයට කියන්නේ මෙම තීරුව සමඟ සන්නිවේදනය ආරම්භ කරන ලෙසයි.


ලූප් කාලය!


අපට ලූප සඳහා තවත් එකක් තිබේ, එය විචල්‍යය 0 ට සමාන වන විට එය ඉවත් කරනු ලැබේ.


මෙය ඇත්ත වශයෙන්ම අප විචල්‍යය නිර්වචනය කරන ස්ථානය බව සලකන්න - එබැවින් එය 0 ලෙස ආරම්භ වේ. ඔබට int සලකුණු නිසාත්, තනි = ලකුණක් පමණක් ඇති නිසාත් ඔබට පැවසිය හැකිය. (එක් = පළමු දෙය දෙවැන්නාට සමාන වේ, i = 0; දෙක == දෙපැත්ත සමානදැයි බැලීමට පරික්ෂා කරයි.)


ඉතින්, මෙය ලූප් සඳහා මෙසේ පවසයි:


ඔබ මෙම කේත කොටසට පහර දෙන විට මම 0 නම් (එය එසේ නම්), ප්‍රකාශිත පික්සල් සංඛ්‍යාවට වඩා අඩු නම්, එය 1 කින් වැඩි කරන්න ...

ඉන්පසු, පළමු පික්සෙල් වල වර්ණය හරිත පැහැයට සකසන්න ...

ඇත්ත වශයෙන්ම, එම වර්ණය පික්සෙල් වෙත තල්ලු කරන්න ...

ඉන්පසු, මීටර් 500 ක් රැඳී සිටින්න (ප්‍රමාද සැකසුම සිට) ...

ඔබ එක් එක් පික්සෙල් සඳහා නැවත නැවත කරන්න.

එය සරල නමුත් විනෝදජනක සජීවිකරණයකි. :)


උඩුගත කර මෙම කේතය ඔබේ නූල් වලට යොදන්න.
























Download the Code






// NeoPixel Ring simple sketch (c) 2013 Shae Erisson// Released under the GPLv3 license to match the rest of the // Adafruit NeoPixel library #include <Adafruit_NeoPixel.h> #ifdef __AVR__ #include <avr/power.h> // Required for 16 MHz Adafruit Trinket #endif // Which pin on the Arduino is connected to the NeoPixels? #define PIN 6 // On Trinket or Gemma, suggest changing this to 1 // How many NeoPixels are attached to the Arduino? #define NUMPIXELS 16 // Popular NeoPixel ring size // When setting up the NeoPixel library, we tell it how many pixels, // and which pin to use to send signals. Note that for older NeoPixel // strips you might need to change the third parameter -- see the // strandtest example for more information on possible values. Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); #define DELAYVAL 500 // Time (in milliseconds) to pause between pixels void setup() { // These lines are specifically to support the Adafruit Trinket 5V 16 MHz. // Any other board, you can remove this part (but no harm leaving it): #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) clock_prescale_set(clock_div_1); #endif // END of Trinket-specific code. pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) } void loop() { pixels.clear(); // Set all pixel colors to 'off' // The first NeoPixel in a strand is #0, second is 1, all the way up // to the count of pixels minus one. for(int i=0; i<NUMPIXELS; i++) { // For each pixel... // pixels.Color() takes RGB values, from 0,0,0 up to 255,255,255 // Here we're using a moderately bright green color: pixels.setPixelColor(i, pixels.Color(0, 150, 0)); pixels.show(); // Send the updated pixel colors to the hardware. delay(DELAYVAL); // Pause before next pass through loop } }

0 Comments