Arduino - Blackjack

Creating a simple blackjack game.


There are multiple ways to setup variables.

int cards[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13};
String suits[] = {"s","c","h","d"};
String cards[] = {"AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS", 
                  "AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC", 
                  "AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H", "9H", "10H", "JH", "QH", "KH", 
                  "AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD"}

In this arduino project, I'll choose the first one.

Shuffle & Deal

Before dealing a hand, we must shuffle the deck. Let's make the deck pretty. We're going to assign all the suits to the appropriate card value. There should be 13 cards of the same suit -- ace to king.

void initDeck(){
  //Make pretty display for LCD
  for(int ctr = 0; ctr < 13; ctr++){
    dCards[ctr] = sc(cards[ctr]) + "" + suits[0];
    dCards[ctr + 13] = sc(cards[ctr]) + "" + suits[1];
    dCards[ctr + 26] = sc(cards[ctr]) + "" + suits[2];
    dCards[ctr + 39] = sc(cards[ctr]) + "" + suits[3];

Now lets shuffle the deck. Since we're using two arrays, one for the card value and one for the suit, we must make sure that both are shuffled together.

void shuffleDeck(){
  for(int ctr = 0; ctr < numbCards; ctr++){
    int randNumb = rand() % (ctr+1);
    shuffleInt(&cards[ctr], &cards[randNumb]);
    shuffleStr(&dCards[ctr], &dCards[randNumb]);

void shuffleInt(int *a, int *b){
  int temp = *a;
  *a = *b;
  *b = temp;

void shuffleStr(String *a, String *b){
  String temp = *a;
  *a = *b;
  *b = temp;

The setDeckValue() that is called when shuffling is to rewrite any card value that is over 10. An example would be: J = 11. So that value will change to 10.

void setDeckValue(){
    //Check to see if any card value is above 10
    //We want this exactly here so that once the deck is initialized and shuffled, we can change the value before using sum
  for(int ctr = 0; ctr < numbCards; ctr++){
    if(cards[ctr] > 10) {
      cards[ctr] = 10;

If you noticed the sc() method in the initDeck() function, then well done. That will actually be the method that makes the deck look pretty.

String sc(int card){
  //Display the card value as user-friendly
  String newCard = String(card);
  if(card == 1){ 
    newCard = "A"; 
  }else if(card == 11){ 
    newCard = "J";
  }else if(card == 12){ 
    newCard = "Q"; 
  }else if(card == 13){ 
    newCard = "K"; 
  return newCard;

It's not over!

If you're reading this and wanting to program a blackjack and without proving all the code for you, theres ways to think about how Blackjack works.

The steps you can take are:

These examples do not show the dealer's card.