Digital Data (Virtual Switch)

HITI Data allows for easy data exchange between your Arduino and HITIPanel. A Digital Data holds a boolean value. Use it if you need to:

  • Monitor a boolean value in your program (Boolean Watcher)
  • Control a boolean value in your program (Virtual Switch) =>  usually to trigger actions

This example explains how to use Digital Data 0 as a virtual switch to control a servo on pin 8. According to the virtual switch state, the servo will alternate between 2 pre-defined positions.

Also, a virtual switch can be controlled with your computer keyboard. In this example, we will control the virtual switch with the “Z” key.

 

1) Wiring diagram

Connect a Servo on pin 8 and add an electrolytic capacitor in parallel with the Servo power supply (beware of the polarity!!!). Connect it as close as possible to the Servo.
Arduino-computer-control-software-customizable-interface-panel-digital-data-virtual-switch-1.png

 

2) Sketch: 1_Basics \ 5_DigitalData

We start by attaching a Servo variable to pin 8 and we move the Servo to an initial position of 12.8° The value must be given in millidegrees.

#include <HITIComm.h>


// Pins assignment
const int pin_Servo = 8;

// Digital Data assignment
const int dd_VirtualSwitch = 0;


// run once at power on
void setup()
{
  // initialize library
  HC_begin();

  // attach Servo to the pin. Move servo to position 12.8°.
  HC_attachServo(pin_Servo, 12800);
}


Inside the loop(), we read the state of the Virtual Switch using HC_digitalDataRead(data).  If you activate the Switch (using the DIGITAL DATA Control Panel), the servo will move to position 169.3°. If you deactivate the Switch, the servo will go back to position 12.8°.

// run repeatedly after setup()
void loop()
{
  // communicate with HITIPanel
  HC_communicate();

  // if the virtual switch is activated (using HITIPanel)
  if (HC_digitalDataRead(dd_VirtualSwitch) == HIGH)
  {
    // move Servo to position 169.3°
    HC_servoWrite(pin_Servo, 169300);
  }
  else
  {
    // move Servo to position 12.8°
    HC_servoWrite(pin_Servo, 12800);
  }
}

 

3) Control Panels

  1. Display the DIGITAL I/O Control Panel (“IO” button).
  2. The servo is at its initial position (12.8°).
    image129.png

  3. Display the DIGITAL DATA Control Panel (“DATA” button).
  4. The state of the Virtual Switch is displayed in the Command Panel of Digital Data 0. Activate the Virtual Switch using its button. This will move the servo to position 169.3°.
    image130.png

  5. Go back to the DIGITAL I/O Control Panel and check that the servo is now at position 169.3°.
    image131.png

 

4) Keyboard Control

  1. Open the Keyboard Dialog box (CTRL+K or Tools\Keyboard).
    Arduino-computer-control-software-customizable-interface-panel-digital-data-virtual-switch-5.png

  2. As you can see, the “Z” key of your keyboard is connected to Digital Data 0. It means that pressing/releasing “Z” will activate/deactivate the Virtual Switch. Also, you can see that the keyboard control is enabled.
  3. So, let’s try this. Click “OK” to close the Keyboard Dialog box and go back to the DIGITAL DATA Control Panel. Then press/release “Z” several times. This should activate/deactivate the Virtual Switch and you should see the servo moving each time you press/release Z.

 

Was this article helpful?
0 out of 0 found this helpful