Sie sind auf Seite 1von 9

void setup() // this function sets up pins 2 through 11 (10 pins) to output

// mode I could have done 1 through 10, but I wasn't sure if port 1 would
// work correctly
{
for (int pin = 2; pin <= 11; pin++)
{
pinMode(pin,OUTPUT);
}
}

// main looping functions


void loop()
{
// seed random variables from analog voltage reading from port 5
randomSeed(analogRead(5));
// check for potential difference across ports A0 and A1, if more than
// about 0.1 volt, the LEDs flash
if(abs(analogRead(A0) - analogRead(A1)) > 100)
{
flash(100);// flash function (see below)
}
else // else if no voltage, run main proglrms
{
// while the voltage difference is <~ 0.1 Volts, run main program
while(abs(analogRead(A0) - analogRead(A1)) < 100)
{
int pickme =random(1,10); // picks a random patter of LED patterns
switch(pickme)
{// FOR DESCRIPTIONS OF PATTERN FUNCTIONS, SEE BELOW
case 1:// all arguments being passed to functions are delay
// times, which are random variables for variety
march(random(20,50));
break;
case 2:
alternate(random(80,100));
break;
case 3:
bullets(random(50,80));
break;
case 4:
radiate(random(80,100));
break;
case 5:
negmarch(random(20,50));
break;
case 6:
stackdots(random(30,50));
break;
case 7:
smash(random(80,100));
break;
case 8:
fadeflash(random(80,100));
break;
case 9:
fadealter(random(80,100));
break;
}
}
}
}

void clearall()// sets all ports (2 through 11) to 0 Volts


{
for (int pin = 2; pin <=11; pin++)
{
digitalWrite(pin,LOW);
}
}

void fillall()// sets all ports (2 through 11) to ~5 Volts


{
for (int pin = 2; pin <=11; pin++)
{
digitalWrite(pin, HIGH);
}
}

// the 'march' function marches a lit LED back and forth across the
// LED line array
void march(int delaytime)
{
for(int pin = 2; pin <=11; pin++)
{
clearall();
digitalWrite(pin, HIGH);
delay(delaytime);
}
for(int pin = 10; pin >= 2; pin--)
{
clearall();
digitalWrite(pin, HIGH);
delay(delaytime);
}
}

// this function is the same as march, but insead an unlit LED marches
// across a lit LED line array
void negmarch(int delaytime)
{
for(int pin = 2; pin <=11; pin++)
{
fillall();
digitalWrite(pin, LOW);
delay(delaytime);
}
for(int pin = 10; pin >= 2; pin--)
{
fillall();
digitalWrite(pin, LOW);
delay(delaytime);
}
}

// this alternates between all lit and all off, used to show reading
// across the analog ports
void flash(int delaytime)
{
for(int i = 1; i <=5; i++)
{
clearall();
delay(delaytime);
fillall();
delay(delaytime);
}
}

// This is a slower, analog version of the flashing function, that


// gradually dims and brightens the whole line array
void fadeflash(int delaytime)
{
clearall();
int newdelay = delaytime / 5;
for(int fade = 0; fade <= 255; fade += 5)
{
for(int pin = 2; pin <= 11; pin++)
{
analogWrite(pin, fade);
}
delay(newdelay);
}
for(int fade = 255; fade >= 0; fade -= 5)
{
for(int pin = 2; pin <= 11; pin++)
{
analogWrite(pin, fade);
}
delay(newdelay);
}
}

// this is the same as the fadeflash function, but every other LED
// either dims or brightens
void fadealter(int delaytime)
{
clearall();
int newdelay = delaytime / 5;
for(int fade = 0; fade <= 255; fade += 5)
{
for(int i = 2; i <= 10; i+=2)
{
analogWrite(i, fade);
}
for (int j = 3; j <= 11; j += 2)
{
analogWrite(j, 255-fade);
}
delay(newdelay);
}
for(int fade = 255; fade >= 0; fade -= 5)
{
for(int i = 2; i <= 10; i+=2)
{
analogWrite(i, fade);
}
for (int j = 3; j <= 11; j += 2)
{
analogWrite(j, 255-fade);
}
delay(newdelay);
}
}

