Confused about Blynk

@Moors7 I’ve added Serial.println statements for the functions and nothing is showing up on the serial terminal. How odd.

Do you have a Serial.begin(); as well? Do Serial prints work elsewhere in your code?

@Moors7 Yes, I’ve declared Serial.begin (9600); in the void setup function.

Can anyone tell me how to terminate a function and restart it again after a winner is decided?

Also, could it be that the rand function is wrong and does not generate any numbers at all?

Please help, I’ve invested too much time on this to let it fail…

Where's your current code?

How do you currently start it?

It should, if implemented correctly.

This is it:

        // This #include statement was automatically added by the Particle IDE.
    #include <adafruit-led-backpack.h>
    // This #include statement was automatically added by the Particle IDE.
    #include <blynk.h>
    #include <stdlib.h>
    #include <application.h>
    #define BLYNK_PRINT Serial

    char auth[] = "a8dfcbf1991644309fb9078b45c7b4f1";

    Adafruit_8x8matrix matrix1;
    Adafruit_8x8matrix matrix2;

    void setupMatrix ( void *mat )
    {
      Adafruit_8x8matrix *m = (Adafruit_8x8matrix*) mat;
      m->clear();
      m->writeDisplay();
      m->setTextSize(1);
      m->setTextWrap(false);
      m->setTextColor(LED_ON);
      m->setRotation(0);
      m->setCursor(0, 0);
    }

    struct cardsandCount
    {
        int cards [25];
        int count;
        int sum;
    };
        
    struct cardsandCount playerStats = { {0}, 0, 0};
    struct cardsandCount compStats = { {0}, 0, 0};
    struct cardsandCount *compFlag;
    struct cardsandCount *playerFlag;

    WidgetLCD lcd(V0);


    enum { STAND,HIT };

    int pinDataV1 = 0, pinDataV2 = 0;

    struct cardsandCount *initialdealPlayer ( struct cardsandCount *theStruct );
    struct cardsandCount *initialdealComp (struct cardsandCount *theStruct );
    void displayonLCD ( struct cardsandCount *theStruct );                          //declaration of functions, function definitions at the bottom
    void displaySum ( struct cardsandCount *theStruct );
    struct cardsandCount *random_generator ( struct cardsandCount *theStruct );
    struct cardsandCount *sumofCards ( struct cardsandCount *theStruct );
    void decideWinner ( struct cardsandCount *playerStruct, struct cardsandCount *compStruct );


    void setup()
    {
        uint32_t seed = millis(); 
        srand(seed);
         
        Serial.begin(9600);
        delay(5000); // Allow board to settle
        
        lcd.clear();

        Blynk.begin(auth);
        
          // set the I2C address of each matrix
        matrix1.begin(0x70);
        matrix2.begin(0x71);

        // initialize each matrix
        setupMatrix(&matrix1);
        setupMatrix(&matrix2); 
        
        playerFlag = initialdealPlayer ( &playerStats );
        playerFlag = sumofCards ( playerFlag );
        displaySum (playerFlag);
        displayonLCD (playerFlag);
        
        compFlag = initialdealComp (&compStats);
        compFlag = sumofCards ( compFlag );
        while ( compFlag->sum < 17 )
        {
             compFlag = random_generator ( compFlag );
             compFlag = sumofCards (compFlag);
        }
        

    }


    void loop ()
    {
        Blynk.run();
        
    }
        
        
    struct cardsandCount *initialdealPlayer ( struct cardsandCount *theStruct )
    {
        struct cardsandCount *structPointer = theStruct;
        
        for ( int i = 0; i < 2; i++ )
        {
            structPointer->cards [i] = rand () % 10 + 1;
            Serial.println(structPointer->cards[i]);
        }
        
        return structPointer;
    }

    struct cardsandCount *initialdealComp (struct cardsandCount *theStruct )
    {
        struct cardsandCount *structPointer = theStruct;
        
        for ( int i = 0; i < 2; i++ )
        {
            structPointer->cards [i] = rand () % 10 + 1;
            Serial.println(structPointer->cards[i]);
        }
        return structPointer;
    }

    void displayonLCD ( struct cardsandCount *theStruct )
    {
        struct cardsandCount *structPointer = theStruct;
        for ( int i = 0; i < structPointer->count; i++ )
        {
            lcd.print(0, i, structPointer->cards [i]);
            Serial.println(structPointer->cards[i]);
        }
    }

    void displaySum ( struct cardsandCount *theStruct )
    {
        struct cardsandCount *structPointer;
        
        char ones = structPointer->sum % 10;
        char tens = structPointer->sum / 10;
        
        Serial.println(structPointer->sum);
        
        // write the ones digit to matrix1
      matrix1.clear();
      matrix1.setCursor(0, 0);
      matrix1.write(tens + '0'); // the ASCII value of tens is ‘0’+tens
      matrix1.writeDisplay();

      // write the tens digit to matrix2
      matrix2.clear();
      matrix2.setCursor(0, 0);
      matrix2.write(ones + '0');
      matrix2.writeDisplay();
    }


    struct cardsandCount *random_generator ( struct cardsandCount *theStruct )
    {
        struct cardsandCount *structPointer = theStruct;
        int i = 0;
        while( structPointer->cards[i] != 0 )
        {
            i++;
        }
        
        structPointer->cards[i] = rand () % 10 + 1;
        structPointer->count++;
        return structPointer;
    }

    struct cardsandCount *sumofCards ( struct cardsandCount *theStruct )
    {
        struct cardsandCount *structPointer = theStruct ;
        for ( int i = 0; i < structPointer->count; i++ )
        {
            structPointer->sum += structPointer->cards [i];
        }
        return structPointer;
        
    }

    BLYNK_WRITE(V1)
    {
         pinDataV1 = param.asInt ();
    }
        
    BLYNK_WRITE(V2)
    {
        pinDataV2 = param.asInt ();

        
        if ( pinDataV1 == 1 && pinDataV2 == 0)
        {
            playerFlag = random_generator (playerFlag);
            playerFlag = sumofCards ( playerFlag );
            displaySum (playerFlag);
            displayonLCD (playerFlag);
        }
        
        else if ( pinDataV1 == 0 && pinDataV2 == 1)
        {
            playerFlag = sumofCards (playerFlag);
            decideWinner ( playerFlag, compFlag );
        }
    }

    void decideWinner ( struct cardsandCount *playerStruct, struct cardsandCount *compStruct)
    {
        struct cardsandCount *playerPointer, *compPointer;
        
        if ( playerPointer->sum > compPointer->sum )
        {
            lcd.print(0,0,"Player wins!");
        }
        
        else if (  playerPointer->sum < compPointer->sum )
        {
            lcd.print (0,0,"Computer wins!");
        }
        
        else if (playerPointer->sum ==compPointer->sum )
        {
            lcd.print(0,0,"It's a draw");
        }
        
    }

