Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
OwenRyan CassadaThomas McAllister
Published © GPL3+

Numeric Guessing Game

This is a Particle Argon program designed to execute a risk-reward game where players have to shoot for the highest number without matching.

IntermediateFull instructions provided8 hours194
Numeric Guessing Game

Things used in this project

Story

Read more

Schematics

Thomas's Schematic

This is a schematic showing how Craig (the Argon) was wired in order to optimize joystick inputs and LCD screen outputs

Ryan's Schematic

This is a schematic showing the keypad, button and led connections for Ryan's Particle Argon

Owen's Schematic

This is a schematic of Owen's Particle Argon. Nothing is connected for Owens Particle Argon as its role is to act as a terminal for the other two argons.

Code

Thomas's Code

C/C++
This code is used on Thomas's Particle Argon, affectionately named "Craig". The Code is used to take data from an Elegoo joystick module and publish the data as well as display the users numeric selection on an LCD display.
// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>

// This #include statement was automatically added by the Particle IDE.
#include <LiquidCrystal.h>

// This #include statement was automatically added by the Particle IDE.
TCPClient client;

unsigned long myChannelNumber = 1932351;    // change this to your channel number
const char * myWriteAPIKey = "UYZHL2Y79H7GDGG3";

int Light1 = A5;
// The following is code for the joystick input for the MEGR 3171 IoT project
const int SW_pin = D7;
// analog pin connected to X output
const int X_pin = A1;
// analog pin connected to Y output
const int Y_pin = A0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(D1, D2, D3, D4, D5, D6);
void setup() {
    ThingSpeak.begin(client);
      pinMode(Light1, OUTPUT);
     Particle.subscribe("toggle-CRAIGLED", CRAIGLED , MY_DEVICES);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Hello, World!");

    pinMode(SW_pin, INPUT);
    digitalWrite(SW_pin, HIGH);
    Serial.begin(9600);
}

void CRAIGLED(const char *event, const char *data) {
    delay(1000);
 digitalWrite(Light1, HIGH);

}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 0);
lcd.print("Your input:   ");

analogRead(X_pin);
analogRead(Y_pin);

if ((analogRead(X_pin)>4000) && ((4000>analogRead(Y_pin)) && (analogRead(Y_pin)>2000))) {
lcd.setCursor(0, 1);
lcd.print("1        ");
Particle.publish("Craig1", PRIVATE);
String CKEY = String(1);
   
ThingSpeak.writeField(myChannelNumber, 2, CKEY, myWriteAPIKey);
delay(16000);
}
if (((4000>analogRead(X_pin)) && (analogRead(X_pin)>2000)) && (analogRead(Y_pin)>4000)) {
lcd.setCursor(0, 1);
lcd.print("2        ");
Particle.publish("Craig2", PRIVATE);
String CKEY = String(2);
   
ThingSpeak.writeField(myChannelNumber, 2, CKEY, myWriteAPIKey);
delay(16000);
}
if ((1000>analogRead(X_pin)) && ((4000>analogRead(Y_pin)) && (analogRead(Y_pin)>2000))) {
lcd.setCursor(0, 1);
lcd.print("3        ");
Particle.publish("Craig3", PRIVATE);
String CKEY = String(3);
   
ThingSpeak.writeField(myChannelNumber, 2, CKEY, myWriteAPIKey);
delay(16000);
}
if (((4000>analogRead(X_pin)) && (analogRead(X_pin)>2000)) && (1000>analogRead(Y_pin))) {
lcd.setCursor(0, 1);
lcd.print("4        ");
Particle.publish("Craig4", PRIVATE);
String CKEY = String(4);
   
ThingSpeak.writeField(myChannelNumber, 2, CKEY, myWriteAPIKey);
delay(16000);
}
// print the number of seconds since reset:
// lcd.print(analogRead(X_pin));
// lcd.setCursor(0, 1);
// lcd.print(analogRead(Y_pin));
delay(1000);
}

Ryan's Code

C/C++
This code is uploaded to Ryan's Particle Argon, aptly named "Ryan's Argon". This code is used to read data from a keypad and publish the data and toggle an LED based on game status
// This #include statement was automatically added by the Particle IDE.
#include <ThingSpeak.h>     //Can find these in the Libraries section of "https://build.particle.io" Particle Web IDE. Libraries is below "<>" Code section.

// This #include statement was automatically added by the Particle IDE.
#include <Keypad_Particle.h>

 //To monitor events and publishes, go to Console Section
 //Before flashing code, make sure the device you want to flash is selected with a yellow star in the Devices section


TCPClient client;

unsigned long myChannelNumber = 1932351;    // this is our channel number
const char * myWriteAPIKey = "UYZHL2Y79H7GDGG3";    // this is our channels write API key

int button = D0;
int Light1 = D1;
int LED = D7;


const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 4; //4 columns



char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','1','1','1'},
  {'2','2','2','2'},
  {'3','3','3','3'},
  {'4','4','4','4'}
};

byte pin_rows[ROW_NUM] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {4, 3, 2,1}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );


void setup()
{
ThingSpeak.begin(client);
    Serial.begin(9600);
     Particle.variable("RCC",keys[ROW_NUM][COLUMN_NUM]);
       // Configure the pins to be outputs

      //Register our Particle function here

  pinMode(button, INPUT_PULLDOWN);
  pinMode(LED, OUTPUT);
  pinMode(Light1, OUTPUT);
  pinMode(Light2, OUTPUT);
  pinMode(Light3, OUTPUT);
  Particle.subscribe("toggle-RCCLED", RCCLED , MY_DEVICES);

}

