Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15514

ISO Non-Blocking Pause in Loop or something even more magical

$
0
0

Hi All,
First post, but DEFINITELY not my first time here...ive been using these boards religiously with this project...so before anything, I would like to sincerely thanks EVERYONE who shares their hard earned expertise so freely. I appreciate you all...

That said, this is actually ROUND 2 for this project...

BACKSTORY INCOMING FEEL FREE TO SKIP DOWN*
In 2016, at the height of my "Maker Phase" I took on a project for work, to create a Plinko board as an employee recognition game...This was my first dive into the arduino (and coding really) world and although i was able to pull it off, I knew I had only scratched the surface...

Miraculously (with extensive help from the online community, again thank you all)...I was able to pull together a 5' tall plinko game, complete with DMD scoreboard, LED prize lights, and sound...All that was split between 2 arduino Unos, and an audio shield...

The game was used weekly for a few years, then was shelved in 2020 due to the pandemic, and crowds of employees cheering on their co-workers just didnt happen anymore...

Fast forward to Dec 2023, and I saw an opportunity to take the plinko game home, and thought it would be fun to donate to my son's cub scout pack so they could use it at their annual carnival as a fundraising game.

Unfortunately, during the years of storage during pandemic, the warehouse staff was less that careful and more than a few things broke and needed to be replaced...including the arduinos themselves...

As fate would have it, I actually caught covid last week...so since Im isolated in the living room (and "unable" to help with anything in the house) I dedicated this weekend to salvaging the game to its former glory (sans sounds...decided to spare the booth workers from hearing the same sound bite over and over...and over...again)

BACKSTORY FIN - more important stuff below*

That said, I have condensed everything (1 DMD, 4 light panels, and 8 prize triggers), into a single Arduino Uno Board...and it actually works!

CRITICAL NOTE: the triggers i designed are very simple contact points...the plinko game puck falls into the the prize slot, and the metal band around the perimeter connects the 2 contact points completing the circuit and triggering the light panel and scoreboard...this means that as long as the circuit is closed, the arduino reads HIGH and continues running the loop...

Gameplay Note: players get 3 drops to build up their score

And therein lies my problem...again, thinking of the game play at a carnival...i dont want the puck to keep adding points to the score while the attendant takes their time removing it from the prize slot...I want the game to run the loop (but then pause for 20 seconds before allowing the score to be increased)

I have tried the delay function in every place i could think of...but one of a few things always happens:

  • the delay causes the score to not activate when the puck lands (waits for the delay to end)
  • the delay causes the score to add but stops 1 short until the delay finishes
  • the delay causes issues with the DMD and things get funky

What I WANT to happen is

  • game turns on (player gets 3 drops to build up their score)
  • player drops puck into 1 of 8 prize slots
  • Light of non selected slots turn off, but the selected slot light stays on
  • value of slot selected is added to DMD (shows number increasing from 0, or last score)
  • new score shows on DMD
  • non selected lights stay off, and score doesnt increase further, until X time occurs allowing the game attendant to get the puck off of the slot
  • After X time occurs, all light turn back on, indicating that the game is now ready to accept the next puck drop
  • reset button will put the score back to 0 for the next player

Unless someone has a brilliant other idea on how to achieve this...Im guessing this is where the millis function comes in...however, from what im reading, that seems to be a bit more involved than I think i can handle at the moment...

Really hoping that my well written backstory above is awesome enough for some master level code writer to take pity on me and just help me knock this out...Im all for learning, but im also very tired...and want this out of my garage...

That said, i will gladly take any help i can get...thanks in advance

Here is the code:


#include "SPI.h"
#include "DMD.h"
#include "TimerOne.h"  
#include "Arial_black_16.h"
#define LED_PIN_1 A4
#define LED_PIN_2 3
#define LED_PIN_3 4
#define LED_PIN_4 5


const int buttonPin = 2;
int buttonState = 0;
const int trigger1 = 14;
int triggerstate1 = 0;
const int trigger2 = 15;
int triggerstate2 = 0;
const int trigger3 = 16;
int triggerstate3 = 0;
const int trigger4 = 17;
int triggerstate4 = 0;
int  score;
int totalscore;
int rep1;
int rep2;
int rep3;
int rep4;
int flash1;

//Fire up the DMD library as dmd
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);



/*-----------------------------------------------------------------------
 Interrupt handler for Timer1 (TimerOne) driven DMD refresh scanning, this gets
 called at the period set in Timer1.initialize();
 -----------------------------------------------------------------------*/
void ScanDMD()
{
  dmd.scanDisplayBySPI();
}
 
const byte PanelWidth = 32;
const byte MaxStringLength = 5;
char CharBuf[MaxStringLength + 1];