Just to note, I realized a logical error that the player may lose before hitting STAND, but I’ll correct that later.

I think I’m starting the who;e program when I first flash it to the Photon.

Thanks!

Anyone has an idea why the code is not working?

Please?

Okay, update is due. Now, there are numbers printed out to the serial terminal, but I can’t make any sense of it. In the void setup function, I added Serial.println statements after the initialdealPlayer function is called, like this:

    playerFlag = initialdealPlayer ( &playerStats );
    for ( int i = 0; i < 2; i++ )
    {
        Serial.println(playerFlag->cards [i] );
    }
    playerFlag = sumofCards ( playerFlag );
    displaySum (playerFlag);
    displayonLCD (playerFlag);

I also added Serial.println statements for initaldealPlayer function, sum and both display functions. Here is my output trace on the serial terminal:

4
9
4
9
0
134231327
7
9
0
7
23

A couple of things I find odd and can’t make sense of. Why is there a line with so many numbers in it? Also another odd fact, when I remove the Serial.println statements after the initialdealPlayer function in void setup, nothing appears on the serial terminal after flashing the code. Also, the terminal keeps printing out zero whenever I press the STAND button in Blynk.

I’m glad there’s something showing in the serial terminal now, but I can’t make sense of the numbers and the program’s behavior from it.

One thing: Have more elaborate print statements.
You are printing anonymous numbers but don’t know which of all your statements actually printed it.
Add some info around it - which function, what other variables and factors involved, …

And once your round ended, you need to reset the game to start as if it was a fresh start.
I don’t see that in your code.
In decide winner you just print the message but don’t reset the players or any other structs.

@ScruffR Thanks for your suggestions! That didn’t occur to me…

A couple of questions here. Assuming that the random generator function works just fine, could the reason that no numbers show up on my LCD widget be that I set it to advanced mode instead of simple? To display numbers, must I use the simple mode and use the BLYNK_READ and Blynk,virtualWrite functions instead? If so, I need another LCD widget that is set to simple mode, right?