// This is the same as the flash function, but every other LED either
// turns on or off alternately
void alternate(int delaytime)
{
for (int n = 1; n <= 5; n++)
{
clearall();
for (int i = 2; i <= 10; i += 2)
{
digitalWrite(i, HIGH);
}
delay(delaytime);
clearall();
for (int j = 3; j <= 11; j += 2)
{
digitalWrite(j, HIGH);
}
delay(delaytime);
}
}

// This functions marches a series of "bullets" across the LED line array
void bullets(int delaytime)
{
for (int n = 2; n <= 26; n++)
{
clearall();
if (n < 13)
{
drawbullet(n);
}
if (n >= 8 && n < 21)
{
drawbullet(n - 6);
}
if (n >= 14 && n < 29)
{
drawbullet(n - 12);
}
delay(delaytime);
if (n >= 22)
{
drawbullet(n - 18);
}
}
}
// this is a sub function of the bullets function to minimize code.
void drawbullet(int pos)
{
switch(pos)
{
case 2:
digitalWrite(2, HIGH);
break;
case 3:
digitalWrite(2, HIGH);
analogWrite(3, round(255/10));
break;
case 12:
digitalWrite(10, HIGH);
analogWrite(11, round(255/10));
break;
case 13:
digitalWrite(11, HIGH);
break;
default:
digitalWrite(pos, HIGH);
analogWrite(pos-1, round(255/10));
analogWrite(pos-2, round(255/20));
break;
}
}

// This makes a "radiating" pattern of LEDS eminate from the center and
// they dim as they approach the ends
void radiate(int delaytime)
{
for(int n = 7; n < 28; n++)
{
clearall();
if(n < 12)
{
drawray(n);
}
if(n >= 10 && n <= 14)
{
drawray(n - 3);
}
if(n >= 13 && n <= 17)
{
drawray(n - 6);
}
if(n >= 16 && n <= 20)
{
drawray(n - 9);
}
if(n >= 19 && n <= 23)
{
drawray(n - 12);
}
if(n >= 22 && n <= 26)
{
drawray(n - 15);
}
delay(delaytime);
}
}

// This is a subfunction of the radiate function


void drawray(int pos)
{
int factor = 5;
analogWrite(pos, round( 255*( (pow(7,factor))/(pow(pos, factor)))));
analogWrite(2 + (11-pos),round( 255*( (pow(7,factor))/(pow(pos, factor)))));
}

// This function is like the march, except the marching LEDs appear to
// stack up at the end of the array one by one.
void stackdots(int delaytime)
{
int stack = 0;
while(stack < 10)
{
for(int pos = 2; pos <= (11 - stack); pos++)
{
clearall();
digitalWrite(pos, HIGH);
drawstack(stack);
delay(delaytime);
}
stack++;
}
}

// this is a subfunction of the stackdots function


void drawstack(int stack)
{
for(int n = 11; n > (11 - stack); n--)
{
if(n >= 2)
{
digitalWrite(n, HIGH);
}
}
}

// This is my favorite, it puts two LEDs in a random position with


// random "velocities," and they bounch off the ends and each other
void smash(int delaytime)
{
int div = 20;
int flashtime = delaytime / div;
int A =random(2,7);
int B =random(7,12);
int Av = 1;
int Bv = 1;
if(random(0,2))
{
Av *= -1;
}
if(random(0,2))
{
Bv *= -1;
}

for(int time = 1; time < 50; time++)


{
if(abs(A-B) == 1 && (Av*Bv) == -1)
{
for(int f = 1; f <round(div/4); f++)
{
clearall();
delay(flashtime);
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
delay(flashtime);
}
Av *= -1;
Bv *= -1;
A += Av;
B += Bv;
}
else
{
clearall();
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
A += Av;
B += Bv;
delay(delaytime);
}
if(A < 2)
{
A = 3;
Av *= -1;
}
if(B > 11)
{
B = 10;
Bv *= -1;
}
if(A >= B)
{
A = B-1;
}
}
}

Das könnte Ihnen auch gefallen