Go Down

Topic: A fresh take on the Chess Turn Timer with arduino and ws2812b LED's (Read 526 times) previous topic - next topic

moJoeRedRog

Hi I am having some difficulty in coding and even wiring this simple project.  I have all my wiring except I want to use an arcade happ style button as the timers reset button.  and I want to use the 2812 to light up one after the other while time elapses.  I also have a buzzer go off when the time is done.  I only have basic coding skills and this is being a challenge I tried using millis but could not reset the lights and start again. 

also i dont know how to wire up the reset button which is a Happ with a com1 and a NC 2 and a NO 3.  so i am not sure the best way to do it.  I am using FastLed  for the ws2812b and want to keep the code functional any help with direction on this one would be appreciated.  there are 7 Leds in the strand i made.  thanks for any help on this one. 

and if the timer can be adjusted with a potentiometer that would be great lowest like 1 minute on high like 5 minutes would make the project more multi dimensional. 

I got it all wired up with my test strand lighting and my analog potentiometer wired right and for now I am using a gnd 220ohm switch to D7 and my pezio is on pwm 5 each component works with tester code but I need huge direction on this for writing the code.  8^)


blh64

No code, no schematic, no help :)

Please read the post at the top of the forum about how to post.

moJoeRedRog

Here is a fritz of the wiring.  The ws2812b is not the same model as I am using a GeekCreit and it has 3 in 3 out.  One ground one power and a data line. They are daisy chained together.  They work I have tested them.  any guidance on what the best way to program this easily would be great.  


Code: [Select]

#include <FastLED.h>
#define NUM_LEDS 7
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
const int potPin = A0;
const int resetButton = 7;
const int buzzer = 5;
int delTime = 0;


