从处理回Arduino的发送数据 [英] Sending data from processing back to arduino
问题描述
我们有计数有关brexit refenrendum鸣叫为肯定和否定的数目的方案。这里有一个引擎收录的链接,因为我不能得到code正确格式: http://pastebin.com/ G3v02KjF
We have a program that counts the number of tweets for yes and no relating to the brexit refenrendum. Here's a link to a pastebin since I couldn't get the code to properly format: http://pastebin.com/G3v02KjF
总之,我们把从处理信息到Arduino得到使用Twitter4j库的tweet。我们统计的微博,并获得了一些我们想成为一个伺服的角度。但是,我们无法弄清楚如何将此号码发送回Arduino的,所以我们可以控制伺服。
Anyway, we're sending information from processing to the arduino to get the tweets using the Twitter4j library. We've counted the tweets and received a number that we want to be the angle of a servo. However, we can't figure out how to send this number back to arduino so we can control the servo.
我们将如何去这样做呢?
How would we go about doing this?
推荐答案
有一个看串行库参考和的write()特别功能。
既然你传递一个值,角度(从0到180),这个整数值可以作为发送一个字节。例如,在 calculateAngle()
功能,您可以将其发送到Arduino的:
Since you're passing a single value, the angle (from 0 to 180) this integer value can be send as a single byte. For example in the calculateAngle()
function you can send it to arduino:
if(myPort != null) myPort.write((int)angle);
(裸记住,浮点值转换为整数将地板/轮值向下)
(Bare in mind, casting the float value to an integer will floor/round the value down)
在Arduino的一面,你只需要读值回,如果有至少一个字节可用。这是一个好主意,约束()值,以防万一有错误(和你不希望使用无效伺服位置)
On the Arduino side, you simply need to read the value back, if there is at least one byte available. It's a good idea to constrain() the value, just in case there are errors (and you wouldn't want to use invalid servo positions)
if(Serial.available() > 0){
int angle = constrain(Serial.read(),0,180);
myservo.write(angle);
}
一定要签出下面的例子:
Be sure to checkout the following examples:
- 加工>例子>库>串行> SimpleWrite (用在处理草图的底部提供了Arduino的code)
- 的Arduino>例子> 04.Communication> 的serialEvent
- Processing > Examples > Libraries > serial > SimpleWrite (use the Arduino code provided at the bottom of the Processing sketch)
- Arduino > Examples > 04.Communication > SerialEvent
这篇关于从处理回Arduino的发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!