This week I started a new project that wasn’t apart of the Arduino Tutorial booklet. This project used the concepts I had learnt about in the previous few projects, such as IR, and Servo motors. I put these two main ideas together and attempted to make a servo motor that would respond to button inputs on an IR controller. Having played around with IR receivers in the last project, I was already comfortable with which wires needed to be put into which pin, and then how to connect that to something else, this time a servo.
My issue came when I tried to program it. At first, there weren’t many issues, I just needed to get familiar with the IR remote library. But after that, I got the basic setup done quickly. The logic part if this wasn’t extremely complicated, there just some initial steps I was unsure how to approach. The first issue was that I needed the value that the buttons I wanted to use on the remote gave the computer. Luckily, after some googling, I found it was an easy print statement and opening up the terminal window. After that I was able to use a basic if/else statement to move the servo depending on which button is pressed.
This code works for what it’s intended to do, however it doesn’t allow you to hold a button to make the servo continuously rotate. I wanted to make this happen, but I wasn’t sure how to. When I was looking at the terminal, I found that when you hold a button, it will show you the decoded value, followed by -1’s for as long as it is held. This made it hard because I can’t check for -1, as it will appear no matter which direction I wanted it to go. The solution to this was to make states in the code, this way it can look for the initial number, then as long as there are -1’s it will continue to turn.
This code worked to an extent. It allowed for the machine to remember what state it was previously in, then based on that it would turn either left or right. But sometimes it turned a couple times and then just stopped. What was happening, as explained to me, was that the delay for the computer to send a signal to the Arduino to move sometimes didn’t aline with the delay for checking input, by fractions of a second, meaning the code would switch the state back to idle while the button was being held, and therefore couldn’t continue turning. I tried to fix this by extending the delay, but I got a different problem, the values that the receiver was picking up were random. They weren’t any of the ones I had tested earlier and so my assumption is that it was picking up signals from other devices.