Having Arduino and Gobetwino wait for eachother to continue

So I have been working on a project that has a stepper motor and driver which will turn a certain amount of microsteps and then stop, wait for a button press and continue on until the maximum amount of button presses is reached. I am using an Arduino Uno R3, and the following driver/motor.
http://www.automationdirect.com/adc/Shopping/Catalog/Motion_Control/Stepper_Systems/Stepper_Drives_-z-_Power/STP-DRV-4035

http://www.automationdirect.com/adc/Shopping/Catalog/Motion_Control/Stepper_Systems/Stepper_Motors_-z-_Cables/STP-MTR-17040

Now I have a functioning code and want to replace the button press with my actual intended function. I want the stepper to move and then send a signal to Gobetwino to hit the “Enter” button. This will take a light measurement in an excel sheet which controls a Cl 200a light meter. I want Arduino to wait until the measurement is finished and have Gobetwino tell Arduino to continue. This should be repeated until the maximum amount of measurements has been reached.

I have looked through the Gobetwino manual and looked around online, but I have not found whether or not this is something that could be done or whether there may be some other alternative?
Any ideas would be greatly appreciated!

#include <AccelStepper.h>,

int x = 10; // x represents the amount of measurments taken on the X axis
int D = 5; // number of degrees between measurments


int StepSize = 27.78 * D; //Accounts for microsteps


AccelStepper stepper(1, 3, 2); // Define a stepper and the pins it will use. 1 means two wires. 3 is the step pin, 2 is Dirpin
int fullRev = 10000;
int quarterRev = 2500;

int inPin = 7;         // Button
int outPin = 3;        // Step Pin
int DirPin = 2;        // Direction Pin

int state = 1;         // the current state of the output pin
int reading;           // the current reading from the input pin

int ButtonPressCount = 0; //start value of how many times button has been pressed.

int LED = 13; //Pin 13 will be our LED.

void setup()
{
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT); //setting up inputs and outputs
  pinMode(LED, OUTPUT);
  
   stepper.setMaxSpeed(1000); 
   stepper.setAcceleration(1500); //acceleration at 1500 steps per second
}

void loop()
{
  
 if (state == 1)                            //State1
{
    
 digitalWrite(LED,HIGH);
     
 reading = digitalRead(inPin); //Read pin
    
    if (reading == HIGH) 
    { 
      
        state = 2;                           //State 2
    }
         
}  
    if (state == 2 && ButtonPressCount < x)
 
      {
       digitalWrite(LED,LOW);
       stepper.move(StepSize); 
       ButtonPressCount++;
       state = 3;                            //State 3
      }
      if (state == 3)
      {
         if (stepper.distanceToGo() == 0)
          {
           state = 1;                        //State 1
          }
 
     if (state == 2 && ButtonPressCount >=x)
       {
       stepper.stop();
       //state = 4;                          //State 4
       }
      }

   stepper.run();
}

chellydth:
I want Arduino to wait until the measurement is finished

What options are available to you to tell when the measurement is finished?

Basically the light meter connects to the computer through a usb cable. There is a specific excel template that you pull up and once you select which meter you are using a pop up window appears and you can click the measure button.

I know Gobetwino can tell once a program finishes and send that back to Arduino so I am thinking maybe there is a way to do this with just the measure screen?

I also know Arduino can communicate through the serial port, so I am trying to figure out if it can read from other ports or only the one it is connected to ?

Is the length of time taken per measurement reasonably consistent and predictable? I'm wondering whether you could just take measurements on a timed basis. If so, that would make things a lot simpler.

Otherwise, does the result of the measurement appear in the spreadsheet, and is it important to you that it is available there? If so your best option would be to modify the template to include a VB fragment that wrote to the Arduino COM port when each measurement was complete, but if you don't need to stick to the Excel approach it might be possible to find out how the spreadsheet communicates with the sensor (I guess it's probably using a virtual serial port) and write your own client that can get / save the data as required and control the Arduino too.

Thanks for the replies!

I tested the measurements and it always takes 1.1 seconds so I wrote a delay into the code. My problem seems to be now that when gobetwino hits enter it only selects the main sheet and does not control the dialog box with the measure button. This seems weird as I can control the dialog box with my keyboard . I have tried a few things with macros, but I am not having much success.

This is my code with gobetwino commands which is timed well only hitting enter will not cause a new measurement.

#include <AccelStepper.h>,

int x = 5; // x represents the amount of measurments taken on the X axis
int D = 5; // number of degrees between measurments


int StepSize = 27.78 * D; //Accounts for microsteps


AccelStepper stepper(1, 3, 2); // Define a stepper and the pins it will use. 1 means two wires. 3 is the step pin, 2 is Dirpin

int outPin = 3;        // Step Pin
int DirPin = 2;        // Direction Pin

int state = 1;         // the current state of the output pin
int reading;           // the current reading from the input pin LED

int ButtonPressCount = 0; //start value of how many times button has been pressed.

int LED = 13; //Pin 13 will be our LED.

