2016-03-07 66 views
0

我已经设法将Arduino草图连接到处理草图,但我被困在如何让Arduino控制处理中的对象。如何通过Arduino草图控制处理中的对象?

使用倾斜传感器时,目标是当倾斜传感器倾斜一个方向时,它将以这种方式移动物体,然后当它倾斜时,另一种方式会以另一种方式移动物体。

任何人都可以帮忙吗?

这是我的Arduino代码:

void setup() 
{ 
    // put your setup code here, to run once: 
Serial.begin(9600); 
} 

void loop() 
{ 
    // put your main code here, to run repeatedly: 
Serial.println("Hello, World!"); 

delay(100); 

} 

这是我的代码进行处理:

import processing.serial.*; 
Serial myPort; 
String val; 
PShape bike; 

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

size(800, 600); 
bike = createShape(RECT, 0, 0, 200, 200); 
bike.setFill(color(102, 204, 0)); 
bike.setStroke(false); 

} 

void draw() 
{ 
    if (myPort.available() > 0) 
    { // If data is available, 
    val = myPort.readStringUntil('\n');   // read it and store it in val 
    } 
println(val); //print it out in the console 

shape(bike, 0, 0); 

} 

回答

0

打破你的问题分解成更小的碎片。

写一个简单的示例草图(换句话说,[mcve])只做一件事:它只是读取Arduino中的值并将它们打印到控制台。它甚至不需要在屏幕上绘制任何东西。

写另一个[mcve]只是做一件事:它只是在屏幕上移动一个对象。注意,这应该是而不是涉及你的Arduino代码。尽量使其尽可能独立。

当你将这两个工作完全分开时,你可以考虑将它们合并成一个草图。如果你遇到困难,你可以发布[mcve]以及一个特定的技术问题。

下面是移动基于鼠标位置的对象草图,只是去让你开始:

float objectX = 0; 

void setup() { 
    size(500, 100); 
} 

void draw() { 

    if (mouseX > objectX) { 
    objectX ++; 
    } 

    background(0); 
    ellipse(objectX, height/2, 25, 25); 
} 
0

你的Arduino的代码看起来that's唯一的hello world。 我使用arduino库(传感器,执行器)使用处理,做的是分割2个arduinos的任务:一个使用libs并从另一个arduino发送/接收数据。这第二个arduino加载了标准的firmdata(固件),允许从处理草图中轻松地直接交流:http://playground.arduino.cc/Interfacing/Processing

相关问题