Digital Data (Switch)

Modified on Wed, 4 Jan, 2023 at 10:09 PM

This example shows how to use a DIGITAL DATA Command Panel as a Virtual Switch to control a boolean value inside your running Arduino program. A Switch is typically used when you need to trigger actions in your Arduino project (e.g. switching ON a pump).


To illustrate this, let’s use Command Panel 0 as a Switch to control a servo on pin 8. According to the switch state, the servo will alternate between 2 pre-defined positions.


A Digital Data can also be controlled with your computer keyboard. Key assignment is done using the Keyboard Panel. In this example, we control the Virtual Switch with the “Z” key



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.


 



Sketch

Upload this 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;

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 Switch using HC_digitalDataRead(data).  When you activate the Switch (using Command Panel 0), the servo moves to position 169.3°. When you deactivate the Switch, the servo goes back to position 12.8°.


void loop()
{
  // communicate with HITIPanel
  HC_communicate();

  // if the Virtual Switch is activated (from Command Panel 0)
  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);
  }
}

 

 

Control Panels

1) Display the I/O Control Panels (“IO” button). 

2) The servo is at its initial position (12.8°).



3) Display the DATA Control Panels (“DATA” button). 

4) The state of the VIRTUAL SWITCHis displayed in Command Panel 0. Activate the Switch using its button. This will move the servo to position 169.3°.



5) Go back to the I/O Control Panels and check that the servo is now at position 169.3°.


 

 

Keyboard Control

1) Open the Keyboard Dialog box (CTRL+K or Tools\Keyboard).



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 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 DATA Control Panels. Then press/release “Z” several times. This should activate/deactivate the Switch and you should see the servo moving each time you press/release Z.






Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article