Analog Data (Control variable)

This example explains how to use the ANALOG DATA Control Panel to send analog settings, controls or setpoints to your Arduino.

To illustrate this, we will send an analog value to your Arduino. Then, according to this control value, we will switch on or off the on-board LED based on this simple rule : if the value is bigger than a pre-defined threshold value, the on-board LED is switched on. If the value is smaller than the threshold value, the LED is turned off.

Refer to this section to learn more about HITI Data.

 

1) Sketch: 1_Basics \ 8_AnalogData

We start our program by declaring pin 13 as an OUTPUT to control on the on-board LED. We also set the threshold value to 1500.

#include <HITIComm.h>


// Pins assignment
const int pin_LED = LED_BUILTIN;

// Analog Data assignment:
const int ad_control   = 0;
const int ad_threshold = 1;

// Threshold value
int threshold = 1500;


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

    // set pins mode
    pinMode(pin_LED, OUTPUT);
}


Then, inside the loop(), we read the control value (set using the ANALOG DATA Control Panel), and we compare it to the threshold value. If it is bigger than this threshold value, the LED is turn on. Else, it is turned off.

Finally, we write the threshold value to Analog Data 1 for watching it inside the ANALOG DATA Control Panel.

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

    // read control value from HITIPanel
    int control = (int) HC_analogDataRead(ad_control);

    // if the received value is bigger than the threshold value
    if (control >= threshold)
    {
        digitalWrite(pin_LED, HIGH); // turn ON the LED
    }
    else
    {
        digitalWrite(pin_LED, LOW);  // turn OFF the LED
    }

    // send threshold value to HITIPanel
    HC_analogDataWrite(ad_threshold, threshold);
}

 

2) Control Panels

  1. Display the ANALOG DATA Control Panel (“DATA” button).
  2. Enter a control value smaller than the threshold value, for instance 1000. The on-board LED should remain turned off. Now enter a value that is bigger than the threshold value, try 3000. The LED should turn on.
    image177.png

  3. Display the DIGITAL I/O Control Panel (“IO” button).
  4. Check the state of the on-board LED.
    image178.png

 

3) Exercise

Try to improve the code to allow setting of the threshold value from the ANALOG DATA Control Panel.

 

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