Sunteți pe pagina 1din 11

Asta ii codul meu fara potentiometru

int LED[] = {3,5,6,9,10}; // pini unde am conectat led-urile


int nrleduri = sizeof(LED)/2;
int Sw = 2; // pinul unde am conectat butonul
int dimm = 20; // intervalul care il folosim pentru a vedea intensitatea led-urilor
int stare = 0;

void setup () {
for(int j = 0; j < nrleduri; j++) {
pinMode(LED[j], OUTPUT);
}
pinMode(Sw, INPUT);
}

void loop() {
int SwState = digitalRead(Sw); // citeste voloarea butonului
if(SwState == HIGH && stare == 0) { // verifica daca butonul este apasat
for(int j = 0; j < nrleduri; j++) {
for(int i = 0; i < 255; i++) {
analogWrite(LED[j], i);
delay(dimm);
}
}
stare = 1;
}
if(stare == 1 && SwState == LOW) {
for(int j = nrleduri - 1; j >= 0; j--) {
for(int i = 255; i >= 0; i--) {
analogWrite(LED[j], i);
delay(dimm);
}
}
stare = 0;
}
}

2










Codu cu potentiometru

/*
* Potentiometer LED Dimmer
* ------------------------
*
* Dims a LED connected to digital pin 10 based on the value obtained
* from a 10k potentiometer connected to analog pin 0.
*
* Created January 2010
* Matt Wiechec
*
*/

int potPin = 2; // select the input pin for the potentiometer
int ledPin = 10; // select the pin for the LED
int potVal = 0; // variable to store the value coming from the sensor

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600); // for debugging via the Serial Monitor
}

void loop() {
potVal = analogRead(potPin); // read the value from the sensor
potVal = map(potVal, 0, 1023, 0, 255); // scale it to use it with the
LED

analogWrite(ledPin, potVal); // set brightness

Serial.print("Pot Val: ");
Serial.println(potVal);
}



3








Codu cu RGB


/ HSV fade/bounce for Arduino - scruss.com - 2010/09/12
// Note that there's some legacy code left in here which seems to do
nothing
// but should do no harm ...

// don't futz with these, illicit sums later
#define RED 9// pin for red LED
#define GREEN 10 // pin for green - never explicitly referenced
#define BLUE 11 // pin for blue - never explicitly referenced
#define SIZE 255
#define DELAY 20
#define HUE_MAX 6.0
#define HUE_DELTA 0.01

//long deltas[3] = { 5, 6, 7 };
long rgb[3];
long rgbval;
// for reasons unknown, if value !=0, the LED doesn't light. Hmm ...
// and saturation seems to be inverted
float hue=0.0, saturation=1, value=1;

/*
chosen LED SparkFun sku: COM-09264
has Max Luminosity (RGB): (2800, 6500, 1200)mcd
so we normalize them all to 1200 mcd -
R 250/600 = 107/256
G 250/950 = 67/256
B 250/250 = 256/256
*/
long bright[3] = { 107, 67, 256};
//long bright[3] = { 256, 256, 256};

long k, temp_value;

void setup () {
randomSeed(analogRead(4));
for (k=0; k<3; k++) {

4

pinMode(RED + k, OUTPUT);
rgb[k]=0;
analogWrite(RED + k, rgb[k] * bright[k]/256);
}
}

void loop() {
hue += HUE_DELTA;
if (hue > HUE_MAX) {
hue=0.0;
}
rgbval=HSV_to_RGB(hue, saturation, value);
rgb[0] = (rgbval & 0x00FF0000) >> 16; // there must be better ways
rgb[1] = (rgbval & 0x0000FF00) >> 8;
rgb[2] = rgbval & 0x000000FF;
for (k=0; k<3; k++) { // for all three colours
analogWrite(RED + k, rgb[k] * bright[k]/256);
}

delay(DELAY);
}

long HSV_to_RGB( float h, float s, float v ) {
/* modified from Alvy Ray Smith's site:
http://www.alvyray.com/Papers/hsv2rgb.htm */
// H is given on [0, 6]. S and V are given on [0, 1].
// RGB is returned as a 24-bit long #rrggbb
int i;
float m, n, f;

// not very elegant way of dealing with out of range: return black
if ((s<0.0) || (s>1.0) || (v<1.0) || (v>1.0)) {
return 0L;
}

if ((h < 0.0) || (h > 6.0)) {
return long( v * 255 ) + long( v * 255 ) * 256 + long( v * 255 ) *
65536;
}
i = floor(h);
f = h - i;
if ( !(i&1) ) {
f = 1 - f; // if i is even
}
m = v * (1 - s);
n = v * (1 - s * f);
switch (i) {
case 6:
case 0:
return long(v * 255 ) * 65536 + long( n * 255 ) * 256 + long( m * 255);
case 1:
return long(n * 255 ) * 65536 + long( v * 255 ) * 256 + long( m * 255);
case 2:
return long(m * 255 ) * 65536 + long( v * 255 ) * 256 + long( n * 255);
case 3:
return long(m * 255 ) * 65536 + long( n * 255 ) * 256 + long( v * 255);
case 4:
return long(n * 255 ) * 65536 + long( m * 255 ) * 256 + long( v * 255);
case 5:
return long(v * 255 ) * 65536 + long( m * 255 ) * 256 + long( n * 255);

5

}
}














