I am currently working on a project connecting and controlling Arduino Uno via Bluetooth, using Processing via PC, but the problem is, it works when the USB port with Arduino is connected, but when I disconnect it and just power the Arduino with a battery, it does not work.
Here is the processing code:
import processing.serial.*;
Serial myPort;
int val;
int value;
int circleX, circleY;
int circleSize = 133;
boolean circleOver = false;
PImage img1;
PImage img2;
PImage img3;
void setup() {
size(240, 320);
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
//String portName = Serial.list()[1];
//myPort = new Serial(this, portName, 9600);
circleX = width-120;
circleY = height-175;
ellipseMode(CENTER);
img1 = loadImage(âButtonUp.pngâ);
img2 = loadImage(âButtonOver.pngâ);
img3 = loadImage(âButtonDown.pngâ);
}
void draw(){
update(mouseX, mouseY);
if (myPort.available()>0) {
value = myPort.read()-48;
print(value);
}
if(circleOver && mousePressed == true){
background(170,50,255);
image(img3,0,0);
myPort.write(âHâ);
} else if (circleOver && mousePressed == false && value == 1){
background(170,50,255);
image(img2,0,0);
} else if (value == 1){
background(170,50,255);
image(img2,0,0);
} else {
background(170,50,255);
image(img1,0,0);
myPort.write(âLâ);
}
}
void update(int x, int y) {
if( overCircle(circleX, circleY, circleSize) ) {
circleOver = true;
} else {
circleOver = false;
}
}
boolean overCircle(int x, int y, int diameter) {
float disX = x - mouseX;
float disY = y - mouseY;
if(sqrt(sq(disX) + sq(disY)) < diameter/2) {
return true;
} else {
return false;
}
}
Here is the Arduino code:
#include <SoftwareSerial.h>
#define RxPin 0
#define TxPin 1
SoftwareSerial BTSerial(RxPin, TxPin);
const int ledPin = 3;
const int switchPin = 4;
char val;
void setup()
{
pinMode(RxPin, INPUT);
pinMode(TxPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(switchPin, INPUT);
BTSerial.begin(9600);
}
void loop() {
if(digitalRead(switchPin) == HIGH) {
BTSerial.print(1);
} else {
BTSerial.print(0);
}
delay(16);
if (BTSerial.available());
{
val = BTSerial.read();
if (val == âHâ) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
}
Any help is appreciated
Thank you