Using Microsoft Visual Programming Language?

Hi all,

I have a quick question,

Can I use any of the existing firmata component libraries to make VPL communicate with the arduino? Has anyone used VPL before?

I was learning processing but I am having trouble to get it to do all of the things I want it to, because when it is doing one task it seems to pause all other tasks, so I was going to try VPL, as I am a novice and believe I would find a visual language easier than learning, say C, for the task I am doing.

Thanks

Rendeverance

I am a novice and believe I would find a visual language easier than learning, say C

Then you have a real misunderstanding of what the Visual part of Visual C++, Visual Basic, or Visual C# is about.

You still have to write the code. The visual part is about the user interface design.

I was learning processing but I am having trouble to get it to do all of the things I want it to, because when it is doing one task it seems to pause all other tasks, so I was going to try VPL

And, how do you propose to be able to do two or more things at once?

Posting the Processing code, and explaining the issues is more likely to produce results that starting over in another language/IDE.

Is the OP not referring to this - VPL Introduction | Microsoft Docs ?

Hi dxwood, yes I am referring to VPL Introduction | Microsoft Docs

I have not used this before see. I understand I still need to write code, this does not bother me but I think I will work quicker if I can lay out my processes in a visual manner like VPL provides.

When I say I want to do more than one thing at once I mean that with processing if I press one button on my GUI, other button presses are not even considered before the sequence is finished... which seems to make it impossible to have a 'stop' button which provides an interrupt. I also have the issue that all of my analog values which are constantly updated on my GUI stop being updated when a sequence (initiated by a button press) is running.

The reason I have yet to post my processing code is that a) its huge and b) I dont think people can easily re-create my scenario without my hardware setup (on the arduino side)

when a sequence (initiated by a button press) is running.

Changing to a visual interface won't help with this. You have to stop writing code that blocks this way.

We'd need to see what this "sequence" is doing, but I'd guess that it is using delay() and loops that don;t check for input.

Ok PaulS,

Thank you, I shall try and create a small representation of what I am trying to do and post it on to see whats happening.

Essentially at this stage I want to have a GUI which has just 4 buttons, 3 start sequences and one that will stop any sequence which is running. Only one sequence can run at a time and the sequence running is reliant upon certain analog values being reached, so the analog values need to be constantly updated or the process will get stuck. I also want there to be a button attached to the arduino which closes a digital pin and would result in stopping any sequence which is running.

I know it will not help me with this issue but out of curiosity, is the microsoft VPL compatible with any of the existing firmata components? It is only that I might look toward using visual studio in the future.

Thanks again,

Matthew

Essentially at this stage I want to have a GUI which has just 4 buttons, 3 start sequences and one that will stop any sequence which is running. Only one sequence can run at a time and the sequence running is reliant upon certain analog values being reached, so the analog values need to be constantly updated or the process will get stuck.

This sounds like a purely PC application problem, having little to do with what the Arduino is doing.

I know it will not help me with this issue but out of curiosity, is the microsoft VPL compatible with any of the existing firmata components?

I don't know, but I highly doubt it.

There is nothing stopping you from ditching Firmata, and developing your own protocol.

rendeverance,
If you think in terms of communication through the usb .. you have all that you need. Make button-presses in your gui send a serial message and write code in arduino to 'translate' that message.
And, it's a two way street.
On the nitty-gritty side, it could get cumbersome to build something blasingly fast.
I posted a heap of vb-code for inspiration

It essentially produces a 3d model of a infrared scanned object.

As for getting stuck in an eventhandler .. it's a question of setting your code right.

Hi thanks guys :slight_smile:

I need to work on this code business, I will look at your examples and see if I can gleam inspiration. I mean I don't need it blisteringly quick but I need it accurate within 1 second really.

I think my main issue is getting everything to work together, none of the things I want it to do are particularly complex, its just that I have many things going on at once and I am having trouble tying them together!

Are there any examples specifically applicable to performing a sequence of events (which is feedback dependent) while having an option to pause and / or interrupt the sequence?

Thanks again for your kind support,

Are there any examples specifically applicable to performing a sequence of events (which is feedback dependent) while having an option to pause and / or interrupt the sequence?

Without having any idea what you are doing, the only possible answer to this question is Probably.

rendeverance:
I need to work on this code business, I will look at your examples and see if I can gleam inspiration. I mean I don't need it blisteringly quick but I need it accurate within 1 second really.

no problem if you'r talking about pure transmission-time

I think my main issue is getting everything to work together, none of the things I want it to do are particularly complex, its just that I have many things going on at once and I am having trouble tying them together!

You should consider ... a helicopter-view with an intent to stick to broad lines and clarity. By that I mean, that you should consider to segment your code into easily understandable units. That could be by collecting some tasks under a common class. Imo complexity is a problem on it's own.

Are there any examples specifically applicable to performing a sequence of events (which is feedback dependent) while having an option to pause and / or interrupt the sequence?

