Hey guys! So I've got a project where I try to control two servos and one electromagnet, I manage to get it all to work when I use the USB, but I want it all to work wirelessly so I've bought a BlueSMiRF Gold (Bluetooth Modem - BlueSMiRF Gold - WRL-10268 - SparkFun Electronics) and plugged it in to my Arduino Uno board. (3.3v, GND, RX to TX and TX to RX).
I uploaded a Arduino code (attached below)
#include <Servo.h>
#include <SoftwareSerial.h>
Servo servo1; Servo servo2;
void setup() {
servo1.attach(9);
servo2.attach(10);
Serial.begin(19200);
Serial.println("Ready");
}
void loop() {
static int v = 0;
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case '0'...'9':
v = v * 10 + ch - '0';
break;
case 's':
servo1.write(v);
v = 0;
break;
case 'w':
servo2.write(v);
v = 0;
break;
case 'd':
digitalWrite(8,HIGH);
break;
case 'a':
digitalWrite(8,LOW);
break;
}
}
}
Then I run a Processing code:
import processing.serial.*;
Serial port; // The serial port
int spos1=90; //Skapar servo_pos1 90 grader
int spos2=90; //Skapar servo_pos2 90 grader
void setup()
{
size(400, 400);
println(Serial.list()); // List COM-ports
//select second com-port from the list
port = new Serial(this, Serial.list()[1], 19200);
}
void draw() {
if (mouseX>25 && mouseX<75 && mouseY>25 && mouseY<75) { //VILLKOR: om musen är i den översta rutan
if (mousePressed && (mouseButton == LEFT)) { //VILLKOR: om vänster musknapp hålls ner
fill(0); //färglägger rektangel
spos1= spos1+1; //ändrar servo pos1
port.write("s"+spos1); //skriver spos1 till arduino
println(spos1); //printar spos1 i kommandofältet nedan
println(mouseX); //printar spos1 i kommandofältet nedan
} else if (mousePressed && (mouseButton == RIGHT)) {
fill(255);
spos1= spos1-1;
port.write("s"+spos1);
println(spos1);
} else {
fill(126);
}
rect(25, 25, 50, 50);
}
if (mouseX>25 && mouseX<75 && mouseY>75 && mouseY<125) { //VILLKOR: Om musen är i ruta 2
if (mousePressed && (mouseButton == LEFT)) {
fill(100, 50, 0); //färglägger rektangel
spos2= spos2+1; //ändrar servo pos
port.write("w"+spos2); //skriver spos till arduino
println(spos2); //printar spos i kommandofältet nedan
println(mouseX); //printar spos i kommandofältet nedan
} else if (mousePressed && (mouseButton == RIGHT)) {
fill(204, 102, 0);
spos2= spos2-1;
port.write("w"+spos2);
println(spos2);
} else {
fill(126);
}
rect(25, 75, 50, 50);
}
if (mouseX>25 && mouseX<75 && mouseY>125 && mouseY<175) { //VILLKOR: Om musen är i ruta 2
if (mouseButton == LEFT) {
fill(0, 100, 200); //färglägger rektangel
port.write("d"); //skriver spos till arduino
} else if (mouseButton == RIGHT) {
fill(0, 200, 100);
port.write("a");
} else {
fill(126);
}
rect(25, 125, 50, 50);
}
}
As I said it all works with the cable, so I thought that all I needed to do was to pair my computers bluetooth with the BlueSMiRF then change the number in the Processing code at "port = new Serial(this, Serial.list()[1], 19200);" to the corresponding number for the bluetooth device.
But nothing happens when I run the program and try to control it all.
Right now I'm running OSX, I'm going to boot into Windows tomorrow and see if there is any other guides to use, but I've been quite stuck on this spot all day so I'd like to wonder if anyone got some good tips?