void setup() {
  // put your setup code here, to run once:
  pinMode(buzzer, OUTPUT);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

void loop() {
  // put your main code here, to run repeatedly:
  delTime = analogRead(potPin) * 10;
  delay(delTime);
  stage1();
  stage2();
  stage3();
  stage4();
  stage5();
  stage6();
  stage7();
  
}

void buzz(){
  tone(buzzer, 1000);
  delay(3000);
  noTone(buzzer);
}

void stage1(){
  leds[0] = CRGB::Purple;
        FastLED.show();
        delay(delTime);
}

void stage2(){
  leds[1] = CRGB::Purple;
        FastLED.show();
        delay(delTime);
}

void stage3(){
  leds[2] = CRGB::Purple;
        FastLED.show();
        delay(delTime);
}
void stage4(){
  leds[3] = CRGB::Purple;
        FastLED.show();
        delay(delTime);
}

void stage5(){
  leds[4] = CRGB::Purple;
        FastLED.show();
        delay(delTime);
}

void stage6(){
  leds[5] = CRGB::Purple;
        FastLED.show();
        delay(delTime);
}

void stage7(){
  leds[6] = CRGB::Purple;
        FastLED.show();
        buzz();
        delay(delTime);
}

void reset(){
    FastLED.clear();
    FastLED.show();
}


I know it does nothing but thats all I got so far, not sure how to keep track of time and light up the led's one at a time at a adjustable by the potentiometer delay rate.  that then can be reset and started over by pressing the button.

Any direction on what way to do that would be good.

Ok I updated the code it lights up at a interval controlled by the knob however I got no reset button code.  Any one got any ideas I added a clear function call reset(); but now need to get the button going to activate the reset code and star it again.


moJoeRedRog

This is my latest code attempt, cant get this button resetting the leds and starting the counter over.  If any one could help that would be awesome.

Code: [Select]

#include <FastLED.h>
#define NUM_LEDS 7
#define DATA_PIN 3
CRGB leds[NUM_LEDS];
const int potPin = A0;
const int resetButton = 7;
const int buzzer = 5;
int delTime = 0;
int val = 0;



void setup() {
  // put your setup code here, to run once:
  pinMode(buzzer, OUTPUT);
  pinMode(resetButton, INPUT_PULLUP);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

void loop() {
  // put your main code here, to run repeatedly:
  delTime = analogRead(potPin) * 10;
  delay(delTime);
  Checkbuttons();
}


void Checkbuttons(){
  bool StateBReset = digitalRead(resetButton);
  delay(50);
  if (!StateBReset) reset();
  combo();
 
}

void combo(){
  stage1();
  stage2();
  stage3();
  stage4();
  stage5();
  stage6();
  stage7();
}

void stage1() {
  leds[0] = CRGB::Purple;
  FastLED.show();
  delay(delTime);
}

void stage2() {
  leds[1] = CRGB::Purple;
  FastLED.show();
  delay(delTime);
}

void stage3() {
  leds[2] = CRGB::Purple;
  FastLED.show();
  delay(delTime);
}
void stage4() {
  leds[3] = CRGB::Purple;
  FastLED.show();
  delay(delTime);
}

void stage5() {
  leds[4] = CRGB::Purple;
  FastLED.show();
  delay(delTime);
}

void stage6() {
  leds[5] = CRGB::Purple;
  FastLED.show();
  delay(delTime);
}

void stage7() {
  leds[6] = CRGB::Red;
  FastLED.show();
  buzz();
  delay(delTime);
}

void buzz() {
  tone(buzzer, 1000);
  delay(3000);
  noTone(buzzer);
}

void reset() {
  FastLED.clear();
  FastLED.show();
}



blh64

You say your button has 3 pins but your schematic shows 4 so I'm not really sure which is correct.  What you want to typically do is connect the common to your input pin and connect the normally open (NO) to ground.  You don't need the resistor.  You also have to declare the input pin to use the internal pullup resistor (which you did!)

Then, when the button is not being pressed, it is not connected to anything so the internal pullup pulls the pin HIGH.  When the button is pressed, it is shorted to ground and reads LOW.

moJoeRedRog

You say your button has 3 pins but your schematic shows 4 so I'm not really sure which is correct.  What you want to typically do is connect the common to your input pin and connect the normally open (NO) to ground.  You don't need the resistor.  You also have to declare the input pin to use the internal pullup resistor (which you did!)

Then, when the button is not being pressed, it is not connected to anything so the internal pullup pulls the pin HIGH.  When the button is pressed, it is shorted to ground and reads LOW.
do you have any ideas how to get the code to run the "void stages1, stages2, etc.. functions" while monitoring the button to activate a reset function.  that would be most helpful.

MorganS

Is delTime going to stay at zero? Then remove all the lines that say delay(delTime);

If it's going to be bigger than about 50 in the future, then you need to do this a totally different way, which involves removing all the lines that say delay(delTime);

Go to your Arduino IDE and open up the Examples. Look at the first Digital example: BlinkWithoutDelay. Make that work with the built-in LED. Make it work with one of your WS218 strips. Note how it uses the variable ledState to remember where it is up to in the cycle? You need to do something similar except you will have more states than just on and off.
"The problem is in the code you didn't post."

moJoeRedRog

Is delTime going to stay at zero? Then remove all the lines that say delay(delTime);

If it's going to be bigger than about 50 in the future, then you need to do this a totally different way, which involves removing all the lines that say delay(delTime);

Go to your Arduino IDE and open up the Examples. Look at the first Digital example: BlinkWithoutDelay. Make that work with the built-in LED. Make it work with one of your WS218 strips. Note how it uses the variable ledState to remember where it is up to in the cycle? You need to do something similar except you will have more states than just on and off.
Hey man thanks for your interest.  delTime is adjustable with the potentiometer and changes the speed at which the strip lights up changing the "time" it takes for the players turn to run out and the buzzer goes off.  I am open for accomplishing this any way.  
I will look into that example and see if it helps.

moJoeRedRog

Ok thanks for the tip, I have made some progress using some code another member wrote for me on another project and the blink with out delay example.  If you could review the code and suggest how I can make the buzzer sound that would be great and I need to find a way to change the interval with a potentiometer.  The button kind of works some of the time. 
Code: [Select]


#include <FastLED.h>
#include <JC_Button.h>          // https://github.com/JChristensen/JC_Button
#define PIN_STRIP1 3
#define COLOR_ORDER RGB
#define LED_TYPE WS2812
#define NUM_LEDS 7
uint8_t max_bright = 255;
struct CRGB leds1[NUM_LEDS];
int ledColor1[7] = {CRGB::Blue,CRGB::Green,CRGB::Blue,CRGB::Blue,CRGB::Green,CRGB::Blue,CRGB::Blue}; // Color for strip 1
const byte PIN_BUTTON_RESET (7);
Button BUTTON_RESET(PIN_BUTTON_RESET);
byte NumStrip1 = 0;
const int potPin = A0;
long interval = 2000;//analogRead(potPin) * 10;
unsigned long previousMillis = 0;


void setup() {
  // put your setup code here, to run once:
  LEDS.addLeds<LED_TYPE, PIN_STRIP1, COLOR_ORDER>(leds1, NUM_LEDS);
  FastLED.setBrightness(max_bright);
  FastLED.clear();
  FastLED.show();
  BUTTON_RESET.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
   unsigned long currentMillis = millis();
 if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
BUTTON_RESET.read(); // read Reset BUTTON
  if (BUTTON_RESET.wasPressed()) ResetStrips ();
  else ChangeStrip1();
 
}
}


void ChangeStrip1 () {
  leds1[NumStrip1] = ledColor1[NumStrip1];
  NumStrip1 = min(NumStrip1 + 1, NUM_LEDS - 1);
  FastLED.show();
}

void ResetStrips () {
  FastLED.clear();
  NumStrip1 = 0;
  FastLED.show();
}


MorganS

Quote
Code: [Select]
if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
BUTTON_RESET.read(); // read Reset BUTTON
  if (BUTTON_RESET.wasPressed()) ResetStrips ();
  else ChangeStrip1();
 
}
So when does it check the button? Only once per interval. Remove that from the interval and check it every loop.

