If statements too slow...

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

If statements too slow...

01 Apr 2019, 03:39

Okay so i have an arduino with some buttons on it. it sends different ASCII letters over 115200 baud serial so no issues there. It sends like this: if button1 is pressed > send 'a' else if button1 is not pushed > send 'A' but for more buttons than just one. All of this works fine but im using autohotkey to read serial from the arduino and then hold the keys.

Code: Select all

while true {
input:=arduino_read()
if (input = "610D0A")
 Send {a down}
else if  (input = "410D0A")
 Send {a up}
else if (input = "620D0A")
 Send {s down}
else if  (input = "420D0A")
 Send {s up}
else if (input = "630D0A")
 Send {d down}
else if  (input = "430D0A")
 Send {d up}
else if (input = "640D0A")
 Send {f down}
else if  (input = "440D0A")
 Send {f up}
else if (input = "650D0A")
 Send {g down}
else if  (input = "450D0A")
 Send {g up}
else if (input = "660D0A")
 Send {Up down}
else if  (input = "460D0A")
 Send {Up up}
else if (input = "670D0A")
 Send {Down down}
else if  (input = "470D0A")
 Send {Down up}
else if (input = "690D0A")
 Send {h down}
else if  (input = "490D0A")
 Send {h up}
else if (input = "680D0A")
 Send {Enter down}
else if  (input = "480D0A")
 Send {Enter up}
}
and this works well until you try to press more than one button at once. is there anything i can use to test more than one button at a time? or just to speed up my if else's
just me
Posts: 9487
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: If statements too slow...

01 Apr 2019, 03:49

What do you get from arduino_read() if you press more than one button?
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

Re: If statements too slow...

01 Apr 2019, 04:06

it just reads the serial input so if i press more than one for example A and D it would look something like

Code: Select all

adAD
or

Code: Select all

adDA
the lowercase is the push and the uppercase is the release. it reads in one ASCII character at a time. Thats what the weird numbers im checking are.
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: If statements too slow...

01 Apr 2019, 09:01

Code: Select all

keys :=	{"610D0A":"{a down}","410D0A":"{a up}"
	,"620D0A":"{s down}","420D0A":"{s up}"
	,"630D0A":"{d down}","430D0A":"{d up}"
	,"640D0A":"{f down}","440D0A":"{f up}"
	,"650D0A":"{g down}","450D0A":"{g up}"
	,"660D0A":"{UP down}","460D0A":"{UP up}"
	,"670D0A":"{DOWN down}","470D0A":"{DOWN up}"
	,"690D0A":"{h down}","490D0A":"{h up}"
	,"680D0A":"{ENTER down}","480D0A":"{ENTER up}"}

while	true
	Send %	keys[arduino_read()]
:?:
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

Re: If statements too slow...

01 Apr 2019, 09:30

Not at home right now but this looks great. Will test later.
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

Re: If statements too slow...

01 Apr 2019, 11:31

okay well. It seems to be working just the same.
I feel as if i need to explain better.but alas...

it works for single buttons. no problems if i press them one at a time. but for this i need to be able to press more than one button at the same time. if i do that with either code it gets stuck held down and i have to re-press it. i know its not a problem with my arduino code or wiring because ive tested that extensively with a serial monitor.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: If statements too slow...

01 Apr 2019, 11:43

Send % keys[arduino_read()] this presses AND RELEASES the key.
It is therefore impossible to do multikey combos with this code (Because you cannot hold any of the keys coming from the arduino)
Your arduino code needs to send press and release events, not just press events
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

Re: If statements too slow...

01 Apr 2019, 11:49

It does send release events. Thats what the capital letter is. lets say i hold button a. the arduino sees this state change and sends 'a' over serial. but if the state changes from held to released then it sends 'A'. example serial output below: this is me messing with 3 buttons. again lowercase is push uppercase is release

Code: Select all

12:48:26.881 -> a
12:48:27.185 -> A
12:48:27.524 -> a
12:48:27.830 -> A
12:48:28.236 -> b
12:48:28.540 -> B
12:48:28.913 -> b
12:48:29.285 -> B
12:48:29.691 -> c
12:48:29.995 -> C
12:48:30.368 -> c
12:48:30.674 -> C
12:48:31.218 -> c
12:48:31.592 -> b
12:48:31.998 -> a
12:48:32.472 -> C
12:48:32.778 -> B
12:48:33.118 -> A
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: If statements too slow...

01 Apr 2019, 13:20

In order to debug what is happening, I would recommend some form of logging.
Tooltip or msgbox are maybe not the best tool for the job in this case, I would maybe recommend outputting to a debug log.
If you use SciTE or AHKStudio, those should have a debug stream viewer included, else you can use the Microsoft tool DebugView

Code: Select all

while (true){
	ard := arduino_read()
	out := keys[ard]
	OutputDebug % "AHK| Seen: " ard ", Sending: " out
	Send % out
}
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: If statements too slow...

01 Apr 2019, 17:01

Have you experimented with different Send modes? Try putting SendMode Input at the top. If that doesn't help, remove it and replace it with SetKeyDelay, 50, 50. If the problem goes away, reduce those numbers (down to -1) to see how low you can go before the problem returns.
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

Re: If statements too slow...

02 Apr 2019, 04:42

oops/ sorry for the late reply. its already in sendmode input. i will try with some other forms of that. also will try logging.
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

Re: If statements too slow...

02 Apr 2019, 05:03

omfg i got it guys i know the problem but i have no clue how to fix it
it sends data SERIALLY so it sends keys at the SAME TIME so it looks like this when i press 2 or more keys

Code: Select all

AHK| Seen: , Sending: 
AHK| Seen: , Sending: 
AHK| Seen: 410D0A420D0A, Sending: 
AHK| Seen: , Sending: 
AHK| Seen: , Sending: 
i need to run the input of ardinoread through some sort of filter that splits it into 3 bytes at a time
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

Re: If statements too slow...

02 Apr 2019, 05:10

so i was going to use strsplit to make an array of strings that i could Send but i cant figure out how to loop over an array in ahk...
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: If statements too slow...

02 Apr 2019, 05:10

Code: Select all

ard := strsplit(ard, "doa")
if ard.count()
  for each, 3bits in ard
    if keys.haskey(3bits)
      send % keys[3bits]
and change keys to not include the "doa" suffix. it looks like its being repeated throughout
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

Re: If statements too slow...

02 Apr 2019, 05:23

is there any way i can just convert the hex back into ascii?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: If statements too slow...

02 Apr 2019, 05:36

where in all of this did u spot any semblance of ascii
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

Re: If statements too slow...

02 Apr 2019, 05:37

the bytes that are being sent over serial its ascii

eg:

its sending the letter then a carriage return then a line feed
brandon4414
Posts: 15
Joined: 01 Apr 2019, 03:29

Re: If statements too slow...

02 Apr 2019, 05:38

http www.asciitable.com / Broken Link for safety see every byte corresponds to a letter on the table

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: GEOVAN, ShatterCoder and 230 guests