1

我的 LED 环上有 4 个区域,我制作了 4 个按钮来选择它们。我希望能够将这些区域用于不同的组合。

所以我的目标是:

单击按钮:选定区域处于活动状态。

双击按钮:选定区域处于非活动状态。

我知道有一个名为 mousePressed() 的函数,但我无法通过双击条件将它实现到我的按钮上。

这是我的处理代码:

  Group AreaGroup = cp5.addGroup("AREAS")
    .setPosition(290,50)
    .setWidth(150)
    .setHeight(30)
    .setFont(font2)
    .moveTo(SetupGroup);
    background(0);
    noStroke();
    ;
    
    cp5.addButton("AREA_1")  // The button
    .setPosition(-15,10)     // x and y relative to the group
    .setSize(90, 50)       // (width, height)
    .setFont(font)
    .moveTo(AreaGroup);   // add it to the group 
    ;     
  
  
    cp5.addButton("AREA_2")  // The button
    .setPosition(90,10)    // x and y relative to the group
    .setSize(90, 50)       // (width, height)
    .setFont(font) 
    .moveTo(AreaGroup);   // add it to the group  
  ; 
  
    cp5.addButton("AREA_3")  // The button
    .setPosition(-15,80)     // x and y relative to the group
    .setSize(90, 50)       // (width, height)
    .setFont(font)
    .moveTo(AreaGroup);   // add it to the group 
    ;     
  
  
    cp5.addButton("AREA_4")  // The button
    .setPosition(90,80)    // x and y relative to the group
    .setSize(90, 50)       // (width, height)
    .setFont(font) 
    .moveTo(AreaGroup);   // add it to the group  
  ;
  
     cp5.addButton("ALL")  // The button
    .setPosition(190,45)    // x and y relative to the group
    .setSize(90, 50)       // (width, height)
    .setFont(font) 
    .moveTo(AreaGroup);   // add it to the group  
  ; 

void AREA_1(){
  println("AREA_1");
  if (port != null) port.write("a\n");
}

void AREA_2(){
  println("AREA_2");
  if (port != null) port.write("b\n");
}

void AREA_3(){
  println("AREA_3");
  if (port != null) port.write("c\n");
}

void AREA_4(){
  println("AREA_4");
  if (port != null) port.write("d\n");
}

void ALL(){
  println("ALL");
  if (port != null) port.write("t\n");
}

这是来自 Arduino 的代码片段

switch(state){

        case 'p':
        Serial.println(F("ex."));
        break;

        case 's':
        Serial.println(F("Start"));
          start = true;
        break;

        case '1':
        Serial.println(F("Switching to ring #1"));
          updateRing1 = true;
          updateRing2 = false;
        break;
        
        case '2':
          Serial.println(F("Switching to ring #2"));
          updateRing1 = false;
          updateRing2 = true;
        break;
        
        case 'a':
          Serial.println(F("Area1"));
          Area1 = true;
        break;  

        case 'b':
          Serial.println(F("Area2"));
          Area2 = true;
        break;

        case 'c':
          Serial.println(F("Area3"));
          Area3 = true;
        break;

        case 'd':
          Serial.println(F("Area4"));
          Area4 = true;
        break;

         case 't':
          Serial.println(F("All the leds are choosen"));
          total = true;
          Area1 = false;
          Area2 = false;
          Area3 = false;
          Area4 = false;
        break; 

在此处输入图像描述

4

1 回答 1

1

如果您打算检测双击,millis()请在第一次单击时启动计时器,然后在millis()第二次单击后读取第二次读数并将其与时间阈值进行比较以检测它。您将需要几个定时变量,一个用于当前值,另一个用于存储前一个值。

您对该功能进行了哪些尝试mousePressed()?很高兴知道您尝试了什么以及失败的地方以帮助您

于 2021-08-18T19:11:21.923 回答