Cod cu cub de leduri 3x3x3
/*
ledcube.pde - Example sketch for controlling an LED cube.
Created by Gamaiel Zavala (gzip), 2009-2012
MIT License. See accompanying LICENSE file for terms.
*/

#include <LedCube.h>

#define SIZE 3
#define COLS (SIZE*SIZE)

byte levelPins[SIZE] = {11,12,13};

6

byte colPins[COLS] = {2,3,4,5,6,7,8,9,10};

LedCube cube(SIZE, levelPins, colPins);

void setup ()
{
}

void loop ()
{
delay(10);

// light each light one at a time
for(byte level=0; level<cube.getLevels(); level++)
{
for(byte col=0; col<cube.getCols(); col++)
{
cube.lightPulse(level, col, 100);
}
}

// light one level at a time, increasing speed each time
for(byte d=25; d>2; d-=2)
{
for(byte l=1; l <= cube.getLevels(); l++)
{
cube.lightLevel(l, d);
}
}

7


// light each row on each level
for(byte level=1; level<=cube.getLevels(); level++)
{
for(byte row=1; row<=cube.getLevels()*2; row++)
{
cube.lightRow(row, level);
}
}

// light each plane
for(byte i=3; i; i--)
{
for(byte row=1; row<=cube.getLevels()*2; row++)
{
cube.lightPlane(row, 10*i);
}
}

// single random light at a time
cube.randomLight(random(25,100),100);

// random column drop
for(byte x=0; x<=15; x++)
{
cube.lightDrop(random(0,cube.getCols()), random(50,150));
}

// circle around cube at a random level

8

for(byte x=0; x<=5; x++)
{
cube.lightPerimeter(random(0,cube.getLevels()), random(1,5), random(25,100));
}

// light each face
byte planes[] = {cube.getLevels()+1,cube.getLevels(),cube.getLevels()*2,1};
for(byte i=5; i; i--)
{
for(byte p=0; p<sizeof(planes); p++)
{
cube.lightPlane(planes[p], 5*i);
}
}

// random columns
cube.randomColumn(25);

// turn off a single column randomly
cube.enableBuffer();
for(byte c=0; c<30; c++)
{
cube.fillBuffer();
cube.invertBuffer();
cube.randomColumn();
cube.drawBuffer(7);
}
cube.enableBuffer(false);


9

// cols in and out
for(byte c=1, d=0; c<=10; c++)
{
if(c%2 == 0)
{
for(d=0; d<20; d++)
{
cube.lightColumn(2,1);
cube.lightColumn(4,1);
cube.lightColumn(6,1);
cube.lightColumn(8,1);
}
}
else if(c%4 == 1)
{
for(d=0; d<30; d++)
{
cube.lightColumn(1,1);
cube.lightColumn(3,1);
cube.lightColumn(7,1);
cube.lightColumn(9,1);
}
}
else
{
for(d=0; d<70; d++)
{
cube.lightColumn(5,1);
}

10

}
}

// diamond and box
byte diamond[] = {0,4, 1,1, 1,3, 1,4, 1,5, 1,7, 2,4};
byte box[] = {
2,0, 2,1, 2,2, 2,3, 2,5, 2,6, 2,7, 2,8,
1,0, 1,2, 1,6, 1,8,
0,0, 0,1, 0,2, 0,3, 0,5, 0,6, 0,7, 0,8
};
cube.lightSequence(box, sizeof(box), 200);
cube.lightSequence(diamond, sizeof(diamond), 400);

// helicopter effect
byte topSeq[8] = {0,3,6,7,8,5,2,1};
byte botSeq[8] = {8,5,2,1,0,3,6,7};
for(byte loops = 0, delay = 50; loops<=8; loops++)
{
for(byte s=0; s<8; s++)
{
byte seq[] = {2,topSeq[s], 1,4, 0,botSeq[s]};
cube.lightSequence(seq, sizeof(seq), delay);
}
if(loops < 5) delay-=10; else delay += 10;
}

// turn off one light at a time
cube.enableBuffer();
cube.fillBuffer();

11

cube.drawBuffer(25);
for(byte w=0, l, c, max = cube.getNumLights(); w<max; )
{
// lower bound is inclusive, upper is exclusive
l = random(0, cube.getLevels());
c = random(0, cube.getCols());

if(cube.getBufferAt(l,c) == HIGH)
{
cube.lightOff(l,c);
cube.drawBuffer(5);
w++;
}
}
cube.enableBuffer(false);
}

S-ar putea să vă placă și