集成Arduino和Processing - 按钮计数器 [英] Integrating Arduino and Processing - button counter

查看:22
本文介绍了集成Arduino和Processing - 按钮计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望集成 Arduino 控件和处理接口.在我的 Arduino 代码中,有三个按钮连接到引脚 a1、a0 和 d0(均为数字读取).

I am looking to integrate Arduino controls and Processing Interface. In my Arduino code, there are three buttons attached to pin a1, a0 and d0 (all digitalRead).

int x;// assigned to A0 input
int y;// assigned to A1 input
int z; //assigned to D0 input
int votes[3]={0,0,0};

void setup() {
  // initialize the serial communication
  Serial.begin(9600);

  while(!Serial);
}

void loop() {
  // first we need to read the values from the BUTTONS

  x = digitalRead(A0);

  y = digitalRead(A1);

  z = digitalRead(0);

  if(digitalRead(A0) == HIGH) 
    {
    Serial.print("cat1 ");                  
    Serial.print(votes[0]=votes[0]+1);
    Serial.print("\n");
    }
   else if(digitalRead(A1) == HIGH)
    {
    Serial.print("cat2 ");
    Serial.print(votes[1]=votes[1]+1);
      Serial.print("\n");
    }
   else if(digitalRead(0) == HIGH) 
    {
    Serial.print("cat3 ");
    Serial.print(votes[2]=votes[2]+1);
    Serial.print("\n");
    }

    delay(200);
}

我希望计数器在每次按下按钮时递增,并在处理过程中显示为条形图.每当按下按钮时,条形图都会上升一定量.在这种情况下,如果按下,我希望绘制三行(与三个按钮相关联)之一.

I would like the counter to increment every time the button is pressed and display in processing as a bar graph. Whenever the button is pressed, the bar graph goes up by some amount. In this case I want one of the three lines (associated to three buttons) to draw, if pressed.

处理代码如下:

import processing.serial.*;

// Serial Port variables
Serial myPort;
String buff = "";
String buff1 = "";
String buff2 = "";
int index = 0;
int NEWLINE = 10;
// Store the last 256 values from the sensors so we can draw them.
int[] valuesx = new int[256];
int[] valuesy = new int[256];
int[] valuesz = new int[256];

void setup()
{ 
  // set size of the window 
  size(512, 512);
  // turn on anti-aliasing, this makes things look smoother 
  smooth();

  println(Serial.list()); // use this to determine which serial port is your Arduino
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
}

// draw() happens every frame of the sketch. This is where all the calculations are made.
// When draw() is finished executing, it executes again, over and over.
void draw() {
  // set the background to purple
  background(87, 36, 124);
  // set stroke weight(thickness of the line) to 5 pixels
  strokeWeight(5);

  for (int i = 0; i < 255; i++) {

    stroke(247, i);
    // draw the line (x1, y1, x2, y2)
    line(1, valuesx[i], 1, valuesx[i + 1]);
    line(5, valuesy[i], 5, valuesy[i + 1]);
    line(10, valuesz[i], 10, valuesz[i + 1]);
  }

  // Check the Serial port for incoming data
  while (myPort.available () > 0) {
    // if there is data waiting...
    // execute serialEvent() function. Look below to see how SerialEvent works.
    serialEvent(myPort.read());
  }
}

// serialEvent controls how incoming serial data from the Arduino module is handled
void serialEvent(int serial)
{
  if (serial != NEWLINE) {
    // Store all the characters on the line.
    buff += char(serial);
  } 
  else {
    // The end of each line is marked by two characters, a carriage
    // return and a newline.  We're here because we've gotten a newline,
    // but we still need to strip off the carriage return.
    buff = buff.substring(0, buff.length()-1);
    index = buff.indexOf(",");
    buff1 = buff.substring(0, index);
    buff2 = buff.substring(index+1, buff.length());

    // Parse the String into an integer.  We divide by 4 because
    // analog inputs go from 0 to 1023 while colors in Processing
    // only go from 0 to 255.
    int x = Integer.parseInt(buff1)/2;
    int y = Integer.parseInt(buff2)/2;

    // Clear the value of "buff"
    buff = "";

    // Shift over the existing values to make room for the new one.
    for (int i = 0; i < 255; i++)
    {
      valuesx[i] = valuesx[i + 1];
      valuesy[i] = valuesy[i + 1];
    }

    // Add the received value to the array.
    valuesx[255] = x;
    valuesy[255] = y;
  }
}

不幸的是,这是我按下按钮时出现的错误.

Unfortunately, here's the error that I get when I press a button.

推荐答案

看起来您希望 buff 包含逗号,但它没有.

It looks like you're expecting buff to contain a comma, but it doesn't.

当你调用 indexOf(",") 时,它返回 -1 表示没有找到逗号:

When you call indexOf(","), it returns -1 to indicate that no comma was found:

index = buff.indexOf(",");

然后当你调用 substring() 时,你传入的是 -1 作为 index,它不是一个有效的参数:

Then when you call substring(), you're passing in -1 as the index, which isn't a valid parameter:

buff1 = buff.substring(0, index);

这就是导致错误的原因.现在您的下一个问题将是:为什么 buff 不包含逗号?

That's what's causing the error. Now your next question will be: why doesn't buff contain a comma?

答案将取决于您对 Arduino 的期望.尝试打印出 buff 的值,看看它是什么.

And the answer to that is going to depend on what you're expecting from your Arduino. Try printing out the value of buff to see what it is.

我对 Arduino 了解不多,但我在该代码中没有看到任何打印逗号的地方.

I don't know much about Arduino, but I don't see anywhere in that code that prints a comma.

这篇关于集成Arduino和Processing - 按钮计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