DIY Barcode Scanner Game

Once upon a time it was monsters hidden in the barcodes of everyday items, but now there are electronic components stuck inside. Using the RedBoard Artemis, Barcode Scanner Breakout and Qwiic Micro OLED Display, we can unlock them for a fun game.

Favorited Favorite 0

In my time working with electronics I’ve been exposed to so many different technologies, products, and just wicked cool gadgets. So what am I spending my free time doing? Chasing after those long lost feelings from childhood games, of course! Back in the early 2000s (yes, nearly TWO decades ago…) I got the chance to play with a handheld barcode scanning game called “Skannerz” from Radica Games Limited. You can find a wiki detailing this game in more detail here. Roll the tape!

Beware for those sensitive to old school cringe kid commercials

Basically, it was advertised as monsters and items hidden in everyday barcodes and you can scan to collect them. Training them, battling them, or just collecting them was an awesome time for me. So what better to do with my time than to put together some SparkFun components to make something similar? I’ve made my riff by having common electrical components as the "hidden" surprises - items like resistors, capacitors and LEDs are shown as pixel images on the display screen.

Overview

alt text

SPX Version Barcode Scanner pictured since I had one laying around.

The major components of the system are a barcode scanner, display and microcontroller. I’ve outlined a list of the components I’m using in my system here:

The brain of the system is the SparkFun RedBoard Artemis, since its features include 1 MB flash/384k RAM, an I2C bus and a UART bus, which is powerful enough for what I need. I’m also using the SparkFun DE2120 Barcode Scanner breakout and a SparkFun Qwiic Micro OLED breakout. If you want to make this system or tweak it you could use plenty of other screen options, but you'll have to modify the code. I opted to use the Qwiic Micro OLED because of its ease of use, with simple function calls.

It was easier to start prototyping this system by breaking it into two parts. First, I wanted to develop how I get an item from the barcode. Then, I want to display a pixel image of the component on the OLED screen.

Example 1 - Barcode, to do what?

Common barcodes I can find around my house are UPC-A codes, which are 13-digit barcodes. My initial thought was to break up the barcode into three portions just to have resistors, capacitors and LEDs. But I wanted something a bit more to start with, so I used the first nine digits to choose what component was found, and the last four digits to choose the color of the component (red, green or blue). You can find my code below:

/*******************************************************************************************
 * This example allows a user to scan a Barcode and 'find' a component in the barcode. The 
 * serial terminal then reports the type of component and color of the component.
 * 
 * Brandon Williams @ SparkFun Electronics
 * Original Creation Date: August 10, 2021
 * 
 * This code is Lemonadeware; if you see me (or any other SparkFun employee) at the 
 * local, and you've found our code helpful, please buy us a round!
 * 
 * Hardware Connections:
 * Attach Red Board Artemis to computer using micro-B USB cable.
 * Attach SparkFun Barcode Scanner Breakout - DE2120 to Red Board using 24/26 AWG Jumper Wires
 * 
 * Distributed as-is; no warranty is given.
 *****************************************************************************************/
#include <SparkFun_DE2120_Arduino_Library.h>
#include <SoftwareSerial.h>

SoftwareSerial softSerial(2,3);

DE2120 scanner;

#define BUFFER_LEN 40
char scanBuffer[BUFFER_LEN];

int scan_int[12];
char component[2]; //{item,color}

void setup() {
  Serial.begin(115200);
  Serial.println("**** SparkFun HIDDEN COMPONENTS Game! ****");

  if(scanner.begin(softSerial) == false){
    Serial.println("Scanner did not respond.");
    Serial.println("Please check your wiring. Did you scan the POR232 barcode?");
    while(1);
  }
  Serial.println("Scanner online!");
  Serial.println("\nLet's scan some stuff!! ===>\n");
}

