Approach for Game Code with WS2811 LEDs

Goal: User has to stop the moving light (single lit LED) on a defined LED address as it cycles through the LED strip at x frequency. The user will be using a push button to stop the cycling LED.

Modes:

  1. Playing - The light is cycling, and the user is waiting to push the button.

  2. EndGame - The user has pressed the button, and the LED has stopped on x address.

a. Won - Crazy LED sequence and difficulty level increases. Mode reverts to Playing. b. Lost - LEDs flash red and difficulty level decreases. Mode reverts to Playing.

What I need help with: I found a great script (script, instructable) that contains all the code I need to run these two modes.

However, I am having trouble incorporating the push button into the code to switch between the two modes (Playing and EndGame) in the void loop.

Any feedback will be much appreciated! I do not necessarily need code. Just ideas on what ways I may go about using the push button in the script. Thank you for your time! Also, I am using the fastled library.

I think I may be on the right track here. Going to try it out tomorrow morning.

//Cyclone Game

#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? This tag is used for difficulty parameters.
bool wonThisRound = false;

// Starting location of the cycling light
int LEDaddress = 0;

// Is game running?
bool Playing = true;

// Is this the first win?
bool CycleEnded = true;

// Button details
const int buttonPin = 2;
int buttonState = 0;

// 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);
Serial.begin(9600);
}

// The meat and potatoes
//Two Modes - Playing and End Game
void loop()
{
//END GAME
buttonState = digitalRead(buttonPin);
if (buttonState = HIGH)
{
Playing = false; //User has pressed the button, and the LED has stopped on the winning address.
for (int i = 0; i < NUM_LEDS; i++)
{
leds = CRGB::Black;

  • }*

  • leds[CENTER_LED] = CRGB::Green;*

  • leds[LEDaddress] = CRGB::Red; *

  • FastLED.show();*

  • if (CycleEnded = true)*

  • {*

  • int diff = abs(CENTER_LED - LEDaddress); //Finds distance between the lit led and the center led*

  • if (diff == 0)*

  • {*

  • wonThisRound = true; //Player sucessfully beat the level*

  • increaseDifficulty();*

  • wonThisRound = false;*

  • }*

  • else*

  • {*

  • leds[0] = CRGB::Red;*

  • FastLED.show();*

  • delay(500);*

  • leds[0] = CRGB::Black;*

  • FastLED.show();*

  • delay(500);*

  • }*

  • CycleEnded = false;*

  • }*

  • delay(2000);*

  • buttonState = digitalRead(buttonPin);*

  • if (buttonState = LOW)*

  • {*

  • Playing = true;*

  • }*

  • }*

  • //PLAYING*

  • if(Playing)*

  • {*

  • 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;_
    _
    }_
    _
    delay(getTime(difficulty));_
    _
    buttonState = digitalRead(buttonPin);_
    _
    if (buttonState = HIGH)_
    _
    {_
    _
    Playing = false;_
    _
    CycleEnded = true;_
    _
    }_
    _
    }_
    _
    }_
    _
    //Level Parameters*_
    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*_
    }
    //Winning difficulty increase parameters
    void increaseDifficulty()
    {
    * if (difficulty != MISSION_IMPOSSIBLE && wonThisRound)
    _
    {_
    _
    difficulty++;_
    _
    }_
    _
    }[/quote]*_

I think I may be on the right track here.

I don't.

  pinMode(buttonPin, INPUT);

That requires an external pullup or pulldown resistor. Do you have one? How are it and the switch wired? INPUT_PULLUP does not require an external resistor, and makes wiring so much easier.

  if (buttonState = HIGH)

After having read the state of the switch, throwing that away and assigning HIGH to the buttonState variable renders the switch useless.

Thank you for your reply, Paul. I appreciate your feedback.

That requires an external pullup or pulldown resistor. Do you have one? How are it and the switch wired? INPUT_PULLUP does not require an external resistor, and makes wiring so much easier.

I have 5V coming into one side of my NO switch and a 10K resistor to ground on the other side of the switch. I have a wire coming from the ground side of the switch to data pin 2 on the arduino.

if (buttonState = HIGH)

After having read the state of the switch, throwing that away and assigning HIGH to the buttonState variable renders the switch useless.

Are you saying the switch will be stuck in a HIGH state?

Are you saying the switch will be stuck in a HIGH state?

I'm saying that = is the assignment operator. == is the equality operator. They are NOT interchangeable.

I have 5V coming into one side of my NO switch and a 10K resistor to ground on the other side of the switch. I have a wire coming from the ground side of the switch to data pin 2 on the arduino.

A picture is worth a thousand words.

I'm saying that = is the assignment operator. == is the equality operator. They are NOT interchangeable.

Doh! I completely overlooked that. Thank you!

A picture is worth a thousand words.

https://imgur.com/qkejHGH

Edit: I also have a 470 ohm resistor in line with the data wire going from the LEDs to the data pin on the arduino.

Edit2: The code is working great with the revised equals symbols (==)! Thank you very much, Paul!