0

I am a student living in a student house together with 15 others. I am trying to make a attendance system that will display on a screen who is home and who is not. I thought it was a great plan to give everyone a RFID tag, so when they come home or leave they can check in and check out. This will than be represented on a screen displaying green or red circles (using processing).

I have used part of the code from https://www.youtube.com/channel/UC6LO26f_9qwysjvSHdVmfrQ and https://github.com/InfinityWorldHI/RFID_Excel for the arduino code.

I only programmed processing for the first two housemates. However when check in or check out both of the circles change color. I would like to have 2 rows of 8 circles that can change from red to green and the other way around to see if somebody is home.

My arduino program outputs the room nummer "," 1 or 0 (as checked in or checked out) output for example = 11,1.

The processing program will than draw a green or red circle depending on checking in or checking out.

Here is my processing code:

Arduino code (where it outputs to the serial port):


      if(NumbCard[j] == 1 && statu[s] == 0 && Number == 11) {
        statu[s]=1;
        NumbCard[j]=0;
        Serial.print(Number);
        Serial.print(",");
        Serial.println(1);
        //Serial.println("is uitgecheckt");
        //write led uit
      }
      else if(NumbCard[j] == 1 && statu[s] == 0 && Number == 22) {
        statu[s]=1;
        NumbCard[j]=0;
        Serial.print(Number);
        Serial.print(",");
        Serial.println(1);
        //Serial.println("is uitgecheckt");
        //write led uit
      }

Processing code to make the circles:

import processing.serial.*;

// ControlP5 Example 1 : Basic UI elements

import controlP5.*; // import controlP5 library
ControlP5 controlP5; // controlP5 object

Serial myPort;  // Create object from Serial class
int val;     // Data received from the serial port
int end = 10;    // Linefeed in ASCII
String myString = null;
int i =0;

PShape led_on, led_off;

String persoon_status;

color [] colors = new color[2]; 


void setup() {
  colors[0] = color(0,255,0);
  colors[1] = color(255,0,0);
   //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial (this, Serial.list()[0], 9600);
  //led_on = createShape(RECT,10,70,40,40,40);


 size(800,800);
}

void draw() { 
  background(255);
  do{
    myString = myPort.readStringUntil(end);
    if (myString != null) {
      println(myString);
    }
  }
  while (myPort.available() > 0); {

     if(myString != null && myString.trim().equals("11,1") == true) {
    fill(colors[1]);
    } else {
      if (myString != null && myString.trim().equals("11,0") == true)
      fill(colors[0]);
      else{
        rect(10,70,40,40,40);
      }
    }

   if(myString != null && myString.trim().equals("22,1") == true){
    fill(colors[1]);
   } else {
     if(myString != null && myString.trim().equals("22,0") == true)
     fill(colors[0]);
  else {
    rect(10,130,40,40,40);
    }
}
  }
}

I think I am close by to getting to the end, however I could not figure this problem out.

Could someone point me in the correct direction?

Please feel free to ask me for more information.

Your help would be really appreciated!

nielsoggel
  • 21
  • 2
  • You need to [break your problem down into smaller steps](https://happycoding.io/tutorials/how-to/program) and isolate the problem into a [mcve]. For example, can you confirm that the Arduino is sending the proper output? If so, then you can get rid of all that code and use hard-coded values in your Processing sketch. [Debug your code](https://happycoding.io/tutorials/processing/debugging) to narrow the problem down to a small example. Good luck. – Kevin Workman Mar 25 '19 at 17:12
  • Thank you for the tips! – nielsoggel Mar 26 '19 at 10:16

1 Answers1

0

When you set the fill() color, all shapes drawn from that point will have the same color until you set fill() with a new color. In your code you only change the color when an RFID is read, otherwise you just draw the shape, which means they all have the last set color.
Instead, you should use an array to store the status for each roommate. You can then loop through this array and set the fill to the appropriate color before drawing the rectangle.

This code snippet shows how you could implement this.

boolean[] isPresent = { false, false};

void draw() {
  // put the code that handles the RFID processing in
  // a separate function that you call in draw
  readRFID();

  background(255);
  //loop through the array
  for (int i = 0; i < isPresent.length; i++) {     
    //check status and set fill color
    if (isPresent[i]) {
      fill(colors[0]);
    } else {
      fill(colors[1]);
    }
    //draw rectangle with x-pos based on array index
    rect(10+i*50, 10, 40, 40);
  }
}
J.D.
  • 4,511
  • 2
  • 7
  • 20