void loop() {
  if(scanner.readBarcode(scanBuffer,BUFFER_LEN)){
    convertCharToInt();
    getHiddenItem();
    Serial.print("Wow! A ");
    if(component[0] == 'R'){
      if(component[1] == 'r'){
        Serial.print("Red Resistor");
      }
      else if(component[1] == 'g'){
        Serial.print("Green Resistor");
      }
      else if(component[1] == 'b'){
        Serial.print("Blue Resistor");
      }
    }
    else if(component[0] == 'C'){
      if(component[1] == 'r'){
        Serial.print("Red Capacitor");
      }
      else if(component[1] == 'g'){
        Serial.print("Green Capacitor");
      }
      else if(component[1] == 'b'){
        Serial.print("Blue Capacitor");
      }
    }
    else if(component[0] == 'L'){
      if(component[1] == 'r'){
        Serial.print("Red LED");
      }
      else if(component[1] == 'g'){
        Serial.print("Green LED");
      }
      else if(component[1] == 'b'){
        Serial.print("Blue LED");
      }
    }
    Serial.println(" was hidden inside that barcode!!!\n\tYou better hang onto that!");
  }
  delay(200);
}

void convertCharToInt(){
  for(int i = 0; i < 12; i++){
    switch (scanBuffer[i]){
      case '0':
        scan_int[i] = 0;
        break;
      case '1':
        scan_int[i] = 1;
        break;
      case '2':
        scan_int[i] = 2;
        break;
      case '3':
        scan_int[i] = 3;
        break;
      case '4':
        scan_int[i] = 4;
        break;
      case '5':
        scan_int[i] = 5;
        break;
      case '6':
        scan_int[i] = 6;
        break;
      case '7':
        scan_int[i] = 7;
        break;
      case '8':
        scan_int[i] = 8;
        break;
      case '9':
        scan_int[i] = 9;
        break;
      default:
        break;
    }
  }
}

void getHiddenItem(){
  int sum1, sum2, sum3, sum4;
  char itemColor;

  for(int i = 0; i < 3; i++){
    sum1 += scan_int[i];
  }
  for(int i = 3; i < 6; i++){
    sum2 += scan_int[i];
  }
  for(int i = 6; i < 9; i++){
    sum3 += scan_int[i];
  }
  for(int i = 9; i < 12; i++){
    sum4 += scan_int[i];
  }

  itemColor = checkColor(sum4);

  //If all 3 sums are equal rely on 4th sum to determine type, will also determine color
  if(sum1 == sum2 && sum2 == sum3){
    Serial.println("\tAll 3 sums are equal...");

    if(itemColor == 'r'){ //resistor, red
      component[0] = 'R';
      component[1] = itemColor;
    }
    else if(itemColor == 'g'){ //capacitor, green
      component[0] = 'C';
      component[1] = itemColor;
    }
    else if(itemColor == 'b'){ //LED, blue
      component[0] = 'L';
      component[1] = itemColor;
    }
    else{
      Serial.println("Something broke, report the BUG!!");
      Serial.print("\tError code: ");
      Serial.println(itemColor);
    }
  }
  //If sum1 equal to sum2 but larger than sum3 then default to making it an LED
  else if(sum1 == sum2 && sum1 > sum3){
    component[0] = 'L';
    component[1] = itemColor;
  }
  //If sum1 is larger than sum2 but equal to sum3 then default to Capcitor
  else if(sum1 > sum2 && sum1 == sum3){
    component[0] = 'C';
    component[1] = itemColor;    
  }
  //if sum1 is larger than both sum2 and sum3 then item is Resistor
  else if(sum1 > sum2 && sum1 > sum3){
    component[0] = 'R';
    component[1] = itemColor;
  }
  //if sum2 is equal to sum3 but larger than sum1 then default to Resistor
  else if(sum2 == sum3 && sum2 > sum1){
    component[0] = 'R';
    component[1] = itemColor;
  }
  //if sum2 is greater than sum1 and sum 3 then item is capacitor
  else if(sum2 > sum1 && sum2 > sum3){
    component[0] = 'C';
    component[1] = itemColor;
  }
  //if sum3 is greater than sum1 and sum2 then item is LED
  else if(sum3 > sum1 && sum3 > sum2){
    component[0] = 'L';
    component[1] = itemColor;
  }
  else{
    Serial.println("[ITEM] Barcode didn't yeild a known option. Report BUG!");
    Serial.print("\tSUM 1: ");
    Serial.println(sum1);
    Serial.print("\tSUM 2: ");
    Serial.println(sum2);
    Serial.print("\tSUM 3: ");
    Serial.println(sum3);
    Serial.print("\tSUM 4: ");
    Serial.println(sum4);
  }
}