// the setup routine runs once when you press reset:
void setup(void) {
  // initialize the digital pin as an output.
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
  pinMode(triggerstate1, INPUT);
  pinMode(triggerstate2, INPUT);
  pinMode(triggerstate3, INPUT);
  pinMode(triggerstate4, INPUT);
  

  
    //initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
  Timer1.initialize( 5000 );          //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
  Timer1.attachInterrupt( ScanDMD );  //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()
  dmd.selectFont(Arial_Black_16);
{
  pinMode(LED_PIN_1, OUTPUT);
  pinMode(LED_PIN_2, OUTPUT);
  pinMode(LED_PIN_3, OUTPUT);
  pinMode(LED_PIN_4, OUTPUT);

}
  
}

// the loop routine runs over and over again forever:
void loop(void) {
 
  digitalWrite(LED_PIN_1, HIGH);
  digitalWrite(LED_PIN_2, HIGH);
  digitalWrite(LED_PIN_3, HIGH);
  digitalWrite(LED_PIN_4, HIGH);


  buttonState = digitalRead(buttonPin);
  triggerstate1 = digitalRead(trigger1);
  triggerstate2 = digitalRead(trigger2);
  triggerstate3 = digitalRead(trigger3);
  triggerstate4 = digitalRead(trigger4);

    String displayString = String(totalscore);
    center_theDisplay(displayString); delay(80);
    Serial.println (totalscore);
        delay (2000);

  
  if ((triggerstate1 == HIGH) && (rep1 < 500)) {
      for(int score = totalscore ; score < (totalscore+25); score++){
        delay (20);
              digitalWrite(LED_PIN_1, LOW);
              digitalWrite(LED_PIN_2, HIGH);
              digitalWrite(LED_PIN_3, LOW);
              digitalWrite(LED_PIN_4, LOW);
         displayString = score;
         center_theDisplay(displayString); delay(20);
        Serial.println (score);
        rep1 = rep1 + 1;

      }
    totalscore = totalscore + 25;
        
      } 
   
   if ((triggerstate2 == HIGH) && (rep2 < 500)){
      for(int score = totalscore ; score < (totalscore+50); score++){
        delay (25);
              digitalWrite(LED_PIN_1, LOW);
              digitalWrite(LED_PIN_2, LOW);
              digitalWrite(LED_PIN_3, HIGH);
              digitalWrite(LED_PIN_4, LOW);
         displayString =  score;
         center_theDisplay(displayString); delay(25);
        Serial.println (score);
        rep2 = rep2 + 1;
         
      } 
    totalscore = totalscore + 50;
      }

    if ((triggerstate3 == HIGH) && (rep3 < 500)){
      for(int score = totalscore ; score < (totalscore+75); score++){
        delay (25);
              digitalWrite(LED_PIN_1, HIGH);
              digitalWrite(LED_PIN_2, LOW);
              digitalWrite(LED_PIN_3, LOW);
              digitalWrite(LED_PIN_4, LOW);
         displayString = score;
         center_theDisplay(displayString); delay(25);
        Serial.println (score);
        rep3 = rep3 + 1;
        
      
      }
    totalscore = totalscore + 75; 
      }
  
     if ((triggerstate4 == HIGH) && (rep4 < 500)){
      for(int score = totalscore ; score < (totalscore+100); score++){
        delay (25);
              digitalWrite(LED_PIN_1, LOW);
              digitalWrite(LED_PIN_2, LOW);
              digitalWrite(LED_PIN_3, LOW);
              digitalWrite(LED_PIN_4, HIGH);
         displayString = score;
         center_theDisplay(displayString); delay(25);
        Serial.println (score);
        rep4 = rep4 + 1;

      }
      
    totalscore = totalscore + 100;
    
      }
 
} 

 

void center_theDisplay(String input_Str) {
  byte charCount, total_charWidth, x_position;
  input_Str.toCharArray(CharBuf, MaxStringLength + 1); //string to char array
 
  charCount=  input_Str.length();
  if (charCount==0) exit;
 
  total_charWidth= 0;
  for (byte thisChar = 0; thisChar <charCount; thisChar++) {
    total_charWidth= total_charWidth + dmd.charWidth(CharBuf[thisChar]) +1; //add 1 pixel for space
  }  
 
  total_charWidth= total_charWidth -1; //no space for last letter
  x_position= (PanelWidth - total_charWidth) /2; //position(x) of first letter
 
 
  // output the string to the DMD Panel
 dmd.drawString( x_position, 1, CharBuf,charCount, GRAPHICS_NORMAL); 
  


  
if (buttonState == HIGH){
    dmd.clearScreen(true);
    totalscore = 0;
    rep1 = 0;
    rep2 = 0;
    rep3 = 0;
    rep4 = 0;
}
}


11 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 15514

Trending Articles