sending data with processing
in
Core Library Questions
•
6 months ago
Hello, I am new with processing and I am having troubles making it do what i want. I have code to send signals for my robot to move forward, back, left, right and I also have a set of commands for switching on different mods: walking, swimming and switching. However, ones one of the modes is on it should send 1 continuously while the other two send 0 as if they are inactive. I managed to make it working to just send 1 when the button is pressed, but can not make it work continuously ones the button the pressed, it sends very delayed signals in a strange order. Here is my code for processing:
- import processing.serial.*;
- Serial myPort; // Create object from Serial class
- int[] KeyArray = new int[7];
- int PrtChose = 6; //number of the serial port, starts at 0
- PFont f; //initailize a font
- void setup() {
- size(300, 400);
- noStroke();
- background(0);
- f = loadFont("ComicSansMS-48.vlw"); //Load Font
- textFont(f, 15); //Specify font type
- print(Serial.list());
- String[] Avail = Serial.list();
- for(int i =0; i < Avail.length; i++)
- {
- text(Avail[i], 10, 20*i+90); //print text of avaliable serial ports to app
- }
- fill(0,255,0);
- text(Avail[PrtChose], 10, 20*PrtChose+90); //highlite the port in use
- String portName = Serial.list()[PrtChose];
- myPort = new Serial(this, "/dev/cu.usbmodem621", 115200);
- }
- void draw()
- {
- String s = "Swimming";
- fill(204, 102, 0);
- text(s, 185, 12, 70, 80); // Text wraps within text box
- String t = "Switching";
- fill(204, 102, 0);
- text(t, 185, 32, 70, 80); // Text wraps within text box
- String u = "Walking";
- fill(204, 102, 0);
- text(u, 185, 52, 70, 80); // Text wraps within text box
- //chnage colors of 7 ellipses according to what keys are pressed
- if (KeyArray[0] == 1)
- {
- fill(0, 255, 0);
- }
- else
- {
- fill(255, 0, 0);
- }
- ellipse(100, 20, 10, 10); //fwd
- if (KeyArray[1] == 1)
- {
- fill(0, 255, 0);
- }
- else
- {
- fill(255, 0, 0);
- }
- ellipse(100, 40, 10, 10); //bck
- if (KeyArray[2] == 1)
- {
- fill(0, 255, 0);
- }
- else
- {
- fill(255, 0, 0);
- }
- ellipse(120, 40, 10, 10); //lft
- if (KeyArray[3] == 1)
- {
- fill(0, 255, 0);
- }
- else
- {
- fill(255, 0, 0);
- }
- ellipse(80, 40, 10, 10); //rght
- if (KeyArray[4] == 1)
- {
- fill(0, 255, 0);
- ellipse(170, 20, 15, 15); //swimming
- fill(255, 0, 0);
- ellipse(170, 40, 15, 15); //switching
- fill(255, 0, 0);
- ellipse(170, 60, 15, 15); //walking
- }
- if (KeyArray[5] == 1)
- {
- fill(0, 255, 0);
- ellipse(170, 40, 15, 15); //switching
- fill(255, 0, 0);
- ellipse(170, 20, 15, 15); //swimming
- fill(255, 0, 0);
- ellipse(170, 60, 15, 15); //walking
- }
- if (KeyArray[6] == 1)
- {
- fill(0, 255, 0);
- ellipse(170, 60, 15, 15); //walking
- fill(255, 0, 0);
- ellipse(170, 40, 15, 15); //switching
- fill(255, 0, 0);
- ellipse(170, 20, 15, 15); //swimming
- }
- }
- void keyReleased() //key released
- {
- if (key == 'w') //fwd, fwd has been let up, so set stop fwd message
- {
- KeyArray[0] = 0; //no longer pressed
- }
- if (key == 's') //down
- {
- KeyArray[1] = 0;
- }
- if (key == 'd') //right
- {
- KeyArray[2] = 0;
- }
- if (key == 'a') //left
- {
- KeyArray[3] = 0;
- }
- writeMsg(); //send updated state over serial
- }
- void keyPressed() //key pressed
- {
- if (key == 'w') //fwd
- {
- KeyArray[0] = 1; //fwd key pressed down, set forward command
- }
- if (key == 's') //down
- {
- KeyArray[1] = 1;
- }
- if (key == 'd') //right
- {
- KeyArray[2] = 1;
- }
- if (key == 'a') //left
- {
- KeyArray[3] = 1;
- }
- if (key == 'j') //swimm
- {
- KeyArray[4] = 1;
- // KeyArray[5] = 0;
- // KeyArray[6] = 0;
- }
- if (key == 'k') //switch
- {
- // KeyArray[4] = 0;
- KeyArray[5] = 1;
- // KeyArray[6] = 0;
- }
- if (key == 'l') //walk
- {
- // KeyArray[4] = 0;
- // KeyArray[5] = 0;
- KeyArray[6] = 1;
- }
- writeMsg();
- }
- void writeMsg()
- {
- //so both fwd/back, lft/rght dont activate at same time
- if(KeyArray[0] == 1 && KeyArray[1] == 1)
- {
- KeyArray[0] = 0;
- KeyArray[1] = 0;
- }
- if(KeyArray[2] == 1 && KeyArray[3] == 1)
- {
- KeyArray[2] = 0;
- KeyArray[3] = 0;
- }
- if(KeyArray[4] == 1 && KeyArray[5] == 1 && KeyArray[6] == 1)
- {
- KeyArray[4] = 0;
- KeyArray[5] = 0;
- KeyArray[6] = 0;
- }
- if(KeyArray[4] == 1 && KeyArray[5] == 1)
- {
- KeyArray[4] = 0;
- KeyArray[5] = 0;
- }
- if(KeyArray[5] == 1 && KeyArray[6] == 1)
- {
- KeyArray[5] = 0;
- KeyArray[6] = 0;
- }
- if(KeyArray[4] == 1 && KeyArray[6] == 1)
- {
- KeyArray[4] = 0;
- KeyArray[6] = 0;
- }
- //<0,0,0,0,0,0,0> <fwd,bck,rght,Lft,swimm,switch,walk>
- myPort.write("<" + KeyArray[0] + "," + KeyArray[1] + "," + KeyArray[2] + "," + KeyArray[3] + "," + KeyArray[4] + "," + KeyArray[5] + "," + KeyArray[6] + ">"); //write to serial
- println("<" + KeyArray[0] + "," + KeyArray[1] + "," + KeyArray[2] + "," + KeyArray[3] + "," + KeyArray[4] + "." + KeyArray[5] + "." + KeyArray[6] + ">"); //display in console
- }
- int started = 0;
- char inData[10];
- int ended = 0;
- char index = 0;
- int final = 0;
- boolean Fwd = 0;
- boolean Bck = 0;
- boolean Rght = 0;
- boolean Lft = 0;
- boolean Swm = 0;
- boolean Swtch = 0;
- boolean Wlk = 0;
- void setup()
- {
- Serial.begin(115200);
- }
- void loop()
- {
- GetBluData(); //input like: <0,0,0,0,0,0,0> then splits and writes values to F,B,R,L,Swm,Swtch,Wlk
- Serial.print(Fwd);
- Serial.print(",");
- Serial.print(Bck);
- Serial.print(",");
- Serial.print(Rght);
- Serial.print(",");
- Serial.print(Lft);
- Serial.print(",");
- Serial.print(Swm);
- Serial.print(",");
- Serial.print(Swtch);
- Serial.print(",");
- Serial.print(Wlk);
- Serial.print(",");
- Serial.print(",");
- Serial.println();
- }
- void GetBluData()
- {
- while(Serial.available() )
- {
- //finds < and >, the beginning and end of command
- char aChar = Serial.read();
- if(aChar == '<')
- {
- started = true;
- index = 0;
- inData[index] = '\0';
- }
- else if(aChar == '>')
- {
- ended = true;
- }
- else if(started)
- {
- inData[index] = aChar;
- index++;
- inData[index] = '\0';
- }
- else if (aChar =='*')
- {
- final = true;
- }
- }
- if(started && ended)
- {
- const char* strDelimiter = ",";
- char* p;
- //<0,0,0,0,0,0,0,0>
- //splits to individual ints
- if ( p = strtok(inData, strDelimiter) )
- {
- Fwd = atoi(p);
- }
- if ( p = strtok(NULL, strDelimiter) )
- {
- Bck = atoi(p);
- }
- if ( p = strtok(NULL, strDelimiter) )
- {
- Rght = atoi(p);
- }
- if ( p = strtok(NULL, strDelimiter) )
- {
- Lft = atoi(p);
- }
- if ( p = strtok(NULL, strDelimiter) )
- {
- Swm = atoi(p);
- }
- if ( p = strtok(NULL, strDelimiter) )
- {
- Swtch = atoi(p);
- }
- if ( p = strtok(NULL, strDelimiter) )
- {
- Wlk = atoi(p);
- }
- // Get ready for the next time
- started = false;
- ended = false;
- index = 0;
- inData[index] = '\0';
- }
- }
1