Hey Leute,
Ich versuche Seit einiger Zeit zwei Getriebemotoren über einen Arduino mit Motorshield und einem Sparkfun Bluetooth Mate Gold anzusteuern.
Ich weiß nicht was ich alles Bei meinem Code falsch gemacht habe und ob dieser überhaupt funktionieren könnte.
Hier erstmal der Code den ich auf den Arduino hochgeladen habe:
Code:
#include <SoftwareSerial.h>
char val;
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
int bluetoothTx = 3;
int bluetoothRx = 4;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup() {
Serial.begin(9600); // start serial communication at 9600bps
}
void loop() {
if ( Serial.available() ) // if data is available to read
{
val = Serial.read(); // read it and store it in 'val'
}
if ( val == 'S' )
{
digitalWrite(pwmA, 255);
digitalWrite(dirA, LOW);
digitalWrite(brakeA, LOW);
digitalWrite(pwmB, 255);
digitalWrite(dirB, HIGH);
digitalWrite(brakeB, LOW);
}
delay(100);
if (val == 'W') {
digitalWrite(pwmA, 255);
digitalWrite(dirA, LOW);
digitalWrite(brakeA, LOW);
digitalWrite(pwmB, 255);
digitalWrite(dirB, HIGH);
digitalWrite(brakeB, LOW);
Serial.println("Vorwaerts 100%.");
}
delay(100);
if (val == 'S') {
digitalWrite(pwmA, 255);
digitalWrite(dirA, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(pwmB, 255);
digitalWrite(dirB, LOW);
digitalWrite(brakeB, LOW);
Serial.println("Rueckwaerts 100%.");
}
delay(100);
if (val == 'A') {
digitalWrite(pwmA, 255);
digitalWrite(dirA, LOW);
digitalWrite(brakeA, LOW);
digitalWrite(pwmB, 255);
digitalWrite(dirB, LOW);
digitalWrite(brakeB, LOW);
Serial.println("Nach links drehen");
}
delay(100);
if (val == 'D') {
digitalWrite(pwmA, 255);
digitalWrite(dirA, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(pwmB, 255);
digitalWrite(dirB, HIGH);
digitalWrite(brakeB, LOW);
Serial.println("Nach rechts drehen");
}
}
Und jetzt der Processing Code:
Code:
import processing.serial.*;
Serial port;
color currentcolor;
RectButton rect1;
boolean locked = false;
void setup() {
//set up window
size(500, 500);
color baseColor = color(102, 102, 102);
currentcolor = baseColor;
// List all the available serial ports in the output pane.
// You will need to choose the port that the Wiring board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());
// Open the port that the Wiring board is connected to (in this case 1
// which is the second open port in the array)
// Make sure to open the port at the same speed Wiring is using (9600bps)
port = new Serial(this, Serial.list()[2], 9600);
// Define and create rectangle button #1
int x = 100;
int y = 80;
int size = 80;
color buttoncolor = color(153, 102, 102);
color highlight = color(102, 51, 51);
rect1 = new RectButton(x, y, size, buttoncolor, highlight);
}
void draw() {
background(currentcolor);
stroke(255);
update(mouseX, mouseY);
rect1.display();
}
void update(int x, int y) {
if (locked == false) {
rect1.update();
} else {
locked = false;
}
//Turn LED on and off if buttons pressed where
//H = on (high) and L = off (low)
if (mousePressed) {
if (rect1.pressed()) { //ON button
currentcolor = rect1.basecolor;
port.write('W');
}
}
}
class Button {
int x, y;
int size;
color basecolor, highlightcolor;
color currentcolor;
boolean over = false;
boolean pressed = false;
void update()
{
if (over()) {
currentcolor = highlightcolor;
} else {
currentcolor = basecolor;
}
}
boolean pressed()
{
if (over) {
locked = true;
return true;
} else {
locked = false;
return false;
}
}
boolean over()
{
return true;
}
void display()
{
}
}
class RectButton extends Button {
RectButton(int ix, int iy, int isize, color icolor, color ihighlight)
{
x = ix;
y = iy;
size = isize;
basecolor = icolor;
highlightcolor = ihighlight;
currentcolor = basecolor;
}
boolean over()
{
if ( overRect(x, y, size, size) ) {
over = true;
return true;
} else {
over = false;
return false;
}
}
void display()
{
stroke(255);
fill(currentcolor);
rect(x, y, size, size);
}
}
boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
Immer wenn ich den Arduino an Strom anschließe fängt der eine Motor an zu laufen. Mein PC verbindet sich erfolgreich mit dem Arduino, aber das Programm kann nichts bewirken.
Schon mal Danke im Voraus
ToastCrafterHD
Lesezeichen