Before pasting my code let me explain the interface. I would briefly like to mention that none of the textfields to the right of the interface will be visible in the final version - for reasons that will be made clear later.
Input into the textfield "MARGIN" is linked to all of the other fields via "keepFocus(true);". So, as I type, the text appears in all of the other textfields. The buttons submit each textfield individually. I.E. the button named "LIBBE" submits the textfield "LIBBE". It does not submit any textfield except to the ONE which it refers. this is necessary because each textfield has a different affix, "U", "D", "L", "R" etc. this is appended to the textfield input. so when i hit a button it sends a string ( e.g. <R1000> ) to arduino. arduino reads this string an interprets accordingly. to if it finds the character "R" in the string, it will move a stepper motor R / Right 1000 steps.
My problem is that if I hit ENTER in the textfield "Margin", then it submits ALL of the texfields. So Arduino receives <U1000>, <D,1000>, <L1000> and <R1000> and so naturally, all of the motors are firing off in all directions. This is not a problem if I DONT hit enter and use the buttons instead. However, I would like to find a way to disable ENTER all-together, to avoid this problem. If I do, by accident hit enter, then it might have quite grave consequences given the application. (I mean someone could quite easily get knocked out if they are not paying attention!)
I did try setBroadcast and setAutoClear, however, I do actually need to be able to submit the value, but not via the shortcut ENTER. I need to use the buttons instead.
I hope this makes sense. I will paste my code now. Its quite long..
- import processing.serial.*; // serial library
- // GUI variables
- import controlP5.*; // controlP5 library
- ControlP5 controlP5; // create the handler to allow for controlP5 items
- Serial port;
-
-
- Textfield MARGImyTextfield;
- Textfield UPPmyTextfield;
- Textfield DOUNmyTextfield;
- Textfield LIBBmyTextfield;
- Textfield RIHTmyTextfield;
- Textfield RTmyTextfield;
- Textfield AUTOSCAmyTextfield;
- Textfield SCAmyTextfield;
- Textfield CLEAmyTextfield;
-
- // setup
- void setup() {
- size(600,400);
- frameRate(30);
- controlP5 = new ControlP5(this); // initialize the GUI controls
- controlP5.disableShortcuts();
- port = new Serial(this, "COM8", 9600);
- println("Available serial ports:");
- println(Serial.list());
- MARGImyTextfield = controlP5.addTextfield ("MARGIN",100,100,125,15); // addTextfield(name,x,y,width,height)
- MARGImyTextfield.setAutoClear(false);
- MARGImyTextfield.keepFocus(true);
- UPPmyTextfield = controlP5.addTextfield ("UPP",350,100,125,15); // addTextfield(name,x,y,width,height)
- UPPmyTextfield.setAutoClear(false);
- UPPmyTextfield.keepFocus(true);
- DOUNmyTextfield = controlP5.addTextfield ("DOUN",350,140,125,15); // addTextfield(name,x,y,width,height)
- DOUNmyTextfield.setAutoClear(false);
- DOUNmyTextfield.keepFocus(true);
- ;
- RIHTmyTextfield = controlP5.addTextfield ("RIHT",350,220,125,15); // addTextfield(name,x,y,width,height)
- RIHTmyTextfield.setAutoClear(false);
- RIHTmyTextfield.keepFocus(true);
- LIBBmyTextfield = controlP5.addTextfield ("LIBB",350,180,125,15); // addTextfield(name,x,y,width,height)
- LIBBmyTextfield.setAutoClear(false);
- LIBBmyTextfield.keepFocus(true);
- SCAmyTextfield = controlP5.addTextfield ("SCA",350,260,125,15); // addTextfield(name,x,y,width,height)
- SCAmyTextfield.setAutoClear(false);
- SCAmyTextfield.keepFocus(true);
- AUTOSCAmyTextfield = controlP5.addTextfield ("AUTOSCA",350,260,125,15); // addTextfield(name,x,y,width,height)
- AUTOSCAmyTextfield.setAutoClear(false);
- AUTOSCAmyTextfield.keepFocus(true);
- RTmyTextfield = controlP5.addTextfield ("RT",350,300,125,15); // addTextfield(name,x,y,width,height)
- RTmyTextfield.setAutoClear(false);
- RTmyTextfield.keepFocus(true);
- controlP5.addButton ("UPPE", 1,165,180,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("DOUNE", 1,165,220,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("RIHTE", 1,230,200,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("LIBBE", 1,100,200,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("RTN", 1,100,300,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("AUTOSCAN", 1,165,300,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("SCAN", 1,230,300,40,16); // buton(name,value,x,y,width,height)
- controlP5.addButton ("CLEAR", 1,230,100,40,16); // buton(name,value,x,y,width,height)
- }
- // infinite loop
- void draw() {
- background(0);
- if (key == KeyEvent.VK_ENTER);
- key = 0;
- //////////////////////////////////////////////////////////////////////
- // //TEXTFIELD
- }
-
-
- public void UPP(String theText) {
- port.write('<'+"U"+theText+'>'); // write the text in the field
- println("sent"+theText); // print it to the debug screen as well
- }
- public void DOUN(String theText) {
- port.write('<'+"D"+theText+'>'); // write the text in the field
- println("sent"+theText); // print it to the debug screen as well
- }
- public void RIHT(String theText) {
- port.write('<'+"R"+theText+'>'); // write the text in the field
- println("sent"+theText); // print it to the debug screen as well
- }
- public void LIBB(String theText) {
- port.write('<'+"L"+theText+'>'); // write the text in the field
- println("sent"+theText); // print it to the debug screen as well
- }
- public void SCA(String theText) {
- port.write('<'+"S"+theText+'>'); // write the text in the field
- println("sent"+theText); // print it to the debug screen as well
- }
- public void AUTOSCA(String theText) {
- port.write('<'+"A"+theText+'>'); // write the text in the field
- println("sent"+theText); // print it to the debug screen as well
- }
- public void RT(String theText) {
- port.write('<'+"H"+theText+'>'); // write the text in the field
- println("sent"+theText); // print it to the debug screen as well
- }
- //////////////////////////////////////////////////////////////////////
- // //SUBMIT
- void UPPE(int theValue) {
- UPPmyTextfield.submit();
- println("sent X"); // print it to the debug screen as well
- }
- void DOUNE(int theValue) {
- DOUNmyTextfield.submit();
- println("sent X"); // print it to the debug screen as well
- }
- void RIHTE(int theValue) {
- RIHTmyTextfield.submit();
- println("sent X"); // print it to the debug screen as well
- }
- void LIBBE(int theValue) {
- LIBBmyTextfield.submit();
- println("sent X"); // print it to the debug screen as well
- }
- void SCAN(int theValue) {
- SCAmyTextfield.submit();
- println("sent X"); // print it to the debug screen as well
- }
- void AUTOSCAN(int theValue) {
- AUTOSCAmyTextfield.submit();
- println("sent X"); // print it to the debug screen as well
- }
- void RTN(int theValue) {
- RTmyTextfield.submit();
- println("sent X"); // print it to the debug screen as well
- }
- void CLEAR(int theValue) {
- MARGImyTextfield.clear();
- UPPmyTextfield.clear();
- DOUNmyTextfield.clear();
- RIHTmyTextfield.clear();
- LIBBmyTextfield.clear();
- SCAmyTextfield.clear();
- AUTOSCAmyTextfield.clear();
- RTmyTextfield.clear();
- println("sent X"); // print it to the debug screen as well
- }
-
- //////////////////////////////////////////////////////////////////////
- void serialEvent(Serial aPort)
- {
- // read the serial buffer:
- String myString = aPort.readStringUntil('\n');
- // make sure you have a valid string
- if (myString != null)
- {
- // trim off the whitespace (linefeed, carriage return) characters:
- myString = trim(myString);
- println(myString);
- }
- }
Kind Regards, Arthur