Objects in my ArrayList seem to auto-inherit previous properties...?
in
Programming Questions
•
2 years ago
Hey all, new to the forum, but have been knocking about with Processing for a while.
I am using an Arduino based XBee network for wireless sensor data transfer. The XBee connected to the computer handles incoming 'packets'. It simply gets a packet then unpacks it. Depending on the 16 bit network address of the XBee modem that send the message, it should then evaluate whether it has spoken to it before by unpacking every node (or XBee modem) from the ArrayList and comparing it's stored address with the incoming one.
The problem starts in Line 61 below. Upon unpacking the position 0 object in the list, and then instantly printing out its address, it has already managed to gain the incoming address from the computerGateway object!
Which means that all the comparisons afterwards will always say that the current Node has already been found.
I can only imagine that there is some sort of problem in the class code for the XBee, which I will attach in the next post.
I appreciate that the stuff I'm working on is probably a bit messy and therefore confusing, but any light on why an object that I created last time through the loop, thinks that it has the properties of one that should be about to be created this time, would be really helpful!
- import processing.serial.*;
- Serial port;
- // make an ArrayList that will store all Nodes that join the network.
- ArrayList nodeList = new ArrayList();
- // instead of just creating a 'Gateway' here, we can queery the local modem for it's profile then create it?
- int[] computerGatewayAddress64 = {0x00,0x13,0xA2,0x00,0x40,0x7A,0x1F,0x67} ;
- int[] computerGatewayAddress16 = {0xFF,0xFE} ;
- Xbee computerGateway = new Xbee(computerGatewayAddress64,computerGatewayAddress16);
- //SystemTime SystemTime = new SystemTime();
- PFont f;
- void setup() {
- size(500, 500);
- println("");
- println("Available serial ports:");
- println(Serial.list());
- port = new Serial(this, Serial.list()[1], 9600);
- println("");
- f = loadFont("ArialMT-16.vlw");
- textFont(f,16);
- fill(0);
- text("Wireless Environment Sensor Network",10,20);
- // add a dummy Node...
- // nodeList.add(new Xbee(computerGatewayAddress64,computerGatewayAddress16)) ;
- }
- void draw() {
- background(255);
- // check if packets are incoming...
- if (port.available() < 1) {
- // no incoming info so proceed as normal
- }
- else {
- // there's incoming activity, check to see if it's a packet!
- computerGateway.getPacket();
- // ONLY CONTINUE IF WE GOT A GOOD PACKET?
- if (computerGateway.packet != null) {
- // extract all the data from the new packet
- computerGateway.unPackage();
- // check to see if we havn't found any nodes yet...
- if (nodeList.size() == 0) {
- println("First contact with network! Making a Node...") ;
- nodeList.add(new Xbee(computerGateway.incomingAddress64, computerGateway.incomingAddress16)) ;
- }
- if (nodeList.size() != 0) {
- println("We have " + nodeList.size() + " Nodes. Checking to see if incoming is new...");
- }
- // check to see if new address
- boolean foundNewNode = false ;
- for (int i = nodeList.size()-1; i >= 0; i--) {
- int similarBytes = 0 ;
- Xbee tempoaryNode = (Xbee) nodeList.get(i) ;
- println("Opened Node " + i);
- println("16 Bit Address of: " + hex(tempoaryNode.address16[0],2) + hex(tempoaryNode.address16[1],2)) ;
- // why is this magically equal to the new incoming address?!?!?!
- for (int j=0; j<2; j++) {
- if (computerGateway.incomingAddress16[j] == tempoaryNode.address16[j]) {
- // we have found a match, but doesn't mean the whole address is the same!
- similarBytes ++ ;
- }
- if (computerGateway.incomingAddress16[j] != tempoaryNode.address16[j]) {
- // the bytes are different, this is enough to prove that the incoming message isn't from the tempoaryNode
- foundNewNode = true ;
- break ;
- }
- if (similarBytes == 2) {
- // found an existing Node
- foundNewNode = false ;
- // load the Node for update / interaction
- Xbee currentNode = (Xbee) nodeList.get(i) ;
- println("Opened existing Node!") ;
- println("") ;
- break;
- }
- }
- }
- if (foundNewNode) {
- // make an object for it and add it to the list
- nodeList.add(new Xbee(computerGateway.incomingAddress64, computerGateway.incomingAddress16)) ;
- // then load the Node for update / interaction
- Xbee currentNode = (Xbee) nodeList.get(nodeList.size()-1) ;
- println("Made and opened a new Node!") ;
- println("") ;
- }
- // save the data in the packet into the archive file for the relevant node
- }
- }
- // data has now been discovered, decoded, assigned to an object relating to it's origin and archived accordingly.
- // END OF RECEIVING DATA FROM THE NETWORK
- for (int i = nodeList.size()-1; i >= 0; i--) {
- Xbee renderNode = (Xbee) nodeList.get(i) ;
- renderNode.render(i) ;
- }
- text("Number of Nodes found:",10,20);
- text(nodeList.size(),10,35);
- }
1