Mus kontroll arduino uno på pc (2 / 3 steg)
Steg 2: Programozás/programmering
Arduinoen
int pot1;
int pot2;
void setup() {
Serial.BEGIN(9600);
}
void loop() {
pot1 = analogRead(A0);
pot2 = analogRead(A1);
Serial.Print (pot1);
Serial.Print(",");
Serial.println (pot2);
Delay(50);
}
Bearbetning
import java.awt.*;
import javax.swing.SwingUtilities;
import java.awt.MouseInfo;
import java.awt.Point;
int xx = 0, yy = 0, nxx = 0, nyy = 0;
Robot eger; (en) Robot mus;
import processing.serial.*;
Seriell port;
void setup() {
size(100,100);
försök {port = nya Serial(this,"com9",9600); //com= arduino com (Arduino ide -> Verktyg -> Port: "comX"
port.bufferUntil('\n');
} fånga (undantaget ex) {
text ("Nincs soros \n port komunikáció!", 10, 14); (EN) "Det finns ingen seriell kommunikation!"
} prova {
Eger = nya Robot(); (Sv)-mus = nya Robot();
} fånga (AWTException e) {
println ("Nem támogatott en robot klass!"); Exit(); (EN) "inte stöds robot klass!"
}
} makulera draw()
{System.out.println ("("+ nxx +"," + nyy + "nyy+")""), nxx=MouseInfo.getPointerInfo () .getLocation () .x; nyy=MouseInfo.getPointerInfo () .getLocation () .y;}
void serialEvent(Serial myPort) {
String bejovo = myPort.readStringUntil('\n');
om (bejovo! = null) {
bejovo = trim(bejovo);
int [] potik = int (split (bejovo, ","));
om (potik.length > = 2) {
om (nxx > xx + 20 || nyy > åå + 20 || nxx < xx-20|| Nyy < yy-20) {xx = nxx; yy=nyy;delay(250);}
annat {
xx = potik [0]; yy = potik [1]. eger.mouseMove(xx,yy); }
} } }