我参与的当前项目需要在Processing的用户界面中将来自20个不同按钮的数据传输到Arduino Uno,以控制5个不同的通道,每个通道具有与按钮相对应的4种不同模式。目前,每个按钮都有效;但是,我注意到波特率会影响每个按钮的工作效果,并且似乎每个按钮的每个代码段越远,它的功能就越差。现在,我正在实现一个字节数组,该数组通过串行连接发送并由微控制器不断读取。 5个通道中的每个通道仅查看数组的一个索引,并根据与按钮对应的模式对其进行更新。是否有一种有效且简便的方法通过串行连接对阵列进行大规模多任务处理?
////////ARDUINO
void serialRead()
{
if(Serial.available())
{
int incomingValue = Serial.read();
values[currentValue] = incomingValue;
currentValue++;
if(currentValue > 4)
{
currentValue = 0;
}
/////////torso
if(incomingValue == 65 || 66 ||67 || 68 || 69 || 70 || 71 || 72)
{
values[0] = incomingValue;
}
/////////shoulders
if(incomingValue == 73 || 74 ||75 || 76 || 77 || 78 || 79 || 80)
{
values[1] = incomingValue;
}
/////////Arms
if(incomingValue == 81 || 82 || 83 || 84 || 85 || 86 || 87 || 88)
{
values[2] = incomingValue;
}
/////////Hands
if(incomingValue == 89 || 90 || 97 || 98 || 99 || 100 || 101 || 102)
{
values[3] = incomingValue;
}
/////////Neck
if(incomingValue == 103 || 104 || 105 || 106 || 107 || 108 || 109 || 110)
{
values[4] = incomingValue;
}
}
}
///////PROCESSING
void draw(){ // graphics and functions of program
byte out[] = new byte[5];
out[0] = byte(torso);
out[1] = byte(shoulder);
out[2] = byte(arm);
out[3] = byte(hand);
out[4] = byte(neck);
myPort.write(out);
///under each button, the corresponding char is assigned to torso, shoulder,etc.
///they are then converted into their corresponding byte formats and sent through serial
仔细检查字节的传输顺序。在某些系统中,如果您向Arduino发送{1 2 3 4 5},它将收到{5 4 3 2 1}