Go Down

Topic: Need some help With coding a ws2812b project (Read 1 time) previous topic - next topic

lesept

#15
Aug 12, 2018, 11:19 pm Last Edit: Aug 12, 2018, 11:47 pm by lesept
Before each line delay (vardelay) ; you add the lines

vardelay =map(count, 0,maxnumber,mindelay,maxdelay);
count ++;

And of course you defined
int count =0;
int maxnumber =7*24+spinit;
at the beginning of outerspin

And
#define maxdelay 500
#define mindelay 50
At the beginning of your code. You can change these values as you wish.
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

lesept

#16
Aug 12, 2018, 11:42 pm Last Edit: Aug 12, 2018, 11:49 pm by lesept
Here is how I would deal with the first part of the program, to choose the bet

Code: [Select]
int selectinner () {
    int oldbet = 9;
    do {
        int potvalue = readpot();
        int bet = map(potvalue,0,1023,0,7);
        if (oldbet!=bet) {
            oldbet = bet;
            FastLED.clear();
            leds[bet] = CRGB::Blue;
            FastLED.show();
            delay(20);
        }
    } while (button)
    return bet;
}

int readpot() {
    int potvalue = analogRead(pot);
    for (int i=0;i<5;i++) {
        potvalue += analogRead(pot);
        delay(20);
    }
    return potvalue / 5;
}
It waits for a button press to return the chosen value. The player can then change the bet until he decides to press the button.

You can call this by
Code: [Select]
int bet = selectinner () ;

I didn't test it nor tried to compile, so there may still be some mistakes in this code...

It assumes the bet button, called button here (you may want to change the name according to how you called it) is connected as input_pullup. If you don't know what it is, you can easily find explanations on the net.
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

lesept

FastLED has a special way for dealing with multiple strips, see here https://github.com/FastLED/FastLED/wiki/Multiple-Controller-Examples

FastLED.show displays all your different strips at the same time. Fortunately your strips are never in use at the same moment, so you don't have any problem.

Anyway you may want to read that web page.
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

lesept

#18
Aug 13, 2018, 04:49 pm Last Edit: Aug 14, 2018, 08:01 am by lesept
And here is how I would do it
Code: [Select]
#include <FastLED.h>
#define DATA_PIN 2
#define DATA_PIN2 3
#define DATA_PIN3 4
#define NUM_LEDS 8
#define NUM_LEDS2 24
#define NUM_LEDS3 5
#define pot A1
CRGB leds[NUM_LEDS];
CRGB leds2[NUM_LEDS2];
CRGB leds3[NUM_LEDS3];
const byte button = 6;

void setup() {
    pinMode (button,INPUT_PULLUP);
    FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
    FastLED.addLeds<NEOPIXEL, DATA_PIN2>(leds2, NUM_LEDS2);
    FastLED.addLeds<NEOPIXEL, DATA_PIN3>(leds3, NUM_LEDS3);
    Serial.begin(9600);
}

void loop () {
    FastLED.clear();
    FastLED.show();
    int bet = selectinner();
    int wheel = outerwheel ();
    if (bet==wheel/3) win();
    else lose();
    while (button) {}
}

int selectinner () {
    int bet;
    int oldbet = NUM_LEDS;
    do {
        int potvalue = readpot();
        bet = map(potvalue,0,1023,0,NUM_LEDS-1);
        Serial.print (potvalue);
        Serial.print ("\t");
        Serial.println (bet);
        if (oldbet!=bet) {
            oldbet = bet;
            FastLED.clear();
            leds[bet] = CRGB::Blue;
            FastLED.show();
    //        delay(20);
        }
    } while (button);
    return bet;
}

int readpot() {
    int potvalue = analogRead(pot);
    for (int i=0;i<4;i++) {
        potvalue += analogRead(pot);
        delay(20);
    }
    return potvalue / 5;
}

