Triggering Processing Sketch with Arduino

edited December 2017 in Arduino

I am currentlly working on a project, where a picture is being slowly revealed. I am using an rfid reader and tags, arduino and processing. The Arduino reads the tags through the rfid reader, and couples the ID of those tags to a certain value (e.g. tag ID 2A, 34, 4C, 29 is equal to value 1) The arduino code is fully functional, the problems lays in the processing code. When processing recieves a value (in this case either 1,2 or 3) it should trigger the action of the painting being slowly revealed.

Arduino Code

#include <MFRC522.h> // Include of the RC522 Library
#include <SPI.h> // Used for communication via SPI with the Module

#define SDAPIN 10 // RFID Module SDA Pin is connected to the UNO 10 Pin
#define RESETPIN 8 // RFID Module RST Pin is connected to the UNO 8 Pin



byte FoundTag; // Variable used to check if Tag was found
byte ReadTag; // Variable used to store anti-collision value to read Tag information
byte TagData[MAX_LEN]; // Variable used to store Full Tag Data
byte TagSerialNumber[5]; // Variable used to store only Tag Serial Number
byte GoodTagSerialNumber[5] = {0x95, 0xEB, 0x17, 0x53}; // The Tag Serial number we are looking for
byte GreenTagSerialNumber[5] = {0x3A, 0xB2, 0x9A, 0x29}; // The Tag Serial number we are looking for, 16, EF, 8D, C9, Card: 3A, B2, 9A, 29,
byte RedTagSerialNumber[5] = {0x2C, 0xE8, 0xAC, 0x79}; // The Tag Serial number we are looking for, 2C, E8, AC, 79,
byte YellowTagSerialNumber[5] = {0x1, 0x4C, 0x8E, 0xC9}; // The Tag Serial number we are looking for, 1, 4C, 8E, C9, 


MFRC522 nfc(SDAPIN, RESETPIN); // Init of the library using the UNO pins declared above

void setup() {

  SPI.begin();
  Serial.begin(115200);



  // Start to find an RFID Module
  Serial.println("Looking for RFID Reader");
  nfc.begin();
  byte version = nfc.getFirmwareVersion(); // Variable to store Firmware version of the Module

  // If can't find an RFID Module 
  if (! version) { 
    Serial.print("Didn't find RC522 board.");
    while(1); //Wait until a RFID Module is found
    }

  // If found, print the information about the RFID Module
  Serial.print("Found chip RC522 ");
  Serial.print("Firmware version: 0x");
  Serial.println(version, HEX);
  Serial.println();
}

void loop() {


  String GreenTag="False"; // Variable used to confirm good Tag Detected
  String RedTag="False"; // Variable used to confirm good Tag Detected
  String YellowTag="False"; // Variable used to confirm good Tag Detected

  // Check to see if a Tag was detected
  // If yes, then the variable FoundTag will contain "MI_OK"
  FoundTag = nfc.requestTag(MF1_REQIDL, TagData);

  if (FoundTag == MI_OK) {
    delay(200);

    // Get anti-collision value to properly read information from the Tag
    ReadTag = nfc.antiCollision(TagData);
    memcpy(TagSerialNumber, TagData, 4); // Write the Tag information in the TagSerialNumber variable

   Serial.println("Tag detected.");
   Serial.print("Serial Number: ");
    for (int i = 0; i < 4; i++) { // Loop to print serial number to serial monitor
      Serial.print(TagSerialNumber[i], HEX);
      Serial.print(", ");
      }
    Serial.println("");
    Serial.println();

 // Check if detected Tag is a GreenTag 
    for(int i=0; i < 4; i++){
      if (GreenTagSerialNumber[i] != TagSerialNumber[i]) {
        break; // if not equal, then break out of the "for" loop
        }
      if (i == 3) { // if we made it to 4 loops then the Tag Serial numbers are matching
        GreenTag="TRUE";
        } 
      }
    if (GreenTag == "TRUE"){
      Serial.println("Green Bottle Recognized");
      Serial.write(1);
      Serial.println();

      delay(1500);
      }
 // Check if detected Tag is a RedTag 
    for(int i=0; i < 4; i++){
      if (RedTagSerialNumber[i] != TagSerialNumber[i]) {
        break; // if not equal, then break out of the "for" loop
        }
      if (i == 3) { // if we made it to 4 loops then the Tag Serial numbers are matching
        RedTag="TRUE";
        } 
      }
    if (RedTag == "TRUE"){
      Serial.println("Red Bottle Recognized");
      Serial.write(2);
      Serial.println();

      delay(1500);
      }
  // Check if detected Tag is a YellowTag 
    for(int i=0; i < 4; i++){
      if (YellowTagSerialNumber[i] != TagSerialNumber[i]) {
        break; // if not equal, then break out of the "for" loop
        }
      if (i == 3) { // if we made it to 4 loops then the Tag Serial numbers are matching
        YellowTag="TRUE";
        } 
      }
    if (YellowTag == "TRUE"){
      Serial.println("Yellow Bottle Recognized");
      Serial.write(3);
      Serial.println();

      delay(1500);
      }
      delay(1500);     
    }
  }