char checkColor(int color){
  char res;

  if(color >= 0 && color < 9){ //red
    res = 'r';
  }
  else if(color >=9 && color < 18){ //green
    res = 'g';
  }
  else if(color >= 18 && color <= 27){ //blue
    res = 'b';
  }
  else{
    Serial.println("[Color] Barcode didn't yeild a valid result, report the BUG!!");
    Serial.print("\tSUM 4: ");
    Serial.println(color);
    res = 'N';
  }

  return res;
}

Example 2 - Show yourself, component!

alt text

The next step was to integrate the OLED screen with the barcode scanner to display the component as a pixel art image. I made these images with the help of GIMP and a tool found in the SparkFun Micro OLED hookup guide to export the bitmap to an easy to copy array.

I set up the canvas size, grid size and pencil settings. You want the canvas size to be 64x48 px (units) to match the OLED screen size. The grid size needs to be 1x1 on that first line, because that sets the grid units to pixels. Alternatively you could set the next line below, but that is for units other than pixels, which gets really messy since the OLED screen isn’t even a full inch, but I digress. You also want to set your pencil to 1 px width. These settings help set for point and click painting of dot by dot in your pixel art. I had a blast with it, but it took some time to get the hang of.

When you have the pixel art finished and exported to a BMP file then you can use the export tool to get the bitmap array. You can find my finished arrays in my code below:

/*******************************************************************************************
 * This example allows a user to scan a barcode for 'find' a component in the barcode. The 
 * OLED display then shows a pixel art representation of the component and it's corresponding
 * color. 
 *
 * Brandon Williams @ SparkFun Electronics
 * Original Creation Date: August 10, 2021
 * 
 * This code is Lemonadeware; if you see me (or any other SparkFun employee) at the 
 * local, and you've found our code helpful, please buy us a round!
 * 
 * Hardware Connections:
 * Attach Red Board Artemis to computer using micro-B USB cable.
 * Attach SparkFun Barcode Scanner Breakout - DE2120 to Red Board using 24/26 AWG Jumper Wires
 * Attach Qwiic Micro OLED Breakout using a Qwiic cable 
 *
 * Distributed as-is; no warranty is given.
 *****************************************************************************************/
#include <SFE_MicroOLED.h>
#include <SparkFun_DE2120_Arduino_Library.h>

#include <SoftwareSerial.h>

SoftwareSerial softSerial(2,3);

DE2120 scanner;

#define BUFFER_LEN 40
char scanBuffer[BUFFER_LEN];

#define PIN_RESET 9
#define DC_JUMPER 1
MicroOLED oled(PIN_RESET, DC_JUMPER);

int scan_int[12];

uint8_t capacitor [] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x07, 0xEB, 0xEB, 0xDD, 0xDD, 0xD5, 0xC1,
0xC1, 0xC1, 0xD5, 0x9D, 0x1D, 0x0B, 0x6B, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0x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xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

char component[2]; //{item,color}

//struct inventory{
//  char userName;
//  uint8_t inventorySize; //limit 255 items
//  uint8_t resistor[3]; //color codes go [Red,Green,Blue]
//  uint8_t capacitor[3];
//  uint8_t led[3];
//};

