Do Everything! Reset Code Lab to its original program
This project is a bit different from the others, because it's not really a project at all! The code below is what was originally on your Code Lab. We use this code to test all of the features of the Code Lab to make sure they're working perfectly. You can upload the program below to your Code Lab to effectively "reset" it to the way it was when you first received it.
Code
The code in the editor below is ready to run! Plug your Code Lab in to your computer's USB port with the cable and hit 'Upload Your Code!' to see what it does. Change something in the code, like a delay or pin number. Try to add something new to the program - it's yours to tinker with! You can always press the 'Restore' button to return the code to the working example.
#include "LEDStrip.h" //The LEDStrip.h library gives your code access to special functions exclusive to the LED Strip
LEDStrip strip = LEDStrip(50, 13, 12); //Set up the strip by defining how many pixels (15) and where the wires are connected (green = 13, blue = 12)
int pressCount = 0; //Variable that increases while button 11 is held. Light sensor won't trigger random lights until pressCount > 1.
void setup(){
Serial.begin(9600);
//LEDs
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(A5,OUTPUT);//speaker
pinMode(A0,INPUT);//trimpot
pinMode(A2,INPUT);//light
//buttons
pinMode(8,INPUT_PULLUP);
pinMode(9,INPUT_PULLUP);
pinMode(10,INPUT_PULLUP);
pinMode(11,INPUT_PULLUP);
}
void loop(){
//Set the entire LED strip to the color set by the turn knob
int color = analogRead(A0); //Create a variable color that is an integer (whole number) and uses the analogRead of port A2 as its starting value
color = map(color,0,1023,0,299); //Because analogRead has a range of 0-1024 and the color range is only 0-299, use 'map'
strip.setPixel(strip.All,color,20);
strip.draw();
//When button 8 pressed, turn off LED strip and play a random tone and light LED 2
while(digitalRead(8)==LOW){
digitalWrite(2,HIGH);
strip.clear();
strip.draw();
tone(A5,random(0,1000));
delay(10);
}
digitalWrite(2,LOW);
//When button 9 pressed, turn off LED strip and play a rising tone and light LED 3
while(digitalRead(9)==LOW){
strip.clear();
strip.draw();
digitalWrite(3,HIGH);
for(int i=0;i<256;i++){
tone(A5,i*2);
delay(3);
}
}
digitalWrite(3,LOW);
//When button 10 pressed, turn off LED strip and play a falling tone and light LED 4
while(digitalRead(10)==LOW){
digitalWrite(4,HIGH);
strip.clear();
strip.draw();
for(int i=700;i>500;i--){
tone(A5,i);
delay(3);
}
}
digitalWrite(4,LOW);
//When button 11 pressed, turn off LED strip and play 3 rising tones and light LED 5,6,7 in sequence
while(digitalRead(11)==LOW){
strip.clear();
strip.draw();
digitalWrite(5,HIGH);
tone(A5,900);
delay(300);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
tone(A5,700);
delay(300);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
tone(A5,500);
delay(300);
pressCount++; //Increase pressCount when this button is pressed. The light sensor can only trigger random colors after pressCount > 1.
//The pressCount variable deals with the issue where overhead lights were flickering, causing random-looking flickers.
digitalWrite(7,LOW);
}
Serial.println(analogRead(A3));
//When there is high sound on the sound sensor, light every other pixel to color value 200
while(analogRead(A3)>750){
for(int i = 0;i<=49;i++){
strip.setPixel(i,i*6);
strip.draw();
}
delay(1000);
}
if(pressCount > 0){//If button 11 has been pressed
//When there is low light on the light sensor, set random LED pixels to random colors
while(analogRead(A2)<50){
strip.setPixel(random(0,50),random(0,299));
strip.draw();
delay(5);
}
}
noTone(A5); // Turn off all tones on the speaker
}
//How to Check all the parts of the Code Lab:
// Turn knob A0 all the way from left to right slowly. All 50 LED pixels should fade from red to, purple,blue, green, yellow, red.
//Hold down button 8. The LED pixels will turn off. The red LED #2 will turn on. Random tones will play quickly from the speaker.
//While holding down button 8, turn the "Volume" knob all the way from one side to another to adjust the noise level.
//Hold down button 9. The LED pixels will turn off. Blue LED #3 will turn on. A rising tone will come from the speaker and the tone will repeat as long as the button is held.
//Hold down button 10. The LED pixels will turn off. Green LED #4 will turn on. A falling tone will come from the speaker and the tone will repeat as long as the button is held.
//Hold down button 11. The LED pixels will turn off. The multicolor LED marked "5 6 7" will flash Red, Green, Blue light in order. The speaker will play three separate tones.
//Hold your hand over the area that says "A2" and "Light Sensor". When there is no light hitting the light sensor, random LED pixels will begin to show random colors.
//Lightly tap the round metal and black piece between "A3" and "Sound Sensor" with your finger. When the sensor registers a sound, the LED pixels will turn to every color of the rainbow. It can take 2 or 3 attempts for the sensor to sense the sound.
It looks like your computer isn't set up to code yet.
Success!
Compile errors:
Serial port monitor:
Input:
Walkthrough Video
No video here! This program is more for demonstration and a "reset" than for teaching a concept. But you can still feel free to tinker and play with it!
Use this spectrum to estimate the color values you'll need for your 'setPixel' arguments to achieve your favorite color!
Challenges
No challenges here! This program is more for demonstration than learning,but we're sure you can find something to change!
Concepts
These are the new code concepts covered in this example program. To become a great coder, read through these concepts to learn new vocabulary.
Quiz
If you're having trouble, try to run an experimental program or look at the example code to help you find the answer.
1. Which syntax ends the entire 'void loop' function?