Processing Code

PImage img;  // image (jpg) for the background

Splash[] Splashs; // Declare the array
int totalNumSplashs = 500;
int numSplashs = totalNumSplashs;

SplashLeft[] SplashsLeft; // Declare the array
int totalNumSplashsLeft = 500;
int numSplashsLeft = totalNumSplashsLeft;

SplashRight[] SplashsRight; // Declare the array
int totalNumSplashsRight = 500;
int numSplashsRight = totalNumSplashsRight;

int percentage = 100;
int stepsize = 10;

int percentageLeft = 100;
int stepsizeLeft = 10;

int percentageRight = 100;
int stepsizeRight = 10;

import processing.serial.*;
Serial myPort;
int val;

void setup() {

   myPort = new Serial(this, "COM5" , 115200);


  //size(600,400);
  fullScreen();
  noStroke();

  Splashs = new Splash[numSplashs]; // Create the array
  for (int i = 0; i < Splashs.length; i++) {
    Splashs[i] = new Splash(); // Create each object
  }

  SplashsLeft = new SplashLeft[numSplashsLeft]; // Create the array
  for (int i = 0; i < SplashsLeft.length; i++) {
    SplashsLeft[i] = new SplashLeft(); // Create each object
  }

  SplashsRight = new SplashRight[numSplashsRight]; // Create the array
  for (int i = 0; i < SplashsRight.length; i++) {
    SplashsRight[i] = new SplashRight(); // Create each object
  }

  img = loadImage("painting1_starry_night.jpg");  // load the background image
  image(img, 0, 0, width, height);
  keyPressed();
}

void draw(){
  /* 
  noStroke();
  fill(255);
  rect((width/3)-5, 0, 10, height);
  rect(2*(width/3)-5, 0, 10, height); 
  */
}

