实现一个处理控制的Arduino上/下计数器

时间:2016-10-26 06:29:11

标签: arduino processing

我正在制作一个Arduino Up / Down计数器,其输出可由处理程序控制。它在训练护罩的3位7段显示屏上显示倒计时,按钮可用于使其向上,向下,停止和重置计数。处理代码应该能够做同样的事情,但是通过软件而不是按钮。我的实现思路是让盒子一旦盘旋,发出切换计数器上下,停止和重置的信号,以便我可以使用SerialWrite示例。下面显示的是我的向上/向下计数器Arduino代码,包括为了将其与Processing接口所必需的串行连接。

unsigned char digit_1 = 7;
unsigned char digit_2 = 8;
unsigned char digit_3 = 10;

int num1 = 0;
int num2 = 0;
int num3 = 0;
int state = 0;
int halt = 0;
char val; //data received from serial port
void setup()
{
  Serial.begin(9600);
  for (int x = 11; x < 19; x++)
  {
    pinMode (x, OUTPUT);
  }

  pinMode (digit_1, OUTPUT);
  pinMode (digit_2, OUTPUT);
  pinMode (digit_3, OUTPUT);
 //  attachInterrupt(0, s1_press, RISING);
 //  attachInterrupt(1, s2_press, RISING);

}


void loop()
{
  while (Serial.available())
  { val = Serial.read();
  }
  if (val == 1)
  {
    state = 1;
  }
  else //if (val == 0)
  {
    state = 0;
    halt = 0;
  }
  if (val == 2)
  {
    halt = 1;
  }
  else
  {
    state = 0;
    halt = 0;
  }

  if (halt == 0) {
    if (state == 0) {
      num3++;
      if (num3 == 10) {
        num3 = 0;
        num2++;
      }
      if (num2 == 10) {
        num2 = 0;
        num1++;
      }
      if (num1 == 10) {
        num1 = 0;
        num2 = 0;
        num3 = 0;
      }
    }

    if (state == 1) {
      num3--;
      if (num3 == -1) {
        num3 = 9;
        num2--;
      }
      if (num2 == -1) {
        num2 = 9;
        num1--;
      }
      if (num1 == -1) {
        num3 = 9;
        num2 = 9;
        num1 = 9;
      }
    }
  }
  for (int x = 0; x < 80; x++) {
    digitalWrite (digit_3, HIGH);
    digitalWrite (digit_2, LOW);
    digitalWrite (digit_1, LOW);
   display_ (num1);
    delay(1);
    digitalWrite (digit_3, LOW);
    digitalWrite (digit_2, HIGH);
    digitalWrite (digit_1, LOW);
    display_ (num2);
    delay(1);
    digitalWrite (digit_3, LOW);
    digitalWrite (digit_2, LOW);
    digitalWrite (digit_1, HIGH);
    display_ (num3);
    delay(1);
  }

}




