Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实现一个处理控制的Arduino上/下计数器_Arduino_Processing - Fatal编程技术网

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

实现一个处理控制的Arduino上/下计数器,arduino,processing,Arduino,Processing,我正在制作一个Arduino递增/递减计数器,其输出可以由处理程序控制。它在训练护盾的3位7段显示屏上显示倒计时,并可使用按钮使其向上、向下、停止和复位。处理代码也应该能够这样做,但是通过软件而不是按钮。我的实现思想是让曾经悬停在上面的框发送信号,切换上下计数、停止和重置,这样我就可以使用SerialWrite示例。下面显示的是我的向上/向下计数器Arduino代码,包括与处理接口所需的串行连接 unsigned char digit_1 = 7; unsigned char digit_2 =

我正在制作一个Arduino递增/递减计数器,其输出可以由处理程序控制。它在训练护盾的3位7段显示屏上显示倒计时,并可使用按钮使其向上、向下、停止和复位。处理代码也应该能够这样做,但是通过软件而不是按钮。我的实现思想是让曾经悬停在上面的框发送信号,切换上下计数、停止和重置,这样我就可以使用SerialWrite示例。下面显示的是我的向上/向下计数器Arduino代码,包括与处理接口所需的串行连接

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);
}
无符号字符数字_1=7;
无符号字符数字_2=8;
无符号字符数字_3=10;
int num1=0;
int num2=0;
int num3=0;
int state=0;
int=0;
char-val//从串行端口接收的数据
无效设置()
{
Serial.begin(9600);
对于(int x=11;x<19;x++)
{
pinMode(x,输出);
}
pinMode(数字_1,输出);
pinMode(数字_2,输出);
pinMode(数字_3,输出);
//连接中断(0,s1_压力,上升);
//连接中断(1,s2_压力,上升);
}
void循环()
{
while(Serial.available())
{val=Serial.read();
}
如果(val==1)
{
状态=1;
}
else//if(val==0)
{
状态=0;
halt=0;
}
如果(val==2)
{
halt=1;
}
其他的
{
状态=0;
halt=0;
}
如果(暂停==0){
如果(状态==0){
num3++;
如果(num3==10){
num3=0;
num2++;
}
如果(num2==10){
num2=0;
num1++;
}
如果(num1==10){
num1=0;
num2=0;
num3=0;
}
}
如果(状态==1){
num3--;
如果(num3==-1){
num3=9;
num2--;
}
如果(num2==-1){
num2=9;
num1--;
}
如果(num1==-1){
num3=9;
num2=9;
num1=9;
}
}
}
对于(int x=0;x<80;x++){
数字写入(数字_3,高);
数字写入(数字2,低位);
数字写入(数字_1,低);
显示(num1);
延迟(1);
数字写入(数字_3,低位);
数字写入(数字2,高);
数字写入(数字_1,低);
显示(num2);
延迟(1);
数字写入(数字_3,低位);
数字写入(数字2,低位);
数字写入(数字_1,高);
显示(num3);
延迟(1);
}
}
无效显示(无符号字符数)
{
开关(num)
{
案例0:
{
数字写入(11,高);
数字写入(12,高);
数字写入(13,高);
数字写入(14,高);
数字写入(15,高);
数字写入(16,高);
数字写入(17,低);
数字写入(18,低);
打破
}
案例1:
{
数字写入(11,低);
数字写入(12,高);
数字写入(13,高);
数字写入(14,低);
数字写入(15,低);
数字写入(16,低);
数字写入(17,低);
数字写入(18,低);
打破
}
案例2:
{
数字写入(11,高);
数字写入(12,高);
数字写入(13,低);
数字写入(14,高);
数字写入(15,高);
数字写入(16,低);
数字写入(17,高);
数字写入(18,低);
打破
}
案例3:
{
数字写入(11,高);
数字写入(12,高);
数字写入(13,高);
数字写入(14,高);
数字写入(15,低);
数字写入(16,低);
数字写入(17,高);
数字写入(18,低);
打破
}
案例4:
{
数字写入(11,低);
数字写入(12,高);
数字写入(13,高);
数字写入(14,低);
数字写入(15,低);
数字写入(16,高);
数字写入(17,高);
数字写入(18,低);
打破
}
案例5:
{
数字写入(11,高);
数字写入(12,低);
数字写入(13,高);
数字写入(14,高);
数字写入(15,低);
数字写入(16,高);
数字写入(17,高);
数字写入(18,低);
打破
}
案例6:
{
数字写入(11,高);
数字写入(12,低);
数字写入(13,高);
数字写入(14,高);
数字写入(15,高);
数字写入(16,高);
数字写入(17,高);
数字写入(18,低);
打破
}
案例7:
{
数字写入(11,高);
数字写入(12,高);
数字写入(13,高);
数字写入(14,低);
数字写入(15,低);
数字写入(16,低);
数字写入(17,低);
数字写入(18,低);
打破
}
案例8:
{
数字写入(11,高);
数字写入(12,高);
数字写入(13,高);
数字写入(14,高);
数字写入(15,高);
数字写入(16,高);
数字写入(17,高);
数字写入(18,低);
打破
}
案例9:
{
数字写入(11,高);
数字写入(12,高);
数字写入(13,高);
数字写入(14,高);
数字写入(15,低);
数字写入(16,高);
数字写入(17,高);
数字写入(18,低);
打破
}
}
}
无效s1_press(){
如果(状态==0)
状态=1;
else if(state==1)
状态=0;
延迟(100);
}
无效s2_press(){
如果(暂停==0)
halt=1;
其他的
halt=0;
延迟(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));
}
串行myPort;//从串行类创建对象
int val;//从串行端口接收的数据
颜色c1=#75C58E;
颜色c2=#75C5FF;
颜色c3=#D07633;
无效设置()
{
大小(420200);
圣
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));
}