处理和Arduino的串行连接

发布于 2025-02-04 09:55:46 字数 915 浏览 4 评论 0原文

我正在尝试建立从处理到Arduino的串行连接。这个想法是,当我按“ a”时,来自Arduino的扬声器会产生声音,而另一个键则带有不同的音符。但是,我对代码有些困扰。谁能让我知道我的代码问题是什么?

处理代码:

import processing.serial.*;
Serial myPort; 
int val; // Data received from the serial port

void setup()
{
size(200, 200);
String portName = Serial.list()[0]; 
myPort = new Serial(this, portName, 9600);
}

void draw() {
}

 void keyPressed(){
   if (key == 'a'){
     myPort.write(1); 
   }
   if (key == 'd'){
     myPort.write(2); 
   }  
 }
 void keyReleased(){
   myPort.write(0);
 } 

Arduino代码:

char val; // Data received from the serial port
int speakerPin = 8;

void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600); 
}

void loop() {
while (Serial.available()) { 
val = Serial.read(); 
}
if (val == '1') {  
tone(speakerPin, 262, 200); 
} else if (val == '2'){
tone(speakerPin, 523, 200); 
}
delay(100); 
}

非常感谢!

I'm trying to make a serial connection from processing to arduino. The idea is that when I press "a", the speaker from Arduino will produce a sound and the same goes with another key pressed but with a different note. However, I'm a bit stuck with the codes. Can anyone let me know what is the problem of my codes?

Processing code:

import processing.serial.*;
Serial myPort; 
int val; // Data received from the serial port

void setup()
{
size(200, 200);
String portName = Serial.list()[0]; 
myPort = new Serial(this, portName, 9600);
}

void draw() {
}

 void keyPressed(){
   if (key == 'a'){
     myPort.write(1); 
   }
   if (key == 'd'){
     myPort.write(2); 
   }  
 }
 void keyReleased(){
   myPort.write(0);
 } 

Arduino code:

char val; // Data received from the serial port
int speakerPin = 8;

void setup() {
pinMode(speakerPin, OUTPUT);
Serial.begin(9600); 
}

void loop() {
while (Serial.available()) { 
val = Serial.read(); 
}
if (val == '1') {  
tone(speakerPin, 262, 200); 
} else if (val == '2'){
tone(speakerPin, 523, 200); 
}
delay(100); 
}

Many many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半山落雨半山空 2025-02-11 09:55:46

你太近了!

问题在于您正在检查字符的Arduino侧,但是,在处理方面,您要发送实际值,而不是ASCII字符。

1 != '1' (49)
2 != '2' (50)

在处理中,只需使用char键入单引号符号:

myPort.write('1');

...

myPort.write('2'); 

(与myport.write(49); for'1',相同MyPort.Write(50);'2')

或者,您可以更改在Arduino侧检查的方式,以免使用Chars与您从处理中发送的方式保持一致。例如

if (val == 1)

(例如(Val =='1')而不是)。

You are so close!

The issue is on the Arduino side you're checking characters, however, on the Processing side you're sending the actual values, not the ASCII characters.

1 != '1' (49)
2 != '2' (50)

In Processing, simply use the char type single quote symbols:

myPort.write('1');

...

myPort.write('2'); 

(which is the same as myPort.write(49); for '1', myPort.write(50); for '2')

Alternatively you can change the way you're checking on Arduino side to not use chars to be consistent with how you're sending from Processing. e.g.

if (val == 1)

(instead of if (val == '1')).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文