Also, another question that just involves programming logic and the workings of Blynk.run(). Can a function modify global variables that are not passed into it as a parameter during a function call? To reset the game, can I just create a function of type void reset (void) within the decideWinner function? Since my struct pointers are declared globally, I could access it and reset it within this void function, right? Then, after the reset function, the initial deal function is called again to restart the game. This would be the last command call in the decideWinner function, and since it is the end of the void loop function, Blynk.run will be called again, and when it is called, Blynk.run calls the Blynk,write functions over again. Am I understanding the flow of the program correctly?

EDIT: My current program logic from Blynk.run is like this:

BLYNK_WRITE(V1)
{
     pinDataV1 = param.asInt ();
     
     if ( pinDataV1 == 1 && pinDataV2 == 0)
    {
        playerFlag = random_generator (playerFlag);
        playerFlag = sumofCards ( playerFlag );
        if ( playerFlag->sum > 21 )
        {   
            decideWinner ( struct cardsandCount playerFlag, struct cardsandCount compFlag );
        }
        displaySum (playerFlag);
        displayonLCD (playerFlag);
    }
    
    else if ( pinDataV1 == 0 && pinDataV2 == 1)
    {
        playerFlag = sumofCards (playerFlag);
        decideWinner ( playerFlag, compFlag );
    }
}
    
BLYNK_WRITE(V2)
{
    pinDataV2 = param.asInt ();

    
    if ( pinDataV1 == 1 && pinDataV2 == 0)
    {
        playerFlag = random_generator (playerFlag);
        playerFlag = sumofCards ( playerFlag );
        displaySum (playerFlag);
        displayonLCD (playerFlag);
    }
    
    else if ( pinDataV1 == 0 && pinDataV2 == 1)
    {
        playerFlag = sumofCards (playerFlag);
        decideWinner ( playerFlag, compFlag );
    }
}

void decideWinner ( struct cardsandCount *playerStruct, struct cardsandCount *compStruct)
{
    struct cardsandCount *playerPointer = playerStruct, *compPointer = compStruct;
    
    if ( playerPointer->sum > compPointer->sum )
    {
        lcd.print(0,0,"Player wins!");
    }
    
    else if (  playerPointer->sum < compPointer->sum )
    {
        lcd.print (0,0,"Computer wins!");
    }
    
    else if ( compPointer->sum > 21 )
    {
        lcd.print(0,0,"Comp goes bust, player wins!");
    }
    
    else if ( playerPointer->sum > 21 )
    {
        lcd.print(0,0,"Player goes bust, comp wins!");
    }
    
    else if (playerPointer->sum ==compPointer->sum )
    {
        lcd.print(0,0,"It's a draw, dealer wins!");
    }
    
    resetStruct ( struct cardsandCount playerPointer, struct cardsandCount compPointer );
    
}

and resetStruct is of type void but will reset the playerFlag and compFlag pointers. I haven’t written it yet, but it will look like this:

void resetStruct ( struct cardsandCount playerPointer, struct cardsandCount compPointer )
{   
    playerFlag = playerPointer; compFlag = compPointer;
    
    //reinitialize each member of struct for both player and comp to zero.
}

Would this work? playerFlag and compFlag are declared globally, so can they be accessed by the resetStruct function?

As mentioned earlier I don't use Blynk a lot, so I'd have to try out some things to answer the Blynk specific questions, but for pure programming questions I should be able to enlighten you :wink:

As global variables are globally accessible, this is a clear Yes!

Nope, you should not declare a local function for that.

Yes, you can. But since you also hold the actuals structs as global variables, why not just work on those? I'm not quite sure why you are using so many indirections anyway :confused:
Given your felt experience with programming, I'd keep things a lot more straight forward (less pointers) for the beginning.

loop() is called over and over hundreds to thousands of times per second and hence Blynk.run() is permanently called to, but only calls BLYNK_WRITE() functions as needed.

Try it out, that's what I'd have to do too (unless I looked closer and saw something obvious, but my time is limited too :wink: )

@ScruffR I’m excited. Numbers finally show up on the LCD widget!!! Although, the results are not those really expected, there is progress! But first, I’ve identified the source of error, though I can’t find the cause of it. When I run the program, here is my execution trace:

initialdealPlayer cards are:
6
initialdealPlayer cards are:
9
Sum of cards are:
0
Ones are:

Tens are:

Obviously, something is wrong with my sum of Cards function, but everything looks right to me. I’ve initialized the sum struct member above to be zero. So why is the sum not updating? Can you help me pleease? I’m never as excited like this till now.

I wrote this when I took a course using C++ and adapted to Arduino when I started playing around with TFT screens. I saw your method and thought you might appreciate this.

#define NUMBER_OF_CARDS 52

enum Suit {SPADES, HEARTS, CLUBS, DIAMONDS};
enum Face {ACE = 0, DUECE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
const char* suitName[] = {"Spades", "Hearts", "Clubs", "Diamonds"};
const char* faceName[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

class Card
{
  public:
    Card() {};
    Card(Face cardFace, Suit cardSuit, byte cardValue);
    void printCard();
    byte getValue();
    Face face;
    Suit suit;
    
  private: 
    byte value;
};

class Hand
{
  public:
    Hand();
    void printHand();
    void getCard(Card newCard);
    byte handTotal();
    void empty();

  private:
    byte aceCount = 0;
    byte total;
    Card* cards;
    byte cardCount = 0;
};

class Deck
{
  public:
    Deck();
    void printDeck();
    void shuffle();
    Card dealCard();
    bool newDeck();
    bool lowDeck();
    
  private:
    Card* theDeckPtr;
    byte currentCard;
};

Card::Card(Face cardFace, Suit cardSuit, byte cardValue)
{
  face = cardFace;
  suit = cardSuit;
  value = cardValue;
}

byte Card::getValue()
{
  return value;
}

void Card::printCard()
{
  Serial.print(faceName[face]);
  Serial.print(F(" of "));
  Serial.println(suitName[suit]);  //Serial.print(suitName[suit]);
  //Serial.print(F(" and value of "));
  //Serial.println(value);
}

Deck::Deck()
{
  Face faces[] = {ACE, DUECE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
  Suit suits[] = {SPADES, HEARTS, CLUBS, DIAMONDS};
  theDeckPtr = new Card[NUMBER_OF_CARDS];
  for (byte i = 0; i < NUMBER_OF_CARDS; i++) // fill the array of Card objects with faces, suits and values
  {
    theDeckPtr[i] = Card(faces[i % 13], suits[i / 13], ((i % 13) + 1 < 10 ? (i % 13) + 1 : 10));
  }
  currentCard = 0;
}
bool Deck::lowDeck()
{
  return currentCard > NUMBER_OF_CARDS >> 1;
}
void Deck::shuffle()
{
  Serial.println(F("Shuffling the deck..........")); //<<<<<<<<<<<<<<<<  EDIT OUT
  randomSeed(millis());
  currentCard = 0;
  for (byte i = 0; i < NUMBER_OF_CARDS; i++)
  {
    byte index = random(i, NUMBER_OF_CARDS);
    Card temp = theDeckPtr[i];
    theDeckPtr[i] = theDeckPtr[index];
    theDeckPtr[index] = temp;
  }
}

void Deck::printDeck()
{
  for (byte i = 0; i < NUMBER_OF_CARDS; i++)
  {
    theDeckPtr[i].printCard();
  }
}

Card Deck::dealCard()
{
  if (currentCard >= NUMBER_OF_CARDS)
  {
    shuffle();
  }
  if (currentCard < NUMBER_OF_CARDS)
  {
    return theDeckPtr[currentCard++];
  }
  return (theDeckPtr[0]);
}

bool Deck::newDeck()
{
  return currentCard ? false : true;;
}

Hand::Hand()
{
  cards = new Card[12]; // 4*ACE + 4*DEUCE + 3*THREE = 21 pts in 11 cards
}

void Hand::getCard(Card newCard)
{
  cards[cardCount++] = newCard;
  if (newCard.face == ACE)
  {
    total += 11;
    aceCount++;
  }
  else
  {
    total += newCard.getValue();
  }
  if(total > 21 && aceCount > 0)
  {
    total -= 10;
    aceCount--;
  }
}

void Hand::printHand()
{
  for (byte i = 0; i < cardCount; i++)
  {
    cards[i].printCard();
  }
  Serial.print(F("Hand Total = "));
  Serial.println(total);
  Serial.println();
}

byte Hand::handTotal()
{
  return total;
}

void Hand::empty()
{
  for(byte i = 0; i < cardCount; i++)
  {
    Card* nullCard = &cards[i];
    nullCard = NULL;
  }
  cardCount = 0;
  total = 0;
  aceCount = 0;
}

//<<<<<<<<<<<<< PROGRAM START <<<<<<<<<<<<<<<<<<<<<<<<

enum GameState {
  DEAL_CARDS,
  PLAYER_DRAW,
  DEALER_DRAW,
  SCORE
};

char* gameStateText[] = {"Dealing Cards...", "Select 'h' to hit or 's' to stay", "Dealer playing...", "select 'x' for a new hand"};

GameState gameState = DEAL_CARDS;
GameState lastGameState = DEAL_CARDS;

int dealerScore = 0;
int playerScore = 0;
Deck myDeck;
Hand myHand;
Hand dealerHand;

void setup()
{
  Serial.begin(9600);
  Serial.println(F("Enter 'x' to draw a hand"));
  while (!Serial.available()) {};
  myDeck.shuffle();
}

void loop()
{
  char playerCommand = 0;
  if (Serial.available())
  {
    playerCommand = Serial.read();
    Serial.println(playerCommand);
  }
  if (gameState == DEAL_CARDS)
  {
    myHand.getCard(myDeck.dealCard());
    dealerHand.getCard(myDeck.dealCard());
    myHand.getCard(myDeck.dealCard());
    dealerHand.getCard(myDeck.dealCard());
    Serial.println(F("My Hand: "));
    myHand.printHand();
    Serial.println(F("Dealer Hand: "));
    dealerHand.printHand();
    evaluateScores();
    if(gameState == DEAL_CARDS)
    {
      gameState = PLAYER_DRAW;
    }
  }
  if (gameState == PLAYER_DRAW)
  {
    if (playerCommand == 'h')
    {
      myHand.getCard(myDeck.dealCard());
      Serial.println(F("My Hand: "));
      myHand.printHand();
      Serial.println(F("Dealer Hand: "));
      dealerHand.printHand();
      evaluateScores();
    }
    else if (playerCommand == 's')
    {
      Serial.println(F("Player Stays"));
      Serial.println(F("My Hand: "));
      myHand.printHand();
      Serial.println(F("Dealer Hand: "));
      dealerHand.printHand();
      evaluateScores();
      gameState = DEALER_DRAW;
    }
  }
  else if (gameState == DEALER_DRAW)
  {
    if (dealerHand.handTotal() <= myHand.handTotal())
    {
      dealerHand.getCard(myDeck.dealCard());
      Serial.println(F("My Hand: "));
      myHand.printHand();
      Serial.println(F("Dealer Hand: "));
      dealerHand.printHand();
      evaluateScores();
    }
  }
  else if(gameState == SCORE)
  {
    if(playerCommand == 'x')
    {
      myHand.empty();
      dealerHand.empty();
      if(myDeck.lowDeck())
      {
        myDeck.shuffle();
      }
      gameState = DEAL_CARDS;
    }
  }
  
  if(lastGameState != gameState)
  {
    Serial.println(gameStateText[gameState]);
  }
  lastGameState = gameState;
}

void evaluateScores()
{
  if(gameState == PLAYER_DRAW || gameState == DEAL_CARDS)
  {
    if(myHand.handTotal() == 21 && dealerHand.handTotal() == 21)
    {
      Serial.println(F("It's a Draw"));
      gameState = SCORE;
    }
    else if (myHand.handTotal() == 21)
    {
      Serial.println(F("Player has Twenty One!"));
      gameState = SCORE;
    }
    else if (dealerHand.handTotal() == 21)
    {
      Serial.println(F("Dealer has 21!"));
      gameState = SCORE;
    }
    else if (myHand.handTotal() > 21)
    {
      Serial.println(F("Player Busted!"));
      gameState = SCORE;
    }
    else if (dealerHand.handTotal() > 21)
    {
      Serial.println(F("Dealer Busts!"));
      gameState = SCORE;
    }
    else if(dealerHand.handTotal() > 16 && dealerHand.handTotal() == myHand.handTotal())
    {
      Serial.println(F("It's a PUSH..."));
      gameState = SCORE;
    }
  }
  else if (gameState == DEALER_DRAW)
  {
    if(dealerHand.handTotal() > 21)
    {
      Serial.println(F("Dealer Busts!"));
      gameState = SCORE;
    }
    else if(dealerHand.handTotal() > 16 && dealerHand.handTotal() == myHand.handTotal())
    {
      Serial.println(F("It's a PUSH..."));
      gameState = SCORE;
    }
    else if(dealerHand.handTotal() > myHand.handTotal())
    {
      Serial.println(F("Dealer Wins!"));
      gameState = SCORE;
    }
  }
}

@BulldogLowell Wow!!! Your program is so sophisticated and far superior to mine! I don’t know C++ though, I probably must read up on it before attempting to understand your program. Do you think it’s possible to integrate your method into my existing program structure? Thanks so much for sharing, though. Will definitely go through it.

Ah, I realized that I haven’t included my current updated program. Here it is:

// This #include statement was automatically added by the Particle IDE.
#include <adafruit-led-backpack.h>
// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#include <stdlib.h>
#include <application.h>
#define BLYNK_PRINT Serial

char auth[] = "a8dfcbf1991644309fb9078b45c7b4f1";

Adafruit_8x8matrix matrix1;
Adafruit_8x8matrix matrix2;

void setupMatrix ( void *mat )
{
  Adafruit_8x8matrix *m = (Adafruit_8x8matrix*) mat;
  m->clear();
  m->writeDisplay();
  m->setTextSize(1);
  m->setTextWrap(false);
  m->setTextColor(LED_ON);
  m->setRotation(0);
  m->setCursor(0, 0);
}

struct cardsandCount
{
    int cards [25];
    int count;
    int sum;
};
    
struct cardsandCount playerStats = { {0}, 0, 0};
struct cardsandCount compStats = { {0}, 0, 0};
struct cardsandCount *compFlag;
struct cardsandCount *playerFlag;

WidgetLCD lcd(V0);


enum { STAND,HIT };

int pinDataV1 = 0, pinDataV2 = 0;

struct cardsandCount *initialdealPlayer ( struct cardsandCount *theStruct );
struct cardsandCount *initialdealComp (struct cardsandCount *theStruct );
void displayonLCD ( struct cardsandCount *theStruct );                          //declaration of functions, function definitions at the bottom
void displaySum ( struct cardsandCount *theStruct );
struct cardsandCount *random_generator ( struct cardsandCount *theStruct );
struct cardsandCount *sumofCards ( struct cardsandCount *theStruct );
void decideWinner ( struct cardsandCount *playerStruct, struct cardsandCount *compStruct );
void resetStruct ( struct cardsandCount *playerPointer, struct cardsandCount *compPointer );


void setup()
{
    uint32_t seed = millis(); 
    srand(seed);
     
    Serial.begin(9600);
    delay(5000); // Allow board to settle
    
    lcd.clear();

    Blynk.begin(auth);
    
      // set the I2C address of each matrix
    matrix1.begin(0x70);
    matrix2.begin(0x71);

    // initialize each matrix
    setupMatrix(&matrix1);
    setupMatrix(&matrix2); 
    
    playerFlag = initialdealPlayer ( &playerStats );
    playerFlag = sumofCards ( playerFlag );
    displaySum (playerFlag);
    displayonLCD (playerFlag);
    
    compFlag = initialdealComp (&compStats);
    compFlag = sumofCards ( compFlag );
    while ( compFlag->sum < 17 )
    {
         compFlag = random_generator ( compFlag );
         compFlag = sumofCards (compFlag);
    }
    

}


void loop ()
{
    Blynk.run();
    
}
    
    
struct cardsandCount *initialdealPlayer ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct;
    
    for ( int i = 0; i < 2; i++ )
    {
        structPointer->cards [i] = rand () % 10 + 1;
        Serial.println("initialdealPlayer cards are: ");
        Serial.println(structPointer->cards[i]);
    }
    
    return structPointer;
}

struct cardsandCount *initialdealComp (struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct;
    
    for ( int i = 0; i < 2; i++ )
    {
        structPointer->cards [i] = rand () % 10 + 1;
        Serial.println("initialdealComp cards are: ");
        Serial.println(structPointer->cards[i]);
    }
    return structPointer;
}

void displayonLCD ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct;
    for ( int i = 0; i < structPointer->count; i++ )
    {
        lcd.print(0, i, structPointer->cards [i]);
        Serial.println("Cards on LCD are: ");
        Serial.println(structPointer->cards[i]);
    }
}

void displaySum ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer;
    