void setup() {
//  SerialUSB.begin(115200);
  Serial.begin(115200);
  Wire.begin();
//  SerialUSB.println("**** SparkFun HIDDEN COMPONENTS Game! ****");
  Serial.println("**** SparkFun HIDDEN COMPONENTS Game! ****");

  //  if(scanner.begin(Serial) == false){
  if(scanner.begin(softSerial) == false){
    Serial.println("Scanner did not respond.");
    Serial.println("Please check your wiring. Did you scan the POR232 barcode?");
    while(1);
  }
  Serial.println("Scanner online!");
  Serial.println("\nLet's scan some stuff!! ===>\n");

  if(oled.begin()){
    oled.clear(ALL);
    oled.display();
    delay(1000);
    oled.clear(PAGE);
    oled.clear(ALL);
  }
  else{
    Serial.println("OLED didn't initialize, please retry");
    while(1);
  }
  //erase garbage data that may be in the buffer after initialization
  scanner.readBarcode(scanBuffer,BUFFER_LEN);
}
void loop() {
  if(scanner.readBarcode(scanBuffer,BUFFER_LEN)){
    convertCharToInt();
    getHiddenItem();
    Serial.print("Wow! A ");
    drawHiddenObject(component[0],component[1]);
    if(component[0] == 'R'){
      if(component[1] == 'r'){
        Serial.print("Red Resistor");
      }
      else if(component[1] == 'g'){
        Serial.print("Green Resistor");
      }
      else if(component[1] == 'b'){
        Serial.print("Blue Resistor");
      }
    }
    else if(component[0] == 'C'){
      if(component[1] == 'r'){
        Serial.print("Red Capacitor");
      }
      else if(component[1] == 'g'){
        Serial.print("Green Capacitor");
      }
      else if(component[1] == 'b'){
        Serial.print("Blue Capacitor");
      }
    }
    else if(component[0] == 'L'){
      if(component[1] == 'r'){
        Serial.print("Red LED");
      }
      else if(component[1] == 'g'){
        Serial.print("Green LED");
      }
      else if(component[1] == 'b'){
        Serial.print("Blue LED");
      }
    }
    Serial.println(" was hidden inside that barcode!!!\n\tYou better hang onto that!");
    delay(5000);
    oled.clear(ALL);
    oled.clear(PAGE);
  }
  delay(500);
}

void convertCharToInt(){
  for(int i = 0; i < 12; i++){
    switch (scanBuffer[i]){
      case '0':
        scan_int[i] = 0;
        break;
      case '1':
        scan_int[i] = 1;
        break;
      case '2':
        scan_int[i] = 2;
        break;
      case '3':
        scan_int[i] = 3;
        break;
      case '4':
        scan_int[i] = 4;
        break;
      case '5':
        scan_int[i] = 5;
        break;
      case '6':
        scan_int[i] = 6;
        break;
      case '7':
        scan_int[i] = 7;
        break;
      case '8':
        scan_int[i] = 8;
        break;
      case '9':
        scan_int[i] = 9;
        break;
      default:
        break;
    }
  }
}

void getHiddenItem(){
  int sum1, sum2, sum3, sum4;
  char itemColor;

  for(int i = 0; i < 3; i++){
    sum1 += scan_int[i];
  }
  for(int i = 3; i < 6; i++){
    sum2 += scan_int[i];
  }
  for(int i = 6; i < 9; i++){
    sum3 += scan_int[i];
  }
  for(int i = 9; i < 12; i++){
    sum4 += scan_int[i];
  }

  itemColor = checkColor(sum4);

  //If all 3 sums are equal rely on 4th sum to determine type, will also determine color
  if(sum1 == sum2 && sum2 == sum3){
    Serial.println("\tAll 3 sums are equal...");

    if(itemColor == 'r'){ //resistor, red
      component[0] = 'R';
      component[1] = itemColor;
    }
    else if(itemColor == 'g'){ //capacitor, green
      component[0] = 'C';
      component[1] = itemColor;
    }
    else if(itemColor == 'b'){ //LED, blue
      component[0] = 'L';
      component[1] = itemColor;
    }
    else{
      Serial.println("Something broke, report the BUG!!");
      Serial.print("\tError code: ");
      Serial.println(itemColor);
    }
  }
  //If sum1 equal to sum2 but larger than sum3 then default to making it an LED
  else if(sum1 == sum2 && sum1 > sum3){
    component[0] = 'L';
    component[1] = itemColor;
  }
  //If sum1 is larger than sum2 but equal to sum3 then default to Capcitor
  else if(sum1 > sum2 && sum1 == sum3){
    component[0] = 'C';
    component[1] = itemColor;    
  }
  //if sum1 is larger than both sum2 and sum3 then item is Resistor
  else if(sum1 > sum2 && sum1 > sum3){
    component[0] = 'R';
    component[1] = itemColor;
  }
  //if sum2 is equal to sum3 but larger than sum1 then default to Resistor
  else if(sum2 == sum3 && sum2 > sum1){
    component[0] = 'R';
    component[1] = itemColor;
  }
  //if sum2 is greater than sum1 and sum 3 then item is capacitor
  else if(sum2 > sum1 && sum2 > sum3){
    component[0] = 'C';
    component[1] = itemColor;
  }
  //if sum3 is greater than sum1 and sum2 then item is LED
  else if(sum3 > sum1 && sum3 > sum2){
    component[0] = 'L';
    component[1] = itemColor;
  }
  else{
    Serial.println("[ITEM] Barcode didn't yeild a known option. Report BUG!");
    Serial.print("\tSUM 1: ");
    Serial.println(sum1);
    Serial.print("\tSUM 2: ");
    Serial.println(sum2);
    Serial.print("\tSUM 3: ");
    Serial.println(sum3);
    Serial.print("\tSUM 4: ");
    Serial.println(sum4);
  }
}

