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