    char ones = structPointer->sum % 10;
    char tens = structPointer->sum / 10;
    
    Serial.println("Ones are: ");
    Serial.println(ones);
    Serial.println("Tens are: ");
    Serial.println(tens);
    
    // write the ones digit to matrix1
  matrix1.clear();
  matrix1.setCursor(0, 0);
  matrix1.write(tens + '0'); // the ASCII value of tens is ‘0’+tens
  matrix1.writeDisplay();

  // write the tens digit to matrix2
  matrix2.clear();
  matrix2.setCursor(0, 0);
  matrix2.write(ones + '0');
  matrix2.writeDisplay();
}


struct cardsandCount *random_generator ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct;
    int i = 0;
    while( structPointer->cards[i] != 0 )
    {
        i++;
    }
    
    structPointer->cards[i] = rand () % 10 + 1;
    structPointer->count++;
    return structPointer;
}

struct cardsandCount *sumofCards ( struct cardsandCount *theStruct )
{
    struct cardsandCount *structPointer = theStruct ;
    for ( int i = 0; i < structPointer->count; i++ )
    {
        structPointer->sum = structPointer->sum + structPointer->cards [i];
    }
    Serial.println("Sum of cards are: ");
    Serial.println(structPointer->sum);
    
    return structPointer;
    
}

