Digital Data (Button)

Modified on Wed, 4 Jan, 2023 at 2:14 PM

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


The difference with a Virtual Switch is that a Virtual Button is immediately deactivated by the Arduino program after reading its state, to simulate a click.


To illustrate this, let’s use Command Panel 0 as a Button to move a servo on pin 8. Each time the button is clicked, the servo moves 10°, and move back to 0° if its position exceeds 170°.



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 \ 6_DigitalData


We start by attaching a Servo variable to pin 8 and we move the Servo to an initial position of 0°.


#include <HITIComm.h>

// Pins assignment
const int pin_Servo = 8;

// Digital Data assignment
const int dd_VirtualButton = 0;

// Servo position
long pos = 0;

void setup()
{
  // initialize library
  HC_begin();

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


We then create a function to move the servo 10°, and to move it back to 0° if its position exceeds 170°. The position value is in millidegrees.


// move servo 10°. Move back to 0° if position exceeds 170°.
void moveServo()
{
  // increment position by 10°
  pos = pos + 10000;

  // reset position if it exceeds 170°
  if(pos > 170000)
    pos = 0;

  // move servo
  HC_servoWrite(pin_Servo, 0);
}

 

Inside the loop(), we read the state of the Button using HC_digitalDataRead(data). It the Button is activated, we immediately deactivate it, to simulate a button click, and we move the servo.


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

  // if the Virtual Button is activated (from Command Panel 0)
  if (HC_digitalDataRead(dd_VirtualButton) == HIGH)
  {
    // immediately deactivate Button
    HC_digitalDataWrite(dd_VirtualButton, LOW);

    moveServo();
  }
}

 


Control Panels

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

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



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

4) Click on the BUTTON several times. This will move the servo 10° each time.



5) Go back to the I/O Control Panels and check the servo position.








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