Marcin Gruda
Published © GPL3+

Lane Tech HS - PCL - Cyberpunk Rating Visualizer

Use a cyberpunk-esque plastic shape with lights to visualize the reviews of the game after launch.

IntermediateFull instructions provided5 hours38
Lane Tech HS - PCL - Cyberpunk Rating Visualizer

Things used in this project

Hardware components

Argon
Particle Argon
×1
Ribbon Cable, Color Coded Flat
Ribbon Cable, Color Coded Flat
×1
LED, Red
LED, Red
×6
LED, Blue
LED, Blue
×6
Resistor 100 ohm
Resistor 100 ohm
×6
Breadboard (generic)
Breadboard (generic)
×2

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Google Sheets
Google Sheets
GSX2JSON

Story

Read more

Schematics

Circuit Schematic

Code

Final Visualization

C/C++
Receives data, processes it while displaying the current overall rating and any rises/falls in the rating over a certain span. (Original goal is to display this live.
// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>

StaticJsonDocument<4000> doc;

//Initialize pins
int redPin = D4;
int bluePin = D6;

void setup()
{
    // Subscribe to the webhook response event
    Particle.subscribe("hook-response/getRating", myHandler, MY_DEVICES);
    
    //Set pin modes
    pinMode(redPin, OUTPUT);  
    pinMode(bluePin, OUTPUT);  
    
    Serial.begin(9600);
    
    //Setup Light Show to display full range
    analogWrite(bluePin, 0);
    analogWrite(redPin, 0);
    int i = 0;
    int j = 0;
    while (i < 255)
    {
        analogWrite(redPin, i);
        i++;
        delay(20);
    }
    while (i > 0)
    {
        analogWrite(redPin, i);
        i--;
        analogWrite(bluePin, j);
        j++;
        delay(20);
    }
    while (j > 0)
    {
        analogWrite(bluePin, j);
        j--;
        delay(20);
    }
    analogWrite(bluePin, 0);
    analogWrite(redPin, 0);
    
    //Trigger webhooks
    Particle.publish("getRating");
}

void loop()
{
}

void myHandler(const char *event, const char *data)
{
    //Setup buffer
    String dataStr = String(data);
    char strBuffer[500] = "";
    dataStr.toCharArray(strBuffer, 500);
    
    //Process first token
    String token = strtok(strBuffer, ",");
    float prevVal = token.toFloat();
    if (prevVal >= 2.5)
    {
        analogWrite(bluePin, prevVal*51);
    } 
    else 
    {
        analogWrite(redPin, prevVal*51);
    }
    delay(500);
    Serial.println(+"First value: " + String(token));
    
    //Process other tokens
    while( token != NULL ) 
    {
        float currVal = token.toFloat();
        
        if (currVal > prevVal)
        {
            analogWrite(bluePin, 0);
            analogWrite(redPin, 0);
            delay(10);
            analogWrite(bluePin, 255);
            delay(200);
            analogWrite(bluePin, 0);
            delay(10);
            analogWrite(bluePin, 255);
            delay(200);
            analogWrite(bluePin, 0);
            delay(10);
            analogWrite(bluePin, 255);
            delay(200);
        }
        else if (currVal < prevVal)
        {
            analogWrite(bluePin, 0);
            analogWrite(redPin, 0);
            delay(10);
            analogWrite(redPin, 255);
            delay(200);
            analogWrite(redPin, 0);
            delay(10);
            analogWrite(redPin, 255);
            delay(200);
            analogWrite(redPin, 0);
            delay(10);
            analogWrite(redPin, 255);
            delay(200);
        }
        
        if (currVal >= 2.5)
        {
            analogWrite(bluePin, currVal*51);
            analogWrite(redPin, (5-currVal)*51);
        } 
        else 
        {
            analogWrite(redPin, currVal*51);
            analogWrite(bluePin, (5-currVal)*51);
        }
        delay(500);
        prevVal = currVal;
        token = strtok(NULL, ",");
        Serial.println(token);
   }
   
   //Ending Blinks
   analogWrite(bluePin, 0);
   analogWrite(redPin, 0);
   int i = 0;
   int j = 0;
   while (i < 255)
   {
        analogWrite(redPin, i);
        i++;
        delay(30);
   }
   while (i > 0)
   {
        analogWrite(redPin, i);
        i--;
        delay(30);
   }
   while (j < 255)
   {
        analogWrite(bluePin, j);
        j++;
        delay(30);
   }
    while (j > 0)
    {
        analogWrite(bluePin, j);
        j--;
        delay(30);
    }
    analogWrite(bluePin, 0);
    analogWrite(redPin, 0);
   
}

Value-to-Spreadsheet Code

JavaScript
Google Sheets script for collecting the rating into a spreadsheet format.
function collectCurrent() {
  "use strict";
  try
  {
    //Replace the device ID below with your Argon's unique device ID
    var deviceID = "e00fce684f8339cdb1ef322b";
    
    //Replace the access token below with your Argon's unique access token
    var accessToken = "704d34808d638c475a35f58dacebb28e0dd25869";
    
    var sheet = SpreadsheetApp.getActiveSheet();

    // Fetch the value of the testValue variable from the Spark Cloud API.
    // The name of your variable in Particle's cloud must match the variable in the URL below.
    var response = UrlFetchApp.fetch("https://api.particle.io/v1/devices/" + deviceID + "/rating?access_token=" + accessToken);

    // Parse the JSON and extract the testValue.
    var jsonResponse = JSON.parse(response.getContentText());
    var rating = jsonResponse.result;

    // Create a timestamp.
    var timeStamp = new Date();

    // Append the timestamp and the temperature to the sheet.
    sheet.appendRow([timeStamp, rating]);
  } catch (e)
  {	
    // If something doesn't work, log it, then rethrow the error.
	Logger.log(e.name + ": " + e.message);
	throw (e);
  }
}

Rating Collection

C/C++
Collects rating data on an interval and publishes a variable holding it.
// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>

// https://arduinojson.org/v6/assistant/
StaticJsonDocument<18000> doc;

double rating = 0;

void setup()
{
    // Subscribe to the webhook response event
    Particle.subscribe("hook-response/rating", myHandler, MY_DEVICES);
    Particle.variable("rating", rating);
    Serial.begin(9600);
}

void loop()
{
    //Activate webhook
    Particle.publish("rating");
    delay(5000);
}

void myHandler(const char *event, const char *data)
{
    
    rating = atof(data);
}

Credits

Marcin Gruda

Marcin Gruda

2 projects • 1 follower

Comments

Add projectSign up / Login