Hello everyone. I am playing around with Arduino and Applescript. Basically what i have is 3 tilt sensors on the Arduino, so the information from the sensors will be collected in a 3 digit code for example 101 or 001 depending on what sensors are HIGH or LOW. Then the Arduino code processes the information and outputs a one digit number for example 101 = 3 or 001 = 4. The Output is a decimal:
Serial.print(x, DEC) // where x is an integer
I am new to applescript, but i managed to come up with this simple code that works,
set use_port to item 1 of (get serialport list)
set myPort to serialport open use_port bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
delay 1
repeat
if ((serialport bytes available myPort) ≥ 0) then
set x to serialport read myPort
beep
display dialog (x)
else
exit repeat
end if
end repeat
serialport close myPort
My question is, how do i display the Input in the Applescript console/results tab? Because the “display dialog” breaks the fluidity of the script.
AND do i have to convert the input information from Arduino in order to use Operators?
Made some progress so will post it here. Now i am able to loop the serial port read in order to get the input and monitor it in the Replies event Log:
tell application “AppleScript Editor”
serialport bytes available 8
→ 0
serialport read 8
→ “3”
end tell
tell current application
beep
end tell
But i can not use the Operators. So the line…
if x is equal to “3” then
display dialog “Success”
end if
… does not work. Any idea why?
The code is here:
set use_port to item 1 of (get serialport list)
set myPort to serialport open use_port bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
delay 0.5
repeat
if ((serialport bytes available myPort) ≥ 0) then
repeat
set x to serialport read myPort
if (x > 0) then
delay 0.1
beep
if x is equal to "3" then
display dialog "Success"
end if
end if
exit repeat
end repeat
else
exit repeat
end if
end repeat
-- close the serial port handle
serialport close use_port
Finally i managed it to work. I will post this code for people that might use it as reference. There is only one annoying problem left and i have no clue what so ever on how to solve it. When i plug in Arduino in to the USB, and run the script, everything goes smooth. When i stop the script, and run it again, Applescript crashes. So basically i can only run the script again after a full restart of Applescript. Any ideas on how i could run the script without restarting Applescript?
set use_port to "/dev/cu.usbmodem411"
if (get serialport list) contains use_port then
set onMode to true
set myPort to serialport open use_port bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
delay 0.5
serialport close use_port
repeat while onMode is true
set x to serialport read myPort
if (x > 0) then
beep
if x is equal to "3" then
display dialog "Success"
end if
end if
end repeat
end if