//void RCCLED picks up the event and allows you to write an action when the event you are subscribed to is published.
void RCCLED(const char *event, const char *data) {
    delay(1000);                //Delays make code work smoother and more reliable
 digitalWrite(Light1, HIGH);    //When owens argon publishes "toggle-RCCLED", then led connected to D1 goes on.

}


void loop()
{
   
     
   char key = keypad.getKey();
if (key){
    Serial.println(key);
   
     String RCC = String(key);
    Particle.publish("RCC", String(key), PUBLIC);
ThingSpeak.writeField(myChannelNumber, 1, RCC, myWriteAPIKey);  // the '1' is for the field 1 chart. As an example, for field 3 chart, use: ThingSpeak.writeField(myChannelNumber, 3, RCC, myWriteAPIKey);
delay(16000);       // Needs minimum of 15 seconds to post a new variable to thingspeak. adding an additional second was more reliable.
 
 
}


  if (digitalRead(button) == LOW) {
    digitalWrite(LED,HIGH);
    delay(1000);
    Particle.publish("toggle-on", PRIVATE);     //For D7 LED Turn on
}
   

if (digitalRead(button) == HIGH){
   digitalWrite(LED,LOW);
    delay(1000);
    Particle.publish("toggle-off", PRIVATE);    //Constantly runs and keeps D7 LED off when button is not pressed.
}

 

}
//References
// Particle Library: <Keypad_Particle.h>
// Code for Keypad: https://arduinogetstarted.com/tutorials/arduino-keypad
// Reference code for LED: https://arduinogetstarted.com/tutorials/arduino-keypad

Owen's Code

C/C++
This code is used on Owen's Particle Argon, dubbed "Referee". This code is used as a communication bridge between Thomas's Particle Argon And Ryan's Particle Argon. The light will flash to high whenever data passes through. Acting as a central terminal, this Argon sends and receives data from both partner-Argons.
int LED = D7;

void setup() {
 pinMode(LED, OUTPUT);
 digitalWrite(LED, LOW);
Particle.subscribe("toggle-on", toggleLed , MY_DEVICES);
Particle.subscribe("toggle-off", toggleLedF , MY_DEVICES);
Particle.subscribe("Craig1", Craig1 , MY_DEVICES);
Particle.subscribe("Craig2", Craig2 , MY_DEVICES);
Particle.subscribe("Craig3", Craig3 , MY_DEVICES);
Particle.subscribe("Craig4", Craig4 , MY_DEVICES);
}

void toggleLed(const char *event, const char *data) {
    delay(1000);
 digitalWrite(D7, HIGH);
 Particle.publish("toggle-RCCLED", PRIVATE);
}

void toggleLedF(const char *event, const char *data) {
    delay(1000);
 digitalWrite(D7, LOW);
 }
 
 void Craig1(const char *event, const char *data) {
    delay(1000);
 digitalWrite(D7, LOW);
 Particle.publish("toggle-CRAIGLED", PRIVATE);
 }
 
 void Craig2(const char *event, const char *data) {
    delay(1000);

 }
 void Craig3(const char *event, const char *data) {
    delay(1000);

 }
 void Craig4(const char *event, const char *data) {
    delay(1000);

 }
 
 void loop() {

}

Game Logic

MATLAB
This is game logic that can be implemented to add a random numeric generation to the game where higher numbers are more likely to be chosen. It adds a second layer of risk/reward to the game as matching the mystery number results in disqualification. To play this game without an argon, feel free to copy and paste the code into MATLAB, changing lines 13 and 15 to require an input number to set a value for RCC and Thomas.
% Enter your MATLAB Code below

clear;
close all;
clc;
% 40% chance of 4, 30% chance of 3, 20% chance of 2, and 10% chance of 1 
x = [1,2,2,3,3,3,4,4,4,4];
pos=randi(length(x));
SecretNumber=x(pos);
%Player RCC
%Player Thomas

RCC = ThingSpeakRead(1932351,RCC) ;

Thomas = ThingSpeakRead(1932351,CKEY) ;

DidRyanWin=true; RyanWins=0;
DidThomasWin=true; ThomasWins=0;
if RCC == SecretNumber
    DidRyanWin=false;
    RyanWins=2;
end
if Thomas == SecretNumber
    DidThomasWin = false;
    ThomasWins=2;
end

RCCDev=abs(SecretNumber-RCC); ThomasDev=abs(SecretNumber-Thomas);

if DidRyanWin == true && DidThomasWin == true && RCCDev~=ThomasDev
if RCCDev<ThomasDev && DidRyanWin==true
    RyanWins=1;
    ThomasWins=2;
elseif RCCDev>ThomasDev && DidThomasWin==true
    ThomasWins=1;
    RyanWins=2;
end
elseif DidRyanWin==true && DidThomasWin==true && RCCDev==ThomasDev
if RCC>Thomas && DidRyanWin==true
    RyanWins=1;
    ThomasWins=2;
elseif RCC<Thomas && DidThomasWin==true
    ThomasWins=1;
    RyanWins=2;
end
end

if DidRyanWin==true && DidThomasWin==false
    RyanWins=1;
elseif DidRyanWin==false && DidThomasWin==true
    ThomasWins=1;
end

if RyanWins==1
    disp "Ryan_Wins"
end

Credits

Owen
1 project • 2 followers
Contact
Ryan Cassada
0 projects • 1 follower
Contact
Thomas McAllister
0 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.