- Arduino
- Processing
The arduino software has been used to upload the program into the arduino board's internal memory, the program runs internally in the board and outputs through the serial connection to the laptop to the daemon file in Processing.
The code:
// RFID reader for Arduino
// Wiring version by BARRAGAN <http://people.interaction-ivrea.it/h.barragan>
// Modified for Arudino by djmatic
int val = 0;
char code[10];
int bytesread = 0;
void setup() {
Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(2,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(2, LOW); // Activate the RFID reader
}
void loop() {
if(Serial.available() > 0) { // if data available from reader
if((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial.available() > 0) {
val = Serial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
}
bytesread = 0;
digitalWrite(2, HIGH); // deactivate the RFID reader for a moment so it will not flood
delay(1500); // wait for a bit
digitalWrite(2, LOW); // Activate the RFID reader
}
}
}
// extra stuff
// digitalWrite(2, HIGH); // deactivate RFID reader The code can be found on the Arduino Playground site: http://www.arduino.cc/playground/Learning/PRFID
To get the code to work properly in my program I had to edit parts of the above code. In the above code the arduino prints "The tag code is: " followed by the code and a line spacing. This is not necessary for what I want so I edited the code to only print the code of the tag it is reading. I also added a stop variable to the code array, this helps when recieving the code in processing.
if(bytesread == 10) { // if 10 digit read is complete code[10]='\0'; //add end of string signal Serial.print(code); // print the TAG code }
Processing
The processing application is used to implement a daemon file which will run on the laptop and interact with the Arduino board. The program recieves the data from the arduino via the serial port and loads the necessary video depending on what tag has been used. A program that runs in the background is called a Daemon.
The code:
import processing.serial.*;
import processing.video.*;
import jmcvideo.*;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
Serial myPort; // The serial port
Movie myMovie;
void movieEvent(Movie m) {
m.read();
}
void setup() {
size(320,240,P3D);
minim = new Minim(this);
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], 2400);
background(0);
}
void draw() {
if (myMovie != null){
image(myMovie, 0, 0);
smooth();
}
}
void serialEvent(Serial p){
while (myPort.available() >= 10) {
//Video Code:
String command = "";
for (int i=0;i<10;i++){
command += myPort.readChar();
}
if (myMovie != null){
myMovie.dispose();
player.close();
minim.stop();
}
println(command);
myMovie = new Movie(this, command+".mov");
player = minim.loadFile(command+".wav", 2048);
myMovie.play();
player.play();
}
}
import processing.video.*;
import jmcvideo.*;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
Serial myPort; // The serial port
Movie myMovie;
void movieEvent(Movie m) {
m.read();
}
void setup() {
size(320,240,P3D);
minim = new Minim(this);
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[1], 2400);
background(0);
}
void draw() {
if (myMovie != null){
image(myMovie, 0, 0);
smooth();
}
}
void serialEvent(Serial p){
while (myPort.available() >= 10) {
//Video Code:
String command = "";
for (int i=0;i<10;i++){
command += myPort.readChar();
}
if (myMovie != null){
myMovie.dispose();
player.close();
minim.stop();
}
println(command);
myMovie = new Movie(this, command+".mov");
player = minim.loadFile(command+".wav", 2048);
myMovie.play();
player.play();
}
}
The main variables used (listed below) make the program possible. The first two variables are used for the sound, the serial port is used for the communication between Processing and the arduino board, the movie is the movie being displayed.
- AudioPlayer player;
- Minim minim;
- Serial myPort;
- Movie myMovie;
The program begins by setting up the size of the program and setting a black background. When a code is read by the arduino it is sent to processing which interprets the code and plays the corresponding audio (.wav) and video (.mov) file. The program checks which video is playing previously and cancels the audio swapping it with the file that is needed.
If you want to find out the serial's available on the ports use the following code:
If you want to find out the serial's available on the ports use the following code:
println(Serial.list());
No comments:
Post a Comment