I have found the problem now. It was embarrassingly simple.
It was simply a case of the script sending the commands too quickly. A simple delay 0.5 in-between each one fixed it nicely.
The scripts are below (with a few minor alterations).
Script for setting the LG’s wake timer:
set commander to load script "/Library/Scripts/ArduinoIRCommands.scpt"
set controlArduino to "/Library/Scripts/arduino-serial-master/arduino-serial -b 9600 -p /dev/tty.usbmodem621 -s "
on setup()
set usertime to my GetWakeTime()
if usertime is false then
end if
set confirmation to my CheckUserTime(usertime)
return confirmation
end setup
set waketime to my setup()
set a to first item of waketime as integer
if a is equal to 0 then
set a to 10
end if
set b to item 2 of waketime as integer
if b is equal to 0 then
set b to 10
end if
set c to item 3 of waketime as integer
if c is equal to 0 then
set c to 10
end if
set d to item 4 of waketime as integer
if d is equal to 0 then
set d to 10
end if
set h1 to my getNUMBERS(controlArduino, a)
set h2 to my getNUMBERS(controlArduino, b)
set m1 to my getNUMBERS(controlArduino, c)
set m2 to my getNUMBERS(controlArduino, d)
commander's sendGET2WAKE(controlArduino)
delay 10
do shell script h1
delay 0.5
do shell script h2
delay 0.5
do shell script m1
delay 0.5
do shell script m2
delay 0.5
commander's sendWAKE2SLEEP(controlArduino)
on CheckUserTime(usertime)
if (count of usertime) is not 4 then
my setup()
else
return usertime
end if
end CheckUserTime
on GetWakeTime()
try
display dialog "Use 24hr time." & return & "eg. 0929 for 09:29am" with title "Set TV Wake Time" default answer "1029" buttons {"OK", "Cancel"} default button 1
copy the result as list to {button_returned, text_returned}
set usertime to items of text_returned
on error
display notification "error -128 occurred"
set usertime to false
end try
end GetWakeTime
on getNUMBERS(sc, x)
return item x of {sc & "m", sc & "n", sc & "o", sc & "p", sc & "q", sc & "r", sc & "s", sc & "t", sc & "u", sc & "v"}
end getNUMBERS
Script for sending commands to the Arduino:
(*
Arduino IR Commands for 22LG3000
*)
--set sc to "/Library/Scripts/arduino-serial-master/arduino-serial -b 9600 -p /dev/tty.usbmodem621 -s "
on sendMUTE(sc)
do shell script sc & "a"
end sendMUTE
on sendVOLUP(sc)
do shell script sc & "b"
end sendVOLUP
on sendVOLDWN(sc)
do shell script sc & "c"
end sendVOLDWN
on sendAUP(sc)
do shell script sc & "d"
end sendAUP
on sendADWN(sc)
do shell script sc & "e"
end sendADWN
on sendALFT(sc)
do shell script sc & "f"
end sendALFT
on sendARGT(sc)
do shell script sc & "g"
end sendARGT
on sendSELECT(sc)
do shell script sc & "h"
end sendSELECT
on sendMENU(sc)
do shell script sc & "i"
end sendMENU
on sendRETURN(sc)
do shell script sc & "j"
end sendRETURN
on sendEXIT(sc)
do shell script sc & "k"
end sendEXIT
on sendONOFF(sc)
do shell script sc & "l"
end sendONOFF
on send1(sc)
do shell script sc & "m"
end send1
on send2(sc)
do shell script sc & "n"
end send2
on send3(sc)
do shell script sc & "o"
end send3
on send4(sc)
do shell script sc & "p"
end send4
on send5(sc)
do shell script sc & "q"
end send5
on send6(sc)
do shell script sc & "r"
end send6
on send7(sc)
do shell script sc & "s"
end send7
on send8(sc)
do shell script sc & "t"
end send8
on send9(sc)
do shell script sc & "u"
end send9
on send0(sc)
do shell script sc & "v"
end send0
on sendSLEEP(sc)
do shell script sc & "w"
end sendSLEEP
on sendGET2WAKE(sc)
do shell script sc & "x"
end sendGET2WAKE
on getNUMBERS(sc, x)
return item x of {sc & "m", sc & "n", sc & "o", sc & "p", sc & "q", sc & "r", sc & "s", sc & "t", sc & "u", sc & "v"}
end getNUMBERS
on sendWAKE2SLEEP(sc)
do shell script sc & "y"
end sendWAKE2SLEEP
on sendCANCELSLEEP(sc)
do shell script sc & "z"
end sendCANCELSLEEP
And the Arduino sketch which actually sends the IR commands (using the IRremote library by Ken Shirriff https://github.com/shirriff/Arduino-IRremote)
// Control 22LG3000
// REV2
#include <IRremote.h>
IRsend irsend;
int incomingByte = 0; // for incoming serial data
void setup()
{
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
Serial.println(incomingByte);
if(incomingByte == 97){
sendMUTE();
}else if(incomingByte == 48){
}else if(incomingByte == 98){
sendVOLUP();
}else if(incomingByte == 99){
sendVOLDWN();
}else if(incomingByte == 100){
sendAUP();
}else if(incomingByte == 101){
sendADWN();
}else if(incomingByte == 102){
sendALFT();
}else if(incomingByte == 103){
sendARGT();
}else if(incomingByte == 104){
sendSELECT();
}else if(incomingByte == 105){
sendMENU();
}else if(incomingByte == 106){
sendRETURN();
}else if(incomingByte == 107){
sendEXIT();
}else if(incomingByte == 108){
sendONOFF();
}else if(incomingByte == 109){
send1();
}else if(incomingByte == 110){
send2();
}else if(incomingByte == 111){
send3();
}else if(incomingByte == 112){
send4();
}else if(incomingByte == 113){
send5();
}else if(incomingByte == 114){
send6();
}else if(incomingByte == 115){
send7();
}else if(incomingByte == 116){
send8();
}else if(incomingByte == 117){
send9();
}else if(incomingByte == 118){
send0();
}else if(incomingByte == 119){
sendSLEEP();
}else if(incomingByte == 120){
sendGET2WAKE();
}else if(incomingByte == 121){
sendWAKE2SLEEP();
}else if(incomingByte == 122){
sendCANCELSLEEP();
}
}
}
void sendMUTE() {
irsend.sendNEC(0x20df906f, 32);
delay(4*100);
}
void sendVOLUP() {
irsend.sendNEC(0x20df40bf, 32);
delay(3*100);
irsend.sendNEC(0x20df40bf, 32);
}
void sendVOLDWN() {
irsend.sendNEC(0x20dfc03f, 32);
delay(3*100);
irsend.sendNEC(0x20dfc03f, 32);
}
void sendAUP() {
irsend.sendNEC(0x20df01fd, 32);
delay(5*100);
}
void sendADWN() {
irsend.sendNEC(0x20df827d, 32);
delay(5*100);
}
void sendALFT() {
irsend.sendNEC(0x20dfe01f, 32);
delay(5*100);
}
void sendARGT() {
irsend.sendNEC(0x20df609f, 32);
delay(5*100);
}
void sendSELECT() {
irsend.sendNEC(0x20df22dd, 32);
delay(5*100);
}
void sendMENU() {
irsend.sendNEC(0x20dfc23d, 32);
delay(5*100);
}
void sendRETURN() {
irsend.sendNEC(0x20df14eb, 32);
delay(5*100);
}
void sendEXIT() {
irsend.sendNEC(0x20dfda25, 32);
delay(5*100);
}
void sendONOFF() {
irsend.sendNEC(0x20df10ef, 32);
delay(10*100);
}
void send1() {
irsend.sendNEC(0x20df8877, 32);
delay(10*100);
}
void send2() {
irsend.sendNEC(0x20df48b7, 32);
delay(10*100);
}
void send3() {
irsend.sendNEC(0x20dfc837, 32);
delay(10*100);
}
void send4() {
irsend.sendNEC(0x20df28d7, 32);
delay(10*100);
}
void send5() {
irsend.sendNEC(0x20dfa857, 32);
delay(10*100);
}
void send6() {
irsend.sendNEC(0x20df6897, 32);
delay(10*100);
}
void send7() {
irsend.sendNEC(0x20dfe817, 32);
delay(10*100);
}
void send8() {
irsend.sendNEC(0x20df18e7, 32);
delay(10*100);
}
void send9() {
irsend.sendNEC(0x20df9867, 32);
delay(10*100);
}
void send0() {
irsend.sendNEC(0x20df08f7, 32);
delay(10*100);
}
void sendGET2WAKE() {
sendMENU();
sendADWN();
sendADWN();
sendSELECT();
sendADWN();
sendADWN();
sendSELECT();
sendADWN();
sendSELECT();
sendADWN();
sendADWN();
sendADWN();
sendADWN();
}
//void getWAKEtime();
//
// if
//
//}
void sendSLEEP() {
sendMENU();
sendADWN();
sendADWN();
sendSELECT();
sendADWN();
sendADWN();
sendSELECT();
sendADWN();
sendADWN();
sendALFT();
sendEXIT();
delay (10*100);
}
void sendWAKE2SLEEP() {
sendRETURN();
sendADWN();
sendALFT();
sendEXIT();
sendEXIT();
}
void sendCANCELSLEEP() {
sendMENU();
sendADWN();
sendADWN();
sendSELECT();
sendADWN();
sendADWN();
sendSELECT();
sendADWN();
sendADWN();
sendARGT();
sendEXIT();
sendEXIT();
}
As an example of using this set up, you can use Scenario (AppStore https://itunes.apple.com/gb/app/scenario/id423444193?mt=12) to run a script on sleep. This is very helpful because the TV (which I use as one of my monitors) doesn’t switch off when I put the machine to sleep. Often, pressing the on/off button on the front of the TV doesn’t actually do anything, and I often lose the remote.
So, on sleep, Scenario runs this script:
set commander to load script "/Library/Scripts/ArduinoIRCommands.scpt"
set controlArduino to "/Library/Scripts/arduino-serial-master/arduino-serial -b 9600 -p /dev/tty.usbmodem621 -s "
commander's sendONOFF(controlArduino)
error "MCScenarioReadyToGo"
. . . and again on wake, only without the error as it isn’t needed.
Using BetterTouchTool I can set the F18 and F19 keys to control the volume on the TV, and CMD+F15 to run the script at the top of the post.
It’s a very useful setup.