We are creating an interactive buzz wire game for our second year university project and were wanting to trigger a different animation each time the wire is touched
We have created a simple circuit and code in arduino to turn an LED on when a button is touched and have now changed the button to a wire but we need help trying to change the LED to some sort of media player on mac.
We are trying to use VLC but would you recommend anything else we should use that is more straight forward....
We are very new to programming and have a general knowledge of processing but not a great deal so as much help would be much appreciated
ps. we have seen some websites telling us how to use "terminal" to activate VLC (and done this) but we are not sure how to connect this with arduino?
It sounds like you have the two "ends" for your system.
The arduino can read an input and the mac can play VLC (I don't know what "terminal" is).
You need to think what can join the two.
The Arduino can send serial data so possibly instead of lighting the LED you could get it to send a message to the mac.
On the mac you have a program waiting for the message and then playing a video.
Hey
thanks very much for a reply
we have got a signal going to the mac from the arduino but we cant figure out how to change the signal into commands that could play videos on a media player.
Is there any programes you would recommend to use to wait for the message from the arduino and then play the video?
yea we have used apple script but the signal is not revived to turn the play list on that we have created in iTunes but the hot keys work but we were wondering if we need a middle program to translate the signal from arduino to the apple script? ill post you are code cause we can't get it to work :s if were doing it right
const int BUTTON = 7;
const int LED = 13;
int val = 0; //variable to store the state of the button
int old_val = 0; //stores the previous value of "val"
int state = 0; // 0 = BUTTON off while 1 = BUTTON on
unsigned long startTime = 0; // when did we begin pressing?
const byte ANIMATION = 3;
char* names[ANIMATION] = {
"ctrl A","ctrl B","ctrl C"
};
void setup()
{
pinMode(BUTTON,INPUT); //BUTTON is an input
Serial.begin(9600); //open the serial port to send data back to the computer
//at 9600 bits per second
}
void loop()
{
val = digitalRead(BUTTON); //read input value from the button
//check if there was a transition
if (val == HIGH) { //if we buzz the wire
Serial.println (names[random(0,ANIMATION)]);
digitalWrite (LED, HIGH);
delay (1000);
}
if (val == LOW ) { //if nothing happens
Serial.println ("Normal state");
digitalWrite ( LED, LOW);
}
}
here is the apple script for one of the hot keys
tell application "iTunes"
tell (track 1 of playlist "test") to play
weve tried the script you sent us and it works but then we tried to write the script to run iTunes by our selves but we dont know weather its correct or not and we do we dont know how to combine them in the script you gave us (or do we need to combine them ? or just save them separately?
heres the two scripts written by ourselves, to make an animation looping in the background and the other is to trigger random animations when the player buzzes the wire
What you have to do is to add the commands you want in what I sent you:-
set arduinoPort to "/dev/cu.usbmodem1d11"
-- this opens the port and resets the arduino
set arduino to serialport open arduinoPort bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
if arduino is equal to -1 then
display dialog " could not open port "
else
repeat -- repeat forever
-- wait until something is sent
set bytesIn to 0
repeat until bytesIn is not equal to 0
set bytesIn to serialport bytes available arduino
end repeat
set cmd to serialport read arduino for 1
set cmds to cmd as string
-- display dialog "Command recieved = " & cmds
if cmds is equal to "1" then
display dialog "Command 1 recieved"
end if
if cmds is equal to "2" then
display dialog "Command to quit recieved"
exit repeat
end if
end repeat -- of the forever repeat
-- note this next bit will reset the arduino
serialport close arduino
end if
So instead of having
display dialog "Command 1 received"
You put what you want to so when you get an ASCII 1 from the arduino. You might want to put:-
tell application "iTunes"
tell (track 1 of playlist "normal") to play
end tell
in place of this line. Also you add other commands, depending on what you want to do:-
if cmds is equal to "A" then
display dialog "Command A received"
end if
This will respond if the arduino sends an ASCII 'A'
Is there any way to speed up the time from when we touch the wire to playing movie? because there is a delay of 30 seconds, is that because of my computer/iTunes? would it be less of a delay if I used VLC or quicktime? ill show you the code incase there is a problem with that?
Arduino
const int BUTTON = 7;
const int LED = 13;
int val = 0; //variable to store the state of the button
int old_val = 0; //stores the previous value of "val"
int state = 0; // 0 = BUTTON off while 1 = BUTTON on
unsigned long startTime = 0; // when did we begin pressing?
char animation = 'A'; // the scary animations
char myChar = 'B'; //the normal aniamtion
void setup()
{
pinMode(BUTTON,INPUT); //BUTTON is an input
Serial.begin(9600); //open the serial port to send data back to the computer
//at 9600 bits per second
}
void loop()
{
val = digitalRead(BUTTON); //read input value from the button
//check if there was a transition
if (val == HIGH) { //if we buzz the wire
Serial.println ("A");
digitalWrite (LED, HIGH);
delay (1000);
}
if (val == LOW ) { //if nothing happens
Serial.println ("B");
digitalWrite ( LED, LOW);
}
}
Applescript
set arduinoPort to "/dev/cu.usbmodemfa131"
-- this opens the port and resets the arduino
set arduino to serialport open arduinoPort bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
if arduino is equal to -1 then
display dialog " could not open port "
else
repeat -- repeat forever
-- wait until something is sent
set bytesIn to 0
repeat until bytesIn is not equal to 0
set bytesIn to serialport bytes available arduino
end repeat
set cmd to serialport read arduino for 1
set cmds to cmd as string
-- display dialog "Command recieved = " & cmds
if cmds is equal to "A" then
tell application "iTunes"
tell (some track of playlist "test") to play
end tell
end if
end repeat -- of the forever repeat
-- note this next bit will reset the arduino
serialport close arduino
end if
Were also adding two buttons so would that be a factor in the delay?
Try and find out where the delay is. Put LEDs at the arduino to light when something is triggered. Put print statements inside the apple talk script bothe before telling iTunes to do something and then after. See where the delay is, then you can start to do something about it.
Hey i think this is where the delay is, in the code for the background animation
void loop()
{
val1 = digitalRead(BUTTON1); //read input value from button1
//check if there was a transition
if (val1 == HIGH) { //if we touch the start point
Serial.println ("1");
digitalWrite (LED, HIGH);
delay (100000);
}
if (val1 == LOW ) { //if nothing happens
digitalWrite ( LED, LOW);
}
I set the delay to (100000)...in order to play the whole animation... but it seems that I can't skip it once I press the 1st button I have to wait until it ends so we cant play a short scary animation in between(when the wire is touched) while the background animation is playing? is there any way we can interrupt the signal for about 2 seconds to play this short clip to scare people when they touch the wire and then continue playing the background animation again?
I found a thing called "hardware" interrupt could you explain this to me please does it mean I can interrupt the background animation when I press another button, can we use this? can I use the second button to be the interrupt trigger ?
Will it be able to stop the first animation and play another one, then go back to that loop animation after the interrupt one is finished?
Just never use delay.
Look at the blink without delay example built into the arduino PC software.
Interrupts are not going to help you with this problem, just get rid of the delay.