/*This program sorts the wearer of the sorting hat into their house by randomly choosing the house and blinking an RGB LED the house color You can set the color of an RGB LED by adjusting the voltage applied to the red, green and blue pins of the LED. Feel free to adjust the colors to what you like*/ int r=5; //define our red pin int g=6; //define our green pin int b=7; //define our blue pin int button=8; //define our button pin void setup() { pinMode(r,OUTPUT); //set red pin to output pinMode(g,OUTPUT); //set green pin to output pinMode(b,OUTPUT); //set blut pin to output pinMode(button,INPUT); //set button pin to output digitalWrite(button,HIGH); //turn on internal pull-up resistor } void loop() { int randomseed=analogRead(2); //create seed for the random number generator by reading the noise on pin #2 int newseed=random(randomseed); //create new seed by creating a random number using our old seed. We started doing this because Hufflepuff wasn't getting enough love! randomSeed(newseed); //generate seed while(digitalRead(button)==HIGH){ //wait until button is pushed delay(100); } shuffle(); //go to shuffle function choose(); //sort the person into their house! } /*This function blinks the LEDs through each of the house colors while the hat is "sorting"*/ void shuffle() { int randshuffle=random(10); //find random number between 0and 10 if(randshuffle==0){randshuffle=1;} //10% of the time the hats sorts after 1 second if(randshuffle>0&&randshuffle<=4){randshuffle=1;} //40% of the time the hat waits for 3 second if(randshuffle>5&&randshuffle<=9){randshuffle=3;} //40% of the time the hat waits for 5 seconds if(randshuffle>9){randshuffle=7;} //10% of the time the hat thinks for 10 seconds for(int i=1;i<=randshuffle;i++) //let the hat think { red(); //red LED delay(250); green(); //green LED delay(250); blue(); //blue LED delay(250); yellow(); //yellow LED if (i!=randshuffle) { delay(250); } } } /*this function sorts the wearer into their hourse*/ void choose() { int randchoose=random(4); //randomly choose number from 0-3 for(int i=1;i<=10;i++) { if(randchoose==0) //Gryffindor!!! { off(); delay(250); red(); delay(250); } else if(randchoose==1) //Slytherin!!! { off(); delay(250); green(); delay(250); } else if(randchoose==2) //Ravenclaw!!! { off(); delay(250); blue(); delay(250); } else //Hufflepuff!!! { off(); delay(250); yellow(); delay(250); } } } /*set RGB to red*/ void red(){ analogWrite(r,255); analogWrite(g,0); analogWrite(b,0); } /*set RGB to green*/ void green(){ analogWrite(r,255); analogWrite(g,0); analogWrite(b,255); } /*set RGB to blue*/ void blue(){ analogWrite(r,255); analogWrite(g,255); analogWrite(b,0); } /*set RGB to yellow*/ void yellow(){ analogWrite(r,30); analogWrite(g,0); analogWrite(b,255); } /*set all RGB pins off*/ void off(){ analogWrite(r,255); analogWrite(g,255); analogWrite(b,255); }