void display_ (unsigned char num)
{
  switch (num)
  {
    case 0:
      {
        digitalWrite (11, HIGH);
        digitalWrite (12, HIGH);
        digitalWrite (13, HIGH);
        digitalWrite (14, HIGH);
        digitalWrite (15, HIGH);
        digitalWrite (16, HIGH);
        digitalWrite (17, LOW);
        digitalWrite (18, LOW);

        break;
      }
    case 1:
      {
        digitalWrite (11, LOW);
        digitalWrite (12, HIGH);
        digitalWrite (13, HIGH);
        digitalWrite (14, LOW);
        digitalWrite (15, LOW);
        digitalWrite (16, LOW);
        digitalWrite (17, LOW);
        digitalWrite (18, LOW);

        break;
      }
    case 2:
      {
        digitalWrite (11, HIGH);
        digitalWrite (12, HIGH);
        digitalWrite (13, LOW);
        digitalWrite (14, HIGH);
        digitalWrite (15, HIGH);
        digitalWrite (16, LOW);
        digitalWrite (17, HIGH);
        digitalWrite (18, LOW);
         break;
      }
    case 3:
      {
        digitalWrite (11, HIGH);
        digitalWrite (12, HIGH);
        digitalWrite (13, HIGH);
        digitalWrite (14, HIGH);
        digitalWrite (15, LOW);
        digitalWrite (16, LOW);
        digitalWrite (17, HIGH);
        digitalWrite (18, LOW);
       break;
      }
    case 4:
      {
        digitalWrite (11, LOW);
        digitalWrite (12, HIGH);
        digitalWrite (13, HIGH);
        digitalWrite (14, LOW);
        digitalWrite (15, LOW);
        digitalWrite (16, HIGH);
        digitalWrite (17, HIGH);
        digitalWrite (18, LOW);

        break;
      }
    case 5:
      {
        digitalWrite (11, HIGH);
        digitalWrite (12, LOW);
        digitalWrite (13, HIGH);
        digitalWrite (14, HIGH);
        digitalWrite (15, LOW);
        digitalWrite (16, HIGH);
        digitalWrite (17, HIGH);
        digitalWrite (18, LOW);

        break;
      }
    case 6:
      {
        digitalWrite (11, HIGH);
        digitalWrite (12, LOW);
        digitalWrite (13, HIGH);
        digitalWrite (14, HIGH);
        digitalWrite (15, HIGH);
        digitalWrite (16, HIGH);
        digitalWrite (17, HIGH);
        digitalWrite (18, LOW);

        break;
      }
    case 7:
      {
        digitalWrite (11, HIGH);
        digitalWrite (12, HIGH);
        digitalWrite (13, HIGH);
        digitalWrite (14, LOW);
        digitalWrite (15, LOW);
        digitalWrite (16, LOW);
        digitalWrite (17, LOW);
        digitalWrite (18, LOW);


        break;
      }
    case 8:
      {
        digitalWrite (11, HIGH);
        digitalWrite (12, HIGH);
        digitalWrite (13, HIGH);
        digitalWrite (14, HIGH);
        digitalWrite (15, HIGH);
        digitalWrite (16, HIGH);
        digitalWrite (17, HIGH);
        digitalWrite (18, LOW);

        break;
      }
    case 9:
      {
        digitalWrite (11, HIGH);
        digitalWrite (12, HIGH);
        digitalWrite (13, HIGH);
        digitalWrite (14, HIGH);
        digitalWrite (15, LOW);
        digitalWrite (16, HIGH);
        digitalWrite (17, HIGH);
        digitalWrite (18, LOW);

        break;
      }
  }
}


void s1_press() {
  if (state == 0)
    state = 1;
  else if (state == 1)
    state = 0;
  delay (100);
}

void s2_press() {
  if (halt == 0)
    halt = 1;
  else
    halt = 0;
  delay(100);
}

这是我使用的处理代码:

Serial myPort;  // Create object from Serial class
int val;        // Data received from the serial port
color c1 = #75C58E;
color c2 = #75C5FF;
color c3 = #D07633;
void setup() 
{
  size(420, 200);
  String portName = Serial.list()[1];
  myPort = new Serial(this, portName, 9600);
}

//0 - count up and start
//1 - count down
//2 - stop
//3 - reset

void draw() {
  background(255);
  if (mouseOverRect() == true) {  // If mouse is over square,
    fill(c1);                    // change color and
    myPort.write('1');              // send a '1' to indicate mouse is over square
  } else                          //will be equivalent to the count up/count down trigger
  {
    fill(0);
    myPort.write('0');
  }
  rect(50, 50, 100, 100);         // Draw a square

  if (mouseOverRect2() == true) {  // If mouse is over square,
    fill(c2);                    // change color and
    myPort.write('2');              // send a 2 to indicate mouse is over square
  } else                        //stop trigger
  {
    fill(0);
    myPort.write('0');
  }
  rect(160, 50, 100, 100);

    if (mouseOverRect3() == true) {  // If mouse is over square,
    fill(c3);                    // change color and
    myPort.write('3');              // send a '3' to indicate mouse is over square
  } else                      //reset
  {
    fill(0);
    myPort.write('0');
  }
  rect(270, 50, 100, 100);
}