char checkColor(int color){
  char res;

  if(color >= 0 && color < 9){ //red
    res = 'r';
  }
  else if(color >=9 && color < 18){ //green
    res = 'g';
  }
  else if(color >= 18 && color <= 27){ //blue
    res = 'b';
  }
  else{
    Serial.println("[Color] Barcode didn't yeild a valid result, report the BUG!!");
    Serial.print("\tSUM 4: ");
    Serial.println(color);
    res = 'N';
  }

  return res;
}

void drawHiddenObject(char obj, char color){
  if(obj == 'R'){
    oled.clear(ALL);
    oled.clear(PAGE);
    oled.drawBitmap(resistor);
    switch(color){
      case 'r':
        oled.setCursor(0,0);
        oled.setFontType(0);
        oled.print("Red Resistor");
        oled.display();
        break;
      case 'g':
        oled.setCursor(0,0);
        oled.setFontType(0);
        oled.print("Green Resistor");
        oled.display();
        break;
      case 'b':
        oled.setCursor(0,0);
        oled.setFontType(0);
        oled.print("Blue Resistor");
        oled.display();
        break;
      default:
        break;
    }
  }
  else if(obj == 'L'){
    oled.clear(ALL);
    oled.clear(PAGE);
    oled.drawBitmap(led);
    switch(color){
      case 'r':
        oled.setCursor(0,0);
        oled.setFontType(0);
        oled.print("Red LED");
        oled.display();
        break;
      case 'g':
        oled.setCursor(0,0);
        oled.setFontType(0);
        oled.print("Green LED");
        oled.display();
        break;
      case 'b':
        oled.setCursor(0,0);
        oled.setFontType(0);
        oled.print("Blue LED");
        oled.display();
        break;
      default:
        break;
    }
  }
  else if(obj == 'C'){
    oled.clear(ALL);
    oled.clear(PAGE);
    oled.drawBitmap(capacitor);
    switch(color){
      case 'r':
        oled.setCursor(0,0);
        oled.setFontType(0);
        oled.print("Red Capacitor");
        oled.display();
        break;
      case 'g':
        oled.setCursor(0,0);
        oled.setFontType(0);
        oled.print("Green Capacitor");
        oled.display();
        break;
      case 'b':
        oled.setCursor(0,0);
        oled.setFontType(0);
        oled.print("Blue Capacitor");
        oled.display();
        break;
      default:
        break;
    }
  }
  else Serial.println("\tObject passed doesn't register, report the BUG!!");
}

Until next time...

I’ve got a long way to go in this project, and I’m having a blast. I want to take it further by incorporating an inventory system and making it a mobile handheld device, like the Skannerz toy. Either way, the parts I used in this project are easy to use and make a fun game with. I’ve been scanning several shelves of cans in the pantry, much to the bewilderment of my family. I hope you like this project and get some inspiration for your own barcode-based game. I’ve found that time flies when you scan the day away!


Comments 1 comment

Related Posts

Recent Posts

Tags


All Tags