Try to hit the button just as the moving pixel passes the center pixel. Level up and increase your speed. How fast can you go?

/* * Reaction game- press the button when the moving pixel overlaps * the center one. Speed increases each time you succeed */ //Include the LEDStrip library to use the commands #include "LEDStrip.h" const byte numPixels = 15; //Number of pixels on a strip const byte middlePixel = numPixels/2; //The pixel at the middle of your strip int pixel = 0; //Position of the pixel on the strip const int hitColor = 50; //The color of the strip when you hit the mark const int missColor = 100; //The color of the strip when you miss the mark int Direction = 1; //Direction of travel of the pixel int delayTime = 300; //Starting time (in ms) between pixel movements long timer = millis(); //Timer to control movement of pixel /* * Make the LED strip object LEDStrip strip * = LEDStrip(numPixels, dataPin (DI), clockPin (CI)); */ LEDStrip strip = LEDStrip(numPixels, 13, 12); void setup() { //Button pinMode(A5,INPUT_PULLUP); } void loop() { //If at the center pixel if (pixel == middlePixel){ strip.setPixel(pixel,(hitColor+missColor)); //overlap the colors //If the button is pressed while on the center pixel if (digitalRead(A5) == LOW){ delayTime= delayTime*.75; //delayTime reduced by 25% //Blink the hitColor to indicate success strip.setPixel(strip.ALL,hitColor); strip.draw(); delay(1000); strip.clear(); } } //When not on the center pixel if (pixel != middlePixel){ strip.setPixel(pixel,missColor); //Set the moving pixel strip.setPixel(middlePixel,hitColor); //Set the center pixel //If button pressed when not at center if (digitalRead(A5) == LOW){ //Blink all pixels the missColor for a second strip.setPixel(strip.ALL,missColor); strip.draw(); delay(1000); strip.clear(); delayTime = delayTime + 100; //Slow down the ball } } //Every delayTime many ms, update pixel position if (millis()-timer > delayTime){ timer = millis(); //Reset the timer strip.setPixel(pixel,-1); //Clear previous pixel pixel = pixel + Direction; //Move the pixel //If at either end of the strip, change direction of travel if ((pixel == 0)||(pixel == numPixels-1)){ Direction = Direction*-1; } } strip.draw(); //Draw what was written to the strip this loop } //Change the reduction to delayTime time to make the game move more // quickly //(c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

First include the LEDStrip library. This library gives you access to a new set of commands (called methods) that make using the LED Strip easier. Without including the library, the Arduino software won’t recognize the new methods.

Next, create an integer variable called numPixels. Its value should be equal to the number of LEDs on your strip. 

The middlePixel variable is an integer based on your numPixels variable. Integers truncate (cut off) all decimal places, so if you have 15 pixels, 15/2 will be an integer value of 7. 

Your pixel variable holds the number of the active pixel that the code is modifying.

The hitColor variable is a color value between 0 and 300. It will show up when you score a point on the game

The missColor variable is a color value between 0 and 300. It will shop up when you miss a goal. It’s also used as the color of your player throughout the game. 

The Direction variable tells whether the pixel is moving toward the tip of the strip or toward the base.  -1 means the pixel is moving toward the base of the strip.

Your delayTime variable holds the delay value between pixels being lit up. The lower the delay, the faster the ball moves. 

The timer variable is a ‘long’ variable. Long variable types can hold very large numbers. This variable is set equal to millis() (an automatic counter). Since timer will hold the count of milliseconds later in the code, it should be a long variable type. 

To tell the library the name & attributes of your LED Strip, you create an object. In this case, the object is called ‘strip’ and it has ‘numPixel’ pixels, the data pin is number 11, and the clock pin is number 12 on your carrier board.

In the setup, set a button pin as INPUT_PULLUP pin mode. This means it will send Maker Board a LOW signal when the button is being pressed. 

The loop is checking the position of the pixel & the status of the button and reacting with colors and changes to the delay.

The first if statement in the loop checks to see if the value of pixel matches the value of middlePixel. If that is true, then that pixel color is changed to the sum of hitColor and missColor.

The next if statement is nested within the first. That means it is only checked if the first if statement is true. If the button is pressed while the pixel is in the middle of the strip, the delayTime is updated to 75% of what it previously was, meaning it will move faster.  Next, the entire strip (strip.ALL) is lit up for 1 second as the hitColor.

Now both of the if statements close with curly braces. 

The next if statement is true when pixel is not equal to middlePixel. When that is true, the pixel is set to missColor and the middlePixel is set to hitColor. The if statement that checks the button press is nested inside the if statement that checks the pixel’s position. If the button is pressed and the pixel is not in the middle, the entire strip(strip.ALL) is set to missColor and flashed for one second. Then 100 is added to the delayTime to slow the pixel down again.

The next if statement is all about timing. Millis() is an automatic timer: it starts counting up as soon as Maker Board starts running the code. When Maker Board has run for 1 second, millis() is 1000. When it has run for a minute, millis() is 60000. This if statement asks “if I subtract the timer variable from millis(), is it greater than the delayTime variable?”. If that is true, the timer variable is reset to the current value of mills(). That means that millis()-timer would now equal 0. 

After the timer is reset, the current pixel is set to clear (color value -1) and the pixel variable is updated by “Direction”. If Direction is 1, the pixel moves up the strip. If Direction is -1, the pixel moves down the strip.

The final nested if statement checks to see if pixel has reached either the base or the tip of the strip. If it has, Direction is reversed. 

Finally, the strip.draw() command sends all of the strip.setPixel commands to the strip so that you can see the update.