Over the last week of my robotics course, I have been trying to fix the errors with the code from the previous week, and make the servo motor controlled by an infra-red remote move while a button is held. I started off trying to understand the signals being printed into the terminal, and seeing if there was a way to detect when a button is released, but after researching and a couple failed attempts I figured that this solution would be very challenging and that I’d rather search for a different one. The code I had last time was nearly working, however there were random signals being input no matter what buttons I pressed, this meant that I couldn’t read for the proper signals since, as far as I could tell, they were completely unrelated to the button being pressed with no patterns whatsoever.

I played around with the code I had, just so I could see happened to the state of the program when one of these random signals appeared. As expected, the state was put into idle so that the button had to be released then repressed to activate again. However after looking at all this, I found that no matter which button is pressed it always showed the correct signal before the random ones. This meant that I could make it read for the initial input to decide which state it should be in, then if it receives any input I can make it continue turning until no input is detected. Once this was programmed in, the servo was able to turn multiple times if the button was held, however it still randomly returned to being idle.

What was happening, as explained to me, was that the receiver is always reading for input, but my code checks if the remote is outputting signals every 10ms, which meant that there were times where my code missed an input and so it would decide that the button was no longer being held. After playing around with ideas that didn’t work, and not finding anything helpful online, my teacher suggested using a kind of clock. This meant that I would start a clock when the button is first starting to be held, then the code would run the same, except instead of checking for a new signal every 10ms, I would check within the last 200ms, if the 10ms cycle had received an input. The only downside to this is if I were to let go of the button, there is anywhere up to 200ms delay.

The only issue that was remaining at this point was a very minor one, with a very simple fix. The problem was that the servo could only rotate 180º, while the counter didn’t have a limit, meaning if it exceeded the parameters then it would take extra time next time its pressed to get to 0 or 180 before it could move again. The solution I implemented was to change any value greater than 180 back to 180, and any value lower than 0 back to 0.