Hello i am currently working my thesis (creation of BCI with emotiv, arduino,servo and processing). My goal is to send r and l keystrokes to move the servo right or left with emotiv. For start i have create a code in processing (actually i try to combine things that i found here in the forum) and to arduino. I get a null pointer exception error on valtowrite when i try to hit r or l keystrokes. The goal is to identify automatically the port .Please advise.
Here is the processing code:
import processing.serial.*;
Serial ser_port; // for serial port
PFont fnt; // for font
int num_ports;
boolean device_detected = false;
String[] port_list;
String detected_port = "";
void setup() {
size(400, 200); // size of application window
background(0); // black background
fnt = createFont("Arial", 16, true); // font displayed in window
println(Serial.list());
// get the number of detected serial ports
num_ports = Serial.list().length;
// save the current list of serial ports
port_list = new String[num_ports];
for (int i = 0; i < num_ports; i++) {
port_list[i] = Serial.list()[i];
}
}
void draw()
{
background(0);
// display instructions to user
textFont(fnt, 14);
text("1. Arduino or serial device must be unplugged.", 20, 30);
text(" (unplug device and restart this application if not)", 20, 50);
text("2. Plug the Arduino or serial device into a USB port.", 20, 80);
// see if Arduino or serial device was plugged in
if ((Serial.list().length > num_ports) && !device_detected) {
device_detected = true;
// determine which port the device was plugge into
boolean str_match = false;
if (num_ports == 0) {
detected_port = Serial.list()[0];
}
else {
// go through the current port list
for (int i = 0; i < Serial.list().length; i++) {
// go through the saved port list
for (int j = 0; j < num_ports; j++) {
if (Serial.list()[i].equals(port_list[j])) {
break;
}
if (j == (num_ports - 1)) {
str_match = true;
detected_port = Serial.list()[i];
}
}
}
}
}
// calculate and display serial port name
if (device_detected) {
text("Device detected:", 20, 110);
textFont(fnt, 18);
text(detected_port, 20, 150);
textFont(fnt, 14);
text("3. Now think Right or Left in this Window",20,190);
}
}
char valToWrite = 'r' & 'l'; //a value to send as a single byte
void keyPressed(){
valToWrite = key;
ser_port.write(valToWrite);
}
void keyReleased(){
valToWrite = 'r' & 'l';
ser_port.write(valToWrite);
}
AND here is the arduino code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
#define SERVO_PIN 9
int pos = 0; // variable to store the servo position
int lastMotionMillis = 0;
void setup() {
Serial.begin(9600);
myservo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
delay(300);
myservo.write(90);
}
void loop() {
if (Serial.available() ) {
char c = Serial.read();
if (c=='r') {
pos = pos +25;
myservo.write(pos);
lastMotionMillis = millis();
}
if (c=='l') {
pos = pos -25;
myservo.write(pos);
lastMotionMillis = millis();
}
}
}