void setup()
{  Serial.begin(9600);
   delay(2000);
   Serial.println("#S|OPENTEMP|[]#");//when gobetwino is opened it  will open the excel lightmeter template
   delay(4500);//wait 
   Serial.println("#S|OPENADDON|[]#");//opens add on for light meter
   delay(2500);
   Serial.println("#S|SENDK|[0&(%)]#");// Press alt
   delay(1000);
    Serial.println("#S|SENDK|[0&x]#");
   delay(1000);
   Serial.println("#S|SENDK|[0&y]#");
   delay(1000);
   Serial.println("#S|SENDK|[0&{DOWN}~]#");// Opens Measurement dialog box
   delay(12000); 
   
  pinMode(outPin, OUTPUT); //setting up inputs and outputs
  pinMode(LED, OUTPUT);
  
  stepper.setMaxSpeed(1000); 
  stepper.setAcceleration(1500); //acceleration at 1500 steps per second
  

}

void loop()
{
 
 if (state == 1)                            //State1
{
    
 digitalWrite(LED,HIGH); //LED on
 
 reading = digitalRead(LED); //Read pin
    
    if (reading == HIGH) 
    { 
      
        state = 2;                           //State 2
    }
         
}  
    if (state == 2 && ButtonPressCount < x)
 
      {
       delay(1000);
       Serial.println("#S|SENDK|[0&{ENTER}]#");// Press enter
       delay(2500); // wait for measurement 
       stepper.move(StepSize); 
       ButtonPressCount++;
       state = 3;                             //State 3
       digitalWrite(LED,LOW); 
      }
      if (state == 3)
      {
         if (stepper.distanceToGo() == 0)
          {
           state = 1;                        //State 1
          }
 
     if (state == 2 && ButtonPressCount >=x)
       {
       stepper.stop();
       
       //state = 4;                          //State 4
       }
      }

   stepper.run();
}

What sequence of key strokes is the sketch intended to send?

What is intended to happen to your spreadsheet application when you send this sequence?

What is actually happening?

It presses alt (to open keyboard shortcuts for excel) X and Y to choose the correct add-ins drop down menu. Then Down and space to choose the measure option from the menu. This brings up the pop up with the measure button. All of the first steps work correctly, then when enter is being pressed in state 2 "Serial.println("#S|SENDK|[0&{ENTER}]#");// Press enter" instead of taking a measurement like when I physically hit the enter button, the cursor in the excel sheet moves down one row for each press. It is as if gobetwino cannot interact with the pop up screen even if I can control it with physical keyboard presses.

As a test, could you try sending the sequence up to the {enter} key, and then send the {enter} key separately? For example you could modify the sketch so that it sends the first part on the first button press, and the second part on the second press.

I'm wondering whether either the {enter} is arriving too soon, before the dialog has opened and taken the keyboard focus, or that the key press you're sending is not what the dialog requires (an enter versus return issue, or something like that).

So I tried some different variations of the code. A few of them involved very long delays so that I could actually click on the pop up to make sure it was selected before enter was hit. I also had a delay and pressed enter on my keyboard and it took a measurement. After waiting for the delay to finish enter was sent by the gobetwino and again the cursor just moved down a row in the worksheet, even though the box was selected and I had just taken a measurement with the keyboard.

I am guessing at this point that I need to find another way to accomplish this. Any ideas are always appreciated! Also thanks for the input thus far.

The evidence you describe suggests that Gobetwino is sending the keyboard events to the main window and not to the dialog which has the focus. Does the dialog respond to any other keyboard events - tab, for example? If so, you could test this theory by sending a tab and seeing whether the dialog responds to it.

If that's what's happening there are a few approaches you could try. The Arduino is capable of emulating keyboard / mouse devices directly. How easy this is to do will depend which Arduino you're using, but for a UNO I think it would involve reprogramming the Atmega 16U2 chip that implements the USB controller. With this approach the input from the Arduino would behave exactly as if you had hit the corresponding key on a physical keyboard. This feels like overkill to me, but it's definitely feasible.

You can work out how the spreadsheet works and find a way to achieve the same thing without using the dialog. (This is the approach I'd take.)

You can write your own program to generate the key sequence, and do it in a way that sends the events to the dialog. I don't have your spreadsheet here to test, but for example here's a jscript implementation that sends the same key sequence (as far as I have understood your description). You could run this manually to see whether it works.

<?xml version="1.0" ?>
<job id="SendKeys">
<script language="JScript">
<![CDATA[

    var WshShell = new ActiveXObject("WScript.Shell");

    WScript.Sleep(1000); // brief delay to give you time to give keyboard focus to the target window
    
    // alt+x,y,down,space, enter
    WshShell.sendKeys("%x");
    WshShell.sendKeys("y");
    WshShell.sendKeys("{DOWN}");
    WshShell.sendKeys(" ");
    WshShell.sendKeys("{ENTER}");

]]>
</script>
</job>

Okay I am going to start looking into Jscript and see how that goes. Thanks!