BLYNK_WRITE(V1)
{
     pinDataV1 = param.asInt ();
     
     if ( pinDataV1 == 1 && pinDataV2 == 0)
    {
        playerFlag = random_generator (playerFlag);
        playerFlag = sumofCards ( playerFlag );
        if ( playerFlag->sum > 21 )
        {   
            decideWinner ( playerFlag, compFlag );
        }
        displaySum (playerFlag);
        displayonLCD (playerFlag);
    }
    
    else if ( pinDataV1 == 0 && pinDataV2 == 1)
    {
        playerFlag = sumofCards (playerFlag);
        decideWinner ( playerFlag, compFlag );
    }
}
    
BLYNK_WRITE(V2)
{
    pinDataV2 = param.asInt ();

    
    if ( pinDataV1 == 1 && pinDataV2 == 0)
    {
        playerFlag = random_generator (playerFlag);
        playerFlag = sumofCards ( playerFlag );
        displaySum (playerFlag);
        displayonLCD (playerFlag);
    }
    
    else if ( pinDataV1 == 0 && pinDataV2 == 1)
    {
        playerFlag = sumofCards (playerFlag);
        decideWinner ( playerFlag, compFlag );
    }
}

void decideWinner ( struct cardsandCount *playerStruct, struct cardsandCount *compStruct)
{
    struct cardsandCount *playerPointer = playerStruct, *compPointer = compStruct;
    
    if ( playerPointer->sum > compPointer->sum )
    {
        lcd.print(0,0,"Player wins!");
    }
    
    else if (  playerPointer->sum < compPointer->sum )
    {
        lcd.print (0,0,"Computer wins!");
    }
    
    else if ( compPointer->sum > 21 )
    {
        lcd.print(0,0,"Comp goes bust, player wins!");
    }
    
    else if ( playerPointer->sum > 21 )
    {
        lcd.print(0,0,"Player goes bust, comp wins!");
    }
    
    else if (playerPointer->sum ==compPointer->sum )
    {
        lcd.print(0,0,"It's a draw, dealer wins!");
    }
    
    resetStruct (playerPointer, compPointer );
    
}

void resetStruct ( struct cardsandCount *playerPointer, struct cardsandCount *compPointer )
{   
    playerFlag = playerPointer;
    compFlag = compPointer;
    
   for ( int i = 0; i < playerFlag->count; i++ )
   {
       playerFlag->cards[i] = 0;
   }
   playerFlag->sum=0;
   playerFlag->count=0;
   
   for ( int i = 0; i < compFlag->count; i++ )
   {
       compFlag->cards[i]=0;
   }
   compFlag->sum=0;
   compFlag->count=0;
   
   playerFlag = initialdealPlayer ( playerFlag );
   compFlag = initialdealComp ( compFlag );
   
   
}

I really don’t understand why the sum of cards are zero. I think I wrote everything correctly in the sumofCards function. Can someone just check that and see if there’s anything wrong?

I’m really no understanding why you keep passing around and returning so many pointers where you only deal with the same structs over and over again. The pointers never change.
This just makes things rather obscure and hard to follow - you don’t do yourself any favour with confusing yourself that way IMHO.

Most (if not all) your struct cardsandcount* xxxx(yyy) functions could just as well be void xxxx(yyy) and your playerFlag/compFlag can be set once and for all.

1 Like