Hello all,
I am using an Arduino UNO to control a strip of WS2811 leds with the fastled library. My goal is to create a game similar to the classic Cyclone arcade game. A single light will cycle across the strip and the user must press a button to stop the light on a defined led address.
I found some fantastic code to get me started; however, I am having trouble adapting the code to be used with a push button. The code I am going off of is using serial input to switch between "game on" and "game off" states in the loop. I know there is a lot going on here. I would greatly appreciate any input to help point me in the right direction. Should I abandon this template?
Here is the code I am going off of: http://www.instructables.com/id/Arduino-Cyclone-Arcade-Game/
Direct link: https://drive.google.com/file/d/0B3o406ZX4A4qRHV5c1Vjd1dyYlE/view
And here is my attempt to revise the script to work with a push button:
#include "FastLED.h"
//up to 50
#define NUM_LEDS 30
#define CENTER_LED 14
#define DATA_PIN 7
#define LED_TYPE WS2811
#define COLOR_ORDER RGB
//range 0-64
#define BRIGHTNESS 32
//Definition of difficulty levels
#define EASY 1
#define MEDIUM 2
#define HARD 3
#define ON_SPEED 4
#define SONIC_SPEED 5
#define MISSION_IMPOSSIBLE 6//Starting difficulty
int difficulty = 1;
// Define the array of leds
CRGB leds[NUM_LEDS];
// Did player win this round?
bool correct = false;
// Starting location of the cycling light
int LEDaddress = 0;
// Is game running?
bool gameOn = true;
// Is this the first game upon powering on?
bool firstGame = true;
// Button details
const int buttonPin = 2;
int buttonState = 0;
const int ledPin = 13;// Initialize the led library and arduino functions
void setup()
{
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Easy");
Serial.println("Hz");
}// The meat and potatoes
void loop()
{
buttonState = digitalRead(buttonPin);
if(gameOn)
{
for (int i = 0; i < NUM_LEDS; i++)
{
leds = CRGB::Black; //Turns off all the leds
- }*
- leds[CENTER_LED] = CRGB::Green; //Sets center led color to green*
- leds[LEDaddress] = CRGB::Red; //Sets cyling led color to red*
- FastLED.show(); //Initializes light cycle*
- LEDaddress++; //Sets light cycle to one led at a time*
- if (LEDaddress == NUM_LEDS)*
- {*
- LEDaddress = 0;*
- }*
- digitalRead(buttonPin); //Check to see state of button*
- checkSerial(); // Check for player input*
- delay(getTime(difficulty));*
- }*
- else //Incorrect*
- {*
- for (int i = 0; i < NUM_LEDS; i++)*
- {*
_ leds = CRGB::Black;_
* }*
* leds[CENTER_LED] = CRGB::Red;
_ leds[LEDaddress] = CRGB::Red; _
_ FastLED.show();_
_ if (firstGame)_
_ {_
int diff = abs(CENTER_LED - LEDaddress); //Finds distance between the lit led and the center led*
* if (diff == 0)*
* {*
* correct = true; //Player sucessfully beat the level*
* }*
* firstGame = false;*
* }*
* checkSerial(); // Check for player input*
* digitalRead(buttonPin); //Check to see state of button*
* }*
}
int getTime(int diff) // Returns time delay for led movement base on difficulty
{
* int timeValue = 0;*
* switch (diff)*
* {*
* case EASY:*
* timeValue = 100;*
* break;*
* case MEDIUM:*
* timeValue = 80;*
* break;*
* case HARD:*
* timeValue = 60;*
* break;*
* case ON_SPEED:
_ timeValue = 40;_
_ break;_
case SONIC_SPEED:
_ timeValue = 30;_
_ break;_
case MISSION_IMPOSSIBLE:
_ timeValue = 20;_
_ }_
_ return timeValue;// Return the delay amount*_
}
//Pushbutton parameters
void checkSerial()
{
* if(buttonState == HIGH)*
* {*
* digitalWrite(ledPin, HIGH); //THIS IS THE PART I AM UNSURE ABOUT*
* if (!gameOn)*
* {*
* LEDaddress = 0;*
* firstGame = true;*
* gameOn = true;*
* increaseDifficulty;*
* correct = false;*
* }*
* else*
* {*
* gameOn = false;*
* }*
* }*
}
void increaseDifficulty()
{
* if (difficulty != MISSION_IMPOSSIBLE && correct)
_ {_
_ difficulty++;_
_ }_
_}*_[/quote]
I am able to stop the led in its cycle with the first push button press. However, when pressing the push button a second time (to reset the cycling led) it gets stuck on the second led. I first thought it was due to button noise, but a debounce script did not solve the issue.
If anyone may provide me guidance or a tip of what direction to go in, I would greatly appreciate your input. This script may just not work for what I'm trying to do. Thank you for your time!