void sensor(){

  noStroke();
  image(img, 0, 0, width, height); 

    if (myPort.available()>0)
  {
    val = myPort.read();
  }
  println(val);

  //Change middle       
  if (val == 2) {
       for (int i = 0; i < percentage * Splashs.length /100; i++) {
           Splashs[i].display();
       } 

       for (int i = 0; i < percentageLeft * SplashsLeft.length /100; i++) {
           SplashsLeft[i].display();
       } 

       for (int i = 0; i < percentageRight * SplashsRight.length /100; i++) {
           SplashsRight[i].display();
       }

       percentage = percentage - stepsize;
       if (percentage < 50)
         stepsize = 6;
       if (percentage < 40)
         stepsize = 5;
       if (percentage < 10)
         stepsize = 1;
       if (percentage < 0)
         percentage = 0;
  }

  //Change left
  if (val == 1) {
       for (int i = 0; i < percentage * Splashs.length /100; i++) {
           Splashs[i].display();
       } 

       for (int i = 0; i < percentageLeft * SplashsLeft.length /100; i++) {
           SplashsLeft[i].display();
       } 

       for (int i = 0; i < percentageRight * SplashsRight.length /100; i++) {
           SplashsRight[i].display();
       }

       percentageLeft = percentageLeft - stepsizeLeft;
       if (percentageLeft < 50)
         stepsizeLeft = 6;
       if (percentageLeft < 40)
         stepsizeLeft = 5;
       if (percentageLeft < 10)
         stepsizeLeft = 1;
       if (percentageLeft < 0)
         percentageLeft = 0;
  }

  //Change right
  if (val == 3) {
       for (int i = 0; i < percentage * Splashs.length /100; i++) {
           Splashs[i].display();
       } 

       for (int i = 0; i < percentageLeft * SplashsLeft.length /100; i++) {
           SplashsLeft[i].display();
       } 

       for (int i = 0; i < percentageRight * SplashsRight.length /100; i++) {
           SplashsRight[i].display();
       }

       percentageRight = percentageRight - stepsizeRight;
       if (percentageRight < 50)
         stepsizeRight = 6;
       if (percentageRight < 40)
         stepsizeRight = 5;
       if (percentageRight < 10)
         stepsizeRight = 1;
       if (percentageRight < 0)
         percentageRight = 0;
  }

}

Class 1 class Splash {

  void display() {
    pushMatrix();
    translate(random((width/3)+5,2*(width/3)-5), random(height));
    fill(0);
    //fill(random(150,255), random(150,255), random(150,255));
    noStroke();
    int dropNum = int(map(random(1), 0, 1, 700, 1000));
    for (int i = 0; i < dropNum; i++) {
      float diameter = pow(random(1), 80);
      float distance = sq((1.0 - pow(diameter, 2)) * random(1));
      float scaledDiameter = map(diameter, 0, 1, 1, 65);
      float scaledDistance = map(distance, 0, 1, 0, 300);
      float radian = random(TWO_PI);
      ellipse(scaledDistance * cos(radian), scaledDistance * sin(radian), scaledDiameter, scaledDiameter);
    }
    popMatrix();
  }

}

Class 2 class SplashLeft {

  void display() {
    pushMatrix();
    translate(random(0,(width/3)-5), random(height));
    fill(0);
    //fill(random(150,255), random(150,255), random(150,255));
    noStroke();
    int dropNum = int(map(random(1), 0, 1, 700, 1000));
    for (int i = 0; i < dropNum; i++) {
      float diameter = pow(random(1), 80);
      float distance = sq((1.0 - pow(diameter, 2)) * random(1));
      float scaledDiameter = map(diameter, 0, 1, 1, 65);
      float scaledDistance = map(distance, 0, 1, 0, 300);
      float radian = random(TWO_PI);
      ellipse(scaledDistance * cos(radian), scaledDistance * sin(radian), scaledDiameter, scaledDiameter);
    }
    popMatrix();
  }

}

Class 3 class SplashRight {

  void display() {
    pushMatrix();
    translate(random(2*(width/3)+5, width), random(height));
    fill(0);
    //fill(random(150,255), random(150,255), random(150,255));
    noStroke();
    int dropNum = int(map(random(1), 0, 1, 700, 1000));
    for (int i = 0; i < dropNum; i++) {
      float diameter = pow(random(1), 80);
      float distance = sq((1.0 - pow(diameter, 2)) * random(1));
      float scaledDiameter = map(diameter, 0, 1, 1, 65);
      float scaledDistance = map(distance, 0, 1, 0, 300);
      float radian = random(TWO_PI);
      ellipse(scaledDistance * cos(radian), scaledDistance * sin(radian), scaledDiameter, scaledDiameter);
    }
    popMatrix();
  }

}
Tagged:
Sign In or Register to comment.