Code: (I also fixed the indenting) [Select]
if (currentMillis - previousMillis >= interval) {
  // save the last time you blinked the LED
  previousMillis = currentMillis;
  ChangeStrip1();
}

BUTTON_RESET.read(); // read Reset BUTTON
if (BUTTON_RESET.wasPressed()) {
  ResetStrips ();
  previousMillis = currentMillis;
}
"The problem is in the code you didn't post."

moJoeRedRog

That worked great.  Button is sharp and always works.  any way I could add this buzzer code to come on after the 7th led lights up

Code: [Select]

void buzz(){
  tone(buzzer, 1000);
  delay(3000);
  noTone(buzzer);
}


I tried adding this buzz code but it only works when its on led 6 or under on 7 it wont sound the buzzer.

Code: [Select]

void ChangeStrip1 () {
  leds1[NumStrip1] = ledColor1[NumStrip1];
  NumStrip1 = min(NumStrip1 + 1, NUM_LEDS - 1);
  FastLED.show();
  if (NumStrip1 == 7){
  buzz();
}
}


I figured it out.  Now one last piece of the puzzle is to use a analogRead to change the interval. :)
Any idea on how to make the interval adjustable with the pot, I am really scratching my head on that. I tried passing it the value and multiplying it by 10 but it just makes the strip instantly light up.

I am using a 10k pot and serial monitor is registering a value from 0 to 1023

Johan_Ha

Just a friendly note. Please, try to write proper English with punctuation and upper case letters where needed. It helps us help you.
____________________

If you ask for help and write 'u' instead of 'you' because you think it's convenient, I will write 'no' instead of 'yes'. For same reasons.

MorganS

Where did you put the analogRead()? I'd put it in loop() so it's instantly adjustable at any time.
"The problem is in the code you didn't post."

cyclegadget


 When you put analogRead() in your loop you may want to use something like this that is found on the Arduino Reference page. It is called the map() function.

https://www.arduino.cc/reference/en/language/functions/math/map/
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

moJoeRedRog

Where did you put the analogRead()? I'd put it in loop() so it's instantly adjustable at any time.
That was what I needed.  Loop it is and the prototype is done, thanks a bunch for the great advice.!

Go Up