int outerwheel () {
    const int mindelay = 50;
    const int maxdelay = 500;
    const byte spins = 7;
    int spinIt = random(255);
    spinIt = map (spinIt, 0, 255, 0, NUM_LEDS2-1);
    int maxnum = spins * NUM_LEDS2 + spinIt;
    int count = 0;
    for (byte i=0;i<spins;i++) {
        for (byte j=0;j<NUM_LEDS2;j++) {
            byte prevled = j-1;
            if (prevled<0) prevled = NUM_LEDS2 - 1;
            leds2[prevled] = CRGB::Black;
            leds2[j] = CRGB::Cyan;
            FastLED.show();
            count++;
            int vardelay = map(count,0,maxnum,mindelay,maxdelay);
            delay(vardelay);
        }
    }
    for (byte j=0;j<spinIt;j++) {
        byte prevled = j-1;
        if (prevled<0) prevled = NUM_LEDS2 - 1;
        leds2[prevled] = CRGB::Black;
        leds2[j] = CRGB::Cyan;
        FastLED.show();
        count++;
        int vardelay = map(count,0,maxnum,mindelay,maxdelay);
        delay(vardelay);
    }
    return spinIt;
}

void win() {
    for (int i=0;i<NUM_LEDS3;i++) leds3[i] = CRGB::Green;
    FastLED.show();
}

void lose () {
    for (int i=0;i<NUM_LEDS3;i++) leds3[i] = CRGB::Red;
    FastLED.show();
}

Again not tested nor compiled, there may be some mistakes but you can use it as you want.

Edit : it compiled
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

lesept

I forgot to add that this sketch uses only one button on pin 6 to both select the bet (spin the wheel) and reset the game
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

moJoeRedRog

#20
Aug 14, 2018, 02:49 pm Last Edit: Aug 14, 2018, 04:12 pm by moJoeRedRog
Thanks for the help on this one.  I tried your code and it has a problem with the button, it does nothing.  I looked it up online and I have it wired right but nothing happens when I press it. and another issue.  You used random which is a little not so random.  And will always repeat the same pattern, so I switched in the pRNG library and used that to randomize the spinIt.  I will post a pic of the finished project once completed.

lesept

You should wire GND - BUTTON - D6. With input_pullup it reads LOW when pressed.
I don't know prng I use the Truerandom library
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

lesept

I see. All the while (button) should be while(digitalRead (button))
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

moJoeRedRog

#23
Aug 14, 2018, 05:35 pm Last Edit: Aug 14, 2018, 05:44 pm by moJoeRedRog
I see. All the while (button) should be while(digitalRead (button))
yes that worked.  Another issue it does not reset, pressing the button after a spin will start it over with out a chance to change the bet.  I will try to add a reset button.  The pRNG library is just as easy instead of calling random you just input pRNG.getRndByte(); and it returns a true random number between 0 and 255

lesept

It must be because of bounced of the button
Just add
delay(100);
after the while line of the loop

If it doesn't solve the problem we must find a way of catching the button release, not the button push. Maybe you can find some code for that on the internet, my connectivity is very bad...
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

moJoeRedRog

It must be because of bounced of the button
Just add
delay(100);
after the while line of the loop

If it doesn't solve the problem we must find a way of catching the button release, not the button push. Maybe you can find some code for that on the internet, my connectivity is very bad...
Yes that fixed it.

lesept

A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

moJoeRedRog

Great, you just need to add the music
One step ahead of you I already did!  But there is one little issue, when the outer Ring is activated after the first go around the last led stays on for the remaining loops and even when it finally stops.  Is there anything we can do to correct this.

lesept

I don't understand, please explain again...
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

moJoeRedRog

#29
Aug 15, 2018, 03:54 pm Last Edit: Aug 15, 2018, 07:06 pm by moJoeRedRog
Yes, the 24th cyan Led in the outerWheel stays lit, it does not go off when the LED's go around.  When the final LED is lit the 24th LED is still lit as well. 

here is a photo of the leds.



as you can see the LED on the far upper left is always on

Go Up