vala,
Не совсем понятен фрагмент кода.
Не похож на исходной из 
43 поста. Он работает верно и с отрицательными показаниями. Выводит тоже верно при нажатии на кнопку.
Вот тот, что человек сделал, только заменил value на finalresult и попытался сделать боле похожим на исходник, что бы не путаться при выводе и тем, что в формулах, но увы... Всё тот же косяк...
Код: Выделить всё
#include <Keyboard.h>
#include "GyverButton.h"
int bit_array[25];        // For storing the data bit. bit_array[0] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB).
const int CLOCK_PIN = 4;    // штангенциркуль
const int DATA_PIN = 5;     // штангенциркуль 
const int BTN_PIN = 9;      // кнопка ввод/режимы
const int LED_SELECT_PIN = 6; // светодиод режима "значение + выделить"
const int LED_INPUT_PIN = 7;  // светодиод режима "ввод значения"
const int LED_TAB_PIN = 8;    // светодиод режима "значение + табулятор"
const int BEEPER_PIN = 3;   // пищалка
unsigned long time_now;   // For storing the time when the clock signal is changed from HIGH to LOW (falling edge trigger of data output).
float finalresult = 0.0;      
byte state = 0;     // состояние формата вывода: перенос, маркировать, табуляция
byte zeros = 2;
GButton butt1(BTN_PIN);
void setup() {
  
  pinMode(CLOCK_PIN, INPUT);
  pinMode(DATA_PIN, INPUT);
  pinMode(LED_SELECT_PIN, OUTPUT);
  pinMode(LED_INPUT_PIN, OUTPUT);
  pinMode(LED_TAB_PIN, OUTPUT);
  pinMode(BEEPER_PIN, OUTPUT);
  //Serial.begin(9600);
  butt1.setDebounce(1);        // настройка антидребезга (по умолчанию 80 мс)
  butt1.setTimeout(1000);        // настройка таймаута долгий клик 
  butt1.setClickTimeout(1);   // настройка таймаута быстрый клик 
  set_mode();
  
}
// #### основной код ####
void loop() {
  butt1.tick();  // обязательная функция отработки. Должна постоянно опрашиваться
  
  while (digitalRead(CLOCK_PIN) == LOW);   // If clock is LOW wait until it turns to HIGH
  time_now = micros();
  while (digitalRead(CLOCK_PIN) == HIGH);   // Wait for the end of the HIGH pulse
  if ((micros() - time_now) > 500)  // If the HIGH pulse was longer than 500 micros we are at the start of a new bit sequence
  {
   decode(); //decode the bit sequence
  }
  if (butt1.isClick()) 
  {
    tone(BEEPER_PIN, 1000, 100);
    send_value();
  }     
  if (butt1.isHolded())
  {
    if (state == 2)
    {
      state = 0;
    }
    else
    {
      state++;
    }
    tone(BEEPER_PIN, 500, 100);
    set_mode();
  }
}
// ##### вспомогательные функции #### //
float decode(void) {
//void decode() {
  int bit_array[25];        // For storing the data bit. bit_array[0] = data bit 1 (LSB), bit_array[23] = data bit 24 (MSB).
  byte sign = 1;
  byte i = 0;
  float value = 0.0;
  float result = 0.0;
  bit_array[i] = digitalRead(DATA_PIN);       // Store the 1st bit (start bit) which is always 1.
  while (digitalRead(CLOCK_PIN) == HIGH);
  for (i = 1; i <= 24; i++) {
    while (digitalRead(CLOCK_PIN) == LOW); // Wait until clock returns to HIGH
    bit_array[i] = digitalRead(DATA_PIN);
    while (digitalRead(CLOCK_PIN) == HIGH); // Wait until clock returns to LOW
  }
  for (i = 0; i <= 24; i++) {                 // Show the content of the bit array. This is for verification only.
    //Serial.print(bit_array[i]);
    //Serial.print(" ");
  }
  for (i = 1; i <= 20; i++)// Turning the value in the bit array from binary to decimal.
  {                 
    value = value + (pow(2, i-1) * bit_array[i]);
  }
  if (bit_array[21] == 1) sign = -1;          // Bit 21 is the sign bit. 0 -> +, 1 => -
  if (bit_array[24] == 1)                    // Bit 24 tells the measureing unit (1 -> in, 0 -> mm)
  { 
    result = (value*sign) / 2000.00;
    zeros = 4;
    //Serial.print(result, 3);                   // Print result with 3 decimals
    //Serial.println(" in");
  }
  else 
  {
    result = (value*sign) / 100.00;
    zeros = 2;
    //Serial.print(result, 2);                   // Print result with 2 decimals
    //Serial.println(" mm");
  }
  finalresult = result;
  //return result;
}
void set_mode()
{
  switch (state)
  {
  case 0: set_select();
    break;
  case 1: set_input();
    break;
  case 2: set_tab();
    break;
  }
}
void send_value()
{
  switch (state)
  {
  case 0: send_select();
    break;
  case 1: send_input();
    break;
  case 2: send_tab();
    break;
  }
}
// ### отправка данных в клавиатуру ###
void send_select()
{
  //Serial.print("send mode 0 - value and select ctrl+A "); // 
  //Serial.println(value);
  Keyboard.print(finalresult, zeros);  // send a 'result' to the computer via Keyboard HID
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press('A');    
  Keyboard.releaseAll();
}
void send_input()
{
  //Serial.print("send mode 1 - value and input "); // 
  //Serial.println(value);
  Keyboard.print(finalresult, zeros);  // send a 'result' to the computer via Keyboard HID
  Keyboard.press(KEY_RETURN);    
  Keyboard.releaseAll(); 
}
void send_tab()
{
  //Serial.print("send mode 2 - value and tabulator "); // 
  //Serial.println(value);
  Keyboard.print(finalresult, zeros);  // send a 'result' to the computer via Keyboard HID
  Keyboard.press(KEY_TAB);    
  Keyboard.releaseAll();
}
// ### установка светодиодов режима ###
void set_select(void)
{
  //Serial.println("set mode 0 - value and select ctrl+A"); // 
  digitalWrite(LED_SELECT_PIN, HIGH);
  digitalWrite(LED_INPUT_PIN, LOW);
  digitalWrite(LED_TAB_PIN, LOW);
}
void set_input(void)
{
  //Serial.println("set mode 1 - value and input"); // 
  digitalWrite(LED_SELECT_PIN, LOW);
  digitalWrite(LED_INPUT_PIN, HIGH);
  digitalWrite(LED_TAB_PIN, LOW);
}
void set_tab(void)
{
  //Serial.println("set mode 2 - value and tabulator "); // 
  digitalWrite(LED_SELECT_PIN, LOW);
  digitalWrite(LED_INPUT_PIN, LOW);
  digitalWrite(LED_TAB_PIN, HIGH);
}
В коде меню работает, загораются светодиоды по выбранной команде, но отрицательные значения не верно отображает. (пищалку не подключал)
-3.02мм отображает, как 770.10
-0.1190 " .................... 30.3450
Мне уже кажется проще будет использовать библиотеку OneButton.
Там можно настроить одно нажатие, двойное нажатие и удержание.
Как раз три функции, которые мне нужны...