boolean mouseOverRect() { // Test if mouse is over square
  return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
boolean mouseOverRect2() { // Test if mouse is over square
  return ((mouseX >= 160) && (mouseX <= 260) && (mouseY >= 50) && (mouseY <= 150));
}
boolean mouseOverRect3() { // Test if mouse is over square
  return ((mouseX >= 270) && (mouseX <= 370) && (mouseY >= 50) && (mouseY <= 150));
}

有关如何正确实施此功能的任何提示?它似乎不会影响向上/向下计数器的输出。谢谢!

1 个答案:

答案 0 :(得分:0)

您处理的代码处理代码似乎主要按照您的意图执行。

我建议在处理方面注意一些事项:

  1. 当一个盒子悬停时,你会连续(多次一秒钟)向Arduino发送消息。您可能希望将Arduino发送逻辑移到mousePressed()mouseReleased()函数内部以减少执行次数? (否则你的价值观会非常快速地变化,并且很难看到正在发生的事情)。
    1. 同样,当没有任何东西悬停时,你发送的是&#39; 0&#39; 0性格3次。否则,它会被发送两次(对于另一个不会被徘徊的盒子):取消/覆盖上一条命令消息的可能性很高(&#39; 1&#39;,&#39; #39; 2&#39;&#39 3&#39)
    2. 请记住,您正在阅读Arduino中的int但是从处理中发送char。因此,如果您希望Arduino中有1,则应在Processing中执行myPort.write(1);myPort.write('1');将发送ASCII char 1,由int 49表示。
  2. 这是一个稍微重新组织的处理代码版本,并实施了上述建议:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    int val;        // Data received from the serial port
    color c1 = #75C58E;
    color c2 = #75C5FF;
    color c3 = #D07633;
    void setup() 
    {
      size(420, 200);
      String portName = Serial.list()[1];
      myPort = new Serial(this, portName, 9600);
    }
    
    //0 - count up and start
    //1 - count down
    //2 - stop
    //3 - reset
    
    void draw() {
      background(255);
      if (mouseOverRect() == true) {  // If mouse is over square,
        fill(c1);                    // change color and
      } else                          //will be equivalent to the count up/count down trigger
      {
        fill(0);
      }
      rect(50, 50, 100, 100);         // Draw a square
    
      if (mouseOverRect2() == true) {  // If mouse is over square,
        fill(c2);                    // change color and
      } else                        //stop trigger
      {
        fill(0);
      }
      rect(160, 50, 100, 100);
    
        if (mouseOverRect3() == true) {  // If mouse is over square,
        fill(c3);                    // change color and
      } else                      //reset
      {
        fill(0);
      }
      rect(270, 50, 100, 100);
    }
    
    void mousePressed(){
      if (mouseOverRect() == true) {  // If mouse is over square,
        myPort.write(1);              // send a '1' to indicate mouse is over square
      } 
      else 
      if (mouseOverRect2() == true) {  // If mouse is over square,
        myPort.write(2);              // send a 2 to indicate mouse is over square
      } else                        //stop trigger
      if (mouseOverRect3() == true) {  // If mouse is over square,
        myPort.write(3);              // send a '3' to indicate mouse is over square
      } else                      //reset
      {
        fill(0);
        myPort.write(0);
      }
      rect(270, 50, 100, 100);
    }
    
    boolean mouseOverRect() { // Test if mouse is over square
      return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
    }
    boolean mouseOverRect2() { // Test if mouse is over square
      return ((mouseX >= 160) && (mouseX <= 260) && (mouseY >= 50) && (mouseY <= 150));
    }
    boolean mouseOverRect3() { // Test if mouse is over square
      return ((mouseX >= 270) && (mouseX <= 370) && (mouseY >= 50) && (mouseY <= 150));
    }
    

    您的Arduino代码看起来不那么有条理。我会一次测试一件事:

    1. 增加数字并显示
    2. 减少数字并显示
    3. 复位
    4. 处理串行输入
    5. 您可以使用CoolTerm轻松发送int而不是ASCII字符, 否则,如果您打算使用串行监视器,请从处理中发送'1'而不是1,但要确保您在Arduino中也要比较(例如if (val == '1')而不是if (val == 1)