Hey i am trying to control arduino using leap motion controller. For my final year project i need to control the motion of my soft robot using this leap motion device. But in order to start with basic, i was simply testing the Arduino builtin led with leap and see if it can be controlled but unfortunately, i cant.
The reason i understand is that there is some kind of problem with serial communication, cz when i open the serial monitor of arduino, the prcoessing code doesnt run and vice versa. And the error that comes is 'COM3 port busy' Ill share the code for reference.
also i have Processing 3 installed, i hve also tried the latest processsing but it has the same issue as well.
ARDUINO:
int val;
// int led = 3; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
if (Serial.available() == '\n') { // Check if there is a new message
val= Serial.read();
// 1st fragment
if (val<60) {
digitalWrite(LED_BUILTIN,LOW);
}
if (val>60) {
digitalWrite(LED_BUILTIN,HIGH);
}
// 2nd fragment
/* val = map(val, 30, 165, 0, 255); // map(value, fromLow, fromHigh, toLow, toHigh)
analogWrite(led, val);*/
}
}
PROCESSING:
import processing.serial.*;
// Processing code is available to download - link below the video.
import processing.serial.*;
import de.voidplus.leapmotion.*;
LeapMotion leap;
Serial myPort;
void setup() {
leap = new LeapMotion(this);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
background(0);
for (Hand hand : leap.getHands()) {
pushMatrix();
float handSize = hand.getSphereRadius();
int handSize2 = int(handSize);
myPort.write(handSize2);
println(handSize2);
popMatrix();
}
I moved your topic to an appropriate forum category @huzefa_turab.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
Please edit your post, select all code and click the <CODE/> button and save your post. This will apply so-called code tags to the code which makes it easier to read, easier to copy and the forum software will display it correctly.
Please provide a link; I have no idea what it is.
Only one application can use a serial port at a time. When using your processing application, you can't use the serial monitor in the IDE, when using the serial monitor you can't use the port in the Processing app.
just got another test code from internet, according to me the code is fine but i think my leap motion controller isn't taking the input as the Processing monitor doesnt change the value from 0 to 5 when i bring my hand above.
Processing:
import processing.serial.*;
import com.leapmotion.leap.*; //leap motion library
import processing.serial.*; //serial communication library
Controller leap; //define a controller
Serial port; //define a port
void setup(){
size(250,250); //sketch size
leap = new Controller(); // initialize the controller
port = new Serial(this, Serial.list()[0], 9600);//initialize the port in my case its [2]
}
void draw(){
FingerList fingers = leap.frame().fingers(); //finger list to get the fingers count
int count = fingers.count(); // integer holds the count of fingers
background(0); // box background color
fill(255); // text color
textSize(height/2); // text size
text(count, 90, 160); // text value and position on the box
port.write(count+"\r"); // port sends the integer to arduino*/
}
Arduino:
int c = -1; // int for income value
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200); //set serial with baud rate
}
void loop()
{
if (Serial.available()>0){ //if we have incoming value
c =Serial.read()-48; //read the value minus 48
// Serial.write(c);
if(c == 0) digitalWrite(LED_BUILTIN,LOW); //if 0 fingers turn off led
else if(c == 5) digitalWrite(LED_BUILTIN,HIGH); //if 5 fingers turn on led
}
}