click() event on an element that already has click behavior; anchor tags, submit-type buttons, etc. Usually, it’s simple enough to handle; a call through to
event.stopPropagation(), or even just returning
false from the event can hamstring the default behavior of a clickable control. Other times, it is possible to get away with just ignoring the problem because the 2nd click has no impact on the site (though this is far from good practice).
Today, none of the above were of any use to me, and I found myself a little dumbfounded with little to no help from the Internet. I’m working on a little Arduino project with some guys from the lab, manipulating an Arduino on a network via a RESTful API I’m coding up in Ruby using the Sinatra platform. The current interface is done up on a website running on my local network. Currently, we’re controlling LED’s by using Pulse-Width Modulation to tune their brightness. I’m using AJAX requests to submit PWM values entered in to input fields on the locally hosted site.
Everything was working wonderfully yesterday, but today we added multiple LED’s to the board and I began seeing some strange behavior. Here’s the rundown:
The Arduino board operates by pulling bytes off the serial port, so when the Sinatra app sends out a command string we buffer this serial input in to a character array and then tokenize it. We then have to route the command to the appropriate digital IO pin. Our protocol is a simple delimited string indicating a pin and a PWM value; so sending a command of 3000 to pin 9 would look something like ‘9,3000’. We then use the built-in map function to compute a PWM byte and send that to the A/D converter. But we were getting garbage. Negative numbers, weird bits. We realized that we weren’t accounting for a delay in latency from the time the bits started coming down off the ether and in to the serial buffer. So we added a healthy delay in between when the Serial buffer became readable and when we actually started reading. From here, we were able to write data directly to the Arduino IDE Serial Monitor and get consistently correct information back. But the REST API was still bugging out. The one thing I did notice was that I wasn’t getting garbage anymore; I was getting wrong information that followed a pattern.
strtok() functions from the C library with a comma as my delimiter, pin 9 would receive a value of 30009 instead of 3000.
$(document).onReady() event firing off a 2nd time after content generation, leading to my event handlers getting bound twice. If this isn’t the reason that the event handlers were getting bound twice, I would love to know the real reason, so please drop me a line. But until then, I will assume that this is the cause.
The solution to this was rather simple. Instead of using the pattern
$(selector).click(…), I used the following pattern:
$(selector).unbind('click').click(…). This insures that the handler is bounde no more than once. Our problems were solved, and we were able to execute commands with run-time pin selection on the Arduino board.