Are you talking pc-application? If so, I can suggest that you think circular: Make a loop that contains the tasks and decide exit-points. Control it by boolean variables: In the event-handler you flip a boolean, and it opens/closes the task that is in the loop (behind the bool).
When you use a loop in vb you might utilize a function application.doEvents() once for every loop, to listen for changes. If you look at the blinking-leds or button- examples (arduino), you may see how to build a 'state' on both sides of an event (a button-click changes from state-on to state-off) ... etc. Then, check states in each loop and act upon it.
And, do remember to name your variables right .. that makes understanding that much easier.

... so much from an amateur

Thank you guys for your most useful advice.

Last night I did manage to get somewhere by using booleans, do() and while(). It certainly was an issue with the awful delay() function. I am not quite sure of the purpose of delay() in the real world but there we go. I still have a bit of an issue that while one sequence is running the displayed values (I have the value of the an analog pin continually updated on the GUI) freeze until the loop is completed. I am thinking that I might possibly be able to fix this by using separate threads?

Also the question that mist have been asked a million times already - how might I make a step wait for a set time without using delay()? I mean for the most part there will be a stimulus for the next step, which I seem to be able to do now... however some are purely time dependent.

:slight_smile:

P.S. I shall try and post some example code a bit later on when I am not at work.

I still have a bit of an issue that while one sequence is running the displayed values (I have the value of the an analog pin continually updated on the GUI) freeze until the loop is completed. I am thinking that I might possibly be able to fix this by using separate threads?

Your still flapping your arms and not communicating. Let's see some code.

how might I make a step wait for a set time without using delay()? I mean for the most part there will be a stimulus for the next step, which I seem to be able to do now... however some are purely time dependent.

Time dependent as in "after this step wait x milli/micro/seconds then step again"?

The delay() function is one (terrible) way to make that happen.

Recording when the first step occurred, and, on each pass through loop, determining if it is time to step again, and, if so, step (and then clear the last step time) is another way.

The third, since stepping is controlled by the PC, is to not have the Arduino know anything about time dependent stepping. The PC should, and should tell the stepper to step whenever a step is required, regardless of what the "stimulus" is.

PaulS:
Your still flapping your arms and not communicating. Let’s see some code.

Haha, sorry :blush:

Ok… Please apologise for any hideously formatted parts of my code, its my first try.

This is the main part of the sketch, i.e. the first tab in my program:

// Need G4P library
import guicomponents.*;
import processing.serial.*;
import cc.arduino.*;

// analog variables
int[] vals;
int val;

// My pin assignments
Arduino arduino;
int ledPin = 12;
int pin=0;
int switchPin=8;


void setup(){
  size(480, 320);
  // text analog
  vals = new int[width];
  PFont font;
  font = loadFont("sansserif-24.vlw");
  textFont(font);
  // text analog finished
  
  // For the GUI builder 
  createGUI();
  customGUI();
  
  //Setup my serial to the arduino running firmata
  arduino = new Arduino(this, "COM7", 57600);
  arduino.pinMode(ledPin, Arduino.OUTPUT);
  arduino.pinMode(switchPin, Arduino.INPUT);
}


// Display some analog pin values from the ADC
void draw(){
  background(200,220,200);
  
    // shift array left by one
  for(int i=1; i<width; i++) { 
    vals[i-1] = vals[i];  
  } 

  // add the new values to the end of the array 
  // read potentiometer (0..1024), divide by four (0..255)
  // to stay within canvas drawing limits
 String sch = "ON";
 
  val = arduino.analogRead(pin); 
  vals[width-1] = val/4;
  if(arduino.digitalRead(switchPin)==Arduino.LOW) {
    sch = "OFF";
  } else {
    sch = "ON";
  } 
  
  textAlign(RIGHT);
  text("Pin 0: "+val, 200, 30); 
  text(5.0*(vals[width-1]/255.0)+"V",200,60);
  text(sch ,200,90);
  
}

// Use this method to add additional statements
// to customise the GUI controls
void customGUI(){

}

And this is the second tab which was generated by the GUI builder and I am trying to input my sequences into:

/* =========================================================
 * ====                   WARNING                        ===
 * =========================================================
 * The code in this tab has been generated from the GUI form
 * designer and care should be taken when editing this file.
 * Only add/edit code inside the event handlers i.e. only
 * use lines between the matching comment tags. e.g.

 void myBtnEvents(GButton button) { //_CODE_:button1:12356:
     // It is safe to enter your event code here  
 } //_CODE_:button1:12356:
 
 * Do not rename this tab!
 * =========================================================
 */

void button1_Click1(GButton button) { //_CODE_:button1:488357:
 // MY SEQUENCE HERE:-
  while(arduino.analogRead(pin) > 10);
     println("pumping");
     arduino.digitalWrite(ledPin, Arduino.HIGH);
  while(arduino.analogRead(pin) < 1000);
     println("stop");
     arduino.digitalWrite(ledPin, Arduino.LOW);
  while(arduino.digitalRead(switchPin)==Arduino.HIGH);
     println("stop");
     arduino.digitalWrite(ledPin, Arduino.LOW);
} //_CODE_:button1:488357:

void textfield1_Enter1(GTextField textfield) { //_CODE_:textfield1:337688:
  println("textfield1 - GTextField event occured " + System.currentTimeMillis()%10000000 );
  println(Arduino.list());
  println(val);
} //_CODE_:textfield1:337688:

void button2_Click1(GButton button) { //_CODE_:button2:542520:
  println("button2 - GButton event occured " + System.currentTimeMillis()%10000000 );
} //_CODE_:button2:542520:



// Create all the GUI controls. 
// autogenerated do not edit
void createGUI(){
  G4P.setColorScheme(this, GCScheme.BLUE_SCHEME);
  G4P.messagesEnabled(false);
  button1 = new GButton(this, "Face text", 194, 69, 80, 30);
  button1.setTextAlign(GAlign.CENTER | GAlign.MIDDLE);
  button1.addEventHandler(this, "button1_Click1");
  textfield1 = new GTextField(this, "Some text", 198, 185, 80, 20, false);
  textfield1.addEventHandler(this, "textfield1_Enter1");
  label1 = new GLabel(this, "My label", 291, 184, 80, 20);
  button2 = new GButton(this, "Face text", 346, 56, 80, 30);
  button2.setTextAlign(GAlign.CENTER | GAlign.MIDDLE);
  button2.addEventHandler(this, "button2_Click1");
}

// Variable declarations 
// autogenerated do not edit
GButton button1; 
GTextField textfield1; 
GLabel label1; 
GButton button2;

When I press the button on the GUI the analog values fail to update until the sequence is complete. I also do not seem to be able to interrupt the sequence or start another (if I insert another sequence for the second button)

When I press the button on the GUI

The one that triggers the button1_Click1 callback?

the analog values fail to update until the sequence is complete.

What sequence? All that happens when the button1_Click1 callback is called is that a pin on the Arduino changes state.

I'm wondering if you are aware that draw() is not exactly like loop() on the Arduino. On the Arduino, loop() is called over and over again, as fast as possible. In Processing, draw() is called over and over again, but not as fast as possible. It is called so that the time from the start of one call to the time of the start of the next call is not less than some value, based on the desired frame rate. You can change how often draw() is called by increasing the frame rate.

PaulS:

When I press the button on the GUI

The one that triggers the button1_Click1 callback?

Yes.

PaulS:

the analog values fail to update until the sequence is complete.

What sequence? All that happens when the button1_Click1 callback is called is that a pin on the Arduino changes state.

No, because of the while() the changes of state happen as a result of changes on the analog pin, which have to happen in order, and it seems to work.
What fails to update is that on the main sketch (not the GUI bit) I have set it up to display the analog value as a voltage in realtime, and it is this which freezes once the button is pressed, until the sequence (whatever it is - I simplified it) called when the button is pressed is finished.

rendeverance,
Just stay away from delay(..)
Take a look at the button-debounce example for 'the standard way' to execute code after a set time.

as for the while, you may get somewhere by adding a
break;
statement in each while

What fails to update is that on the main sketch (not the GUI bit) I have set it up to display the analog value as a voltage in realtime, and it is this which freezes once the button is pressed, until the sequence (whatever it is - I simplified it) called when the button is pressed is finished.

Because that is what you have defined to have happen. The draw() method is not called again until the button callback ends.

You’ll need to treat the button callback like an ISR, and simply set a flag that indicates that a “sequence” should be executed, but don’t actually execute it.

In draw(), you will have to observe that the event needs to occur, and make it happen.

Keep in mind that the screen refresh does not actually occur until the end of the draw() method. So, you really will need to ditch those while methods, and implement a real state machine.

I think I am going to =( lol...

Thanks guys... This is where I am finding the tutorials useless as they use things like delay() and while() yet give you no help with what is actually good practice and will work for more then a simple single action program, and the threads example skips around so much I must have read it about 15 times and cannot implement it.

Being a research scientist by trade I did not expect this to be so hard to find appropriate reading and / or examples on, but clearly learning programming as an adult is not going to be easy. I have no idea what an ISR is, or how to implement a real state machine - back to google

Essentially I just wanted to eventually make was a program that has four buttons, and an option to load / edit / save configuration files.

  • When the program was loaded the user could pick one of three sequence options and this would simply send / receive commands as part of a sequence with the arduino.
  • The fourth button would be reserved as a stop button to provide an interrupt option.
  • No two sequences could run at the same time.
  • It could provide continuous on-screen values for certain pins on the arduino, possibly with a datalogging to a file.
  • If any values on the arduino were deemed out-of-bounds by the software (operating like an interlock), any sequences were halted and an error message was displayed.

I was under no illusion that to get ALL of this would be hard, but to get a button press sequence working independently of values being displayed on the GUI is already taking forever to learn and essentially, what I have done amounts to nothing as all of the basic tutorials which I studied and learnt, use methods which are not much use outside of the examples themselves, or at least it seems that way.