Hardware components | ||||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
| × | 8 | ||||
![]() |
| × | 7 | |||
| × | 1 | ||||
![]() |
| × | 2 | |||
![]() |
| × | 2 | |||
| × | 1 | ||||
![]() |
| × | 1 | |||
![]() |
| × | 1 |
My first Arduino Project is a remote-controlled car. I wired all the components, wrote all the code (with the help of a library for the IR reciever), and assembled the chassis by myself.
I only recently started learning Arduino, using a Super Starter Kit. This is my first independent project.
Here are some pictures of it:
Here is a video of me showing it in action:
I bought this chassis kit from amazon, which included the necessary motors. You can use your own chassis and/or motors if you want. I used some old 9V batteries to power everything (you also need 5 mm x 2.1 mm battery clips for the 9V batteries, so you can plug it into the motherboard and PSM). The wires, shield (which is applied onto my motherboard's ports), mini breadboard, IR reciever module, and the rest of the components came with an Arduino Super Starter Kit. I used a shield so that I could use my motherboard elsewhere, and simply attach it to my motherboard and plug in power to start using it. If your motors do not have a jumper cable attached (like mine), then you can cut off part of a jumper cable, remove a small section of the insulation, and wrap/solder the exposed wire to the terminals of the motor.
A summary of the whole process creating this is as follows:
1. Gather all the necessary components for this project, which are the ones for the chassis, the ones for the circuitry, and a computer with the Arduino IDE and the IR reciever library installed.
2. Wire all the components together (guide is above).
3. Upload the code to the motherboard, and test whether the code works with the hardware. If it works, then continue. If not, then troubleshoot it or comment your issues.
4. Assemble the chassis and secure all the components to it. You are ready to go!
Here is a wiring guide for it (this was designed for a 2 motor chassis only):
Once you have finshed the steps of the wiring guide, don't forget to secure everything reasonably well in the chassis and connect the pair of each motors' pins to the third and third-to-last (sixth) pins on each side of the L293D chip through the breadboard.
The controls for the RC car are toggle-based, because I was not able to incorporate normal controls without bugs. If anyone would like to suggest how to make the controls more intuitive, then please comment your suggestions.
Once the motherboard is plugged into power and the PSM for the motors is turned on, the machine will be in the "off" state. It will still consume power, but the controls will not be usable. Press the Power button to turn it "on". Then, you can use the all the controls, which are Forward, Backward, Turn Right, Turn Left, and Stop. Press the power button again to turn it "off". If you are not using the RC car, remember to unplug the motherboard from power and turn off the PSM so as not to waste battery.
The Forward control should make both motors spin forward. The Backward control should make both motors spin backwards. The Turn Right control should make the left motor spin forward, and the right motor should turn off. The Turn Left control should make the right motor spin forward, and the left motor should turn off. The Stop (Pause) control should make both motors stop moving. If the motors do not spin in the right direction, switch around the terminals of the batteries. (i.e. switch their sides on the L293D chip, flip positive and negative terminals of the battery)
I believe this is a great starter project for Arduino, as it exposed me to the intricacies of Arduino projects, depsite not having a practical purpose. I had to plan my process to create this from scratch, do much more complex wiring then I'm used to, I had to write my own code, and I had to assemble a chassis for it.
Thank you for checking this out, and good luck with any of your future projects.
PS: The software section isn't completely accurate. It wouldn't save when I tried to edit it. The development app that should be used is the Arduino IDE. The Circuit Diagram (in the Code section), IRremote.h, and IRremote.cpp files are unnecessary. ThIs is the library for the IR reciever. Download it, unzip it, and include the library in the Arduino IDE (Sketch > Include Library > Add.ZIP Library).
RC Car - Circuit Diagram

RC Car - Circuit Diagram
SVG<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Circuit Diagram, cdlibrary.dll 4.0.0.0 -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" width="640" height="520" xmlns="http://www.w3.org/2000/svg">
<line x1="500" y1="210" x2="600" y2="210" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="500" y1="150" x2="600" y2="150" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="330" y1="210" x2="390" y2="210" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="360" y1="200" x2="380" y2="200" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="330" y1="210" x2="330" y2="490" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="40" y1="490" x2="330" y2="490" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="330" y1="150" x2="390" y2="150" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="330" y1="20" x2="330" y2="150" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="40" y1="20" x2="330" y2="20" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="600" y1="150" x2="600" y2="160" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="600" y1="200" x2="600" y2="210" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="40" y1="200" x2="40" y2="490" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="40" y1="20" x2="40" y2="160" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="40" y1="160" x2="40" y2="167" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="40" y1="193" x2="40" y2="200" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<ellipse cx="40" cy="180" rx="12" ry="12" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-width:2" />
<text x="40" y="180" style="font-family:Arial;font-size:12px;text-anchor:middle" dominant-baseline="middle" transform="rotate(0, 40, 180)">M</text>
<line x1="600" y1="160" x2="600" y2="167" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="600" y1="193" x2="600" y2="200" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<ellipse cx="600" cy="180" rx="12" ry="12" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-width:2" />
<text x="600" y="180" style="font-family:Arial;font-size:12px;text-anchor:middle" dominant-baseline="middle" transform="rotate(0, 600, 180)">M</text>
<line x1="500" y1="110" x2="550" y2="110" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="550" y1="110" x2="550" y2="470" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="390" y1="470" x2="550" y2="470" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="390" y1="430" x2="390" y2="470" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="370" y1="170" x2="390" y2="170" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="370" y1="330" x2="490" y2="330" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="490" y1="330" x2="490" y2="430" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="370" y1="170" x2="370" y2="330" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="360" y1="250" x2="380" y2="250" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="360" y1="250" x2="360" y2="430" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="500" y1="170" x2="530" y2="170" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="530" y1="170" x2="530" y2="430" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="360" y1="430" x2="430" y2="430" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="450" y1="430" x2="530" y2="430" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="430" y1="430" x2="424" y2="430" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<ellipse cx="440" cy="430" rx="16" ry="16" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-width:2" />
<line x1="456" y1="430" x2="450" y2="430" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<path d="M 440,430 M 430,430 L 438,430 M 434,426 L 434,434 M 444,430 L 452,430" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="500" y1="130" x2="530" y2="130" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="530" y1="50" x2="530" y2="130" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="500" y1="230" x2="520" y2="230" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="520" y1="60" x2="520" y2="230" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="500" y1="250" x2="510" y2="250" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="510" y1="70" x2="510" y2="250" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="310" y1="70" x2="510" y2="70" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="300" y1="60" x2="520" y2="60" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="290" y1="50" x2="530" y2="50" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="290" y1="50" x2="290" y2="150" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="300" y1="60" x2="300" y2="160" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="310" y1="70" x2="310" y2="170" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="110" y1="200" x2="140" y2="200" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="90" y1="190" x2="140" y2="190" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="110" y1="200" x2="110" y2="450" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="90" y1="190" x2="90" y2="470" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="90" y1="470" x2="270" y2="470" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="110" y1="450" x2="250" y2="450" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="250" y1="410" x2="250" y2="450" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="270" y1="380" x2="270" y2="470" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="250" y1="380" x2="270" y2="380" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="280" y1="140" x2="350" y2="140" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="350" y1="370" x2="350" y2="375" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="350" y1="415" x2="350" y2="420" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<path d="M 350,375 L 350,377 L 343,380 L 357,386 L 343,392 L 357,398 L 343,404 L 357,410 L 350,413 L 350,415" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<text x="336" y="395" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 336, 395)">220 </text>
<line x1="350" y1="450" x2="350" y2="470" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="300" y1="470" x2="350" y2="470" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="250" y1="350" x2="310" y2="350" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="310" y1="190" x2="310" y2="350" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="300" y1="220" x2="300" y2="470" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="350" y1="140" x2="350" y2="370" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="320" y1="130" x2="380" y2="130" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="320" y1="130" x2="320" y2="240" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="290" y1="230" x2="380" y2="230" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="290" y1="230" x2="290" y2="250" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="340" y1="110" x2="380" y2="110" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="340" y1="110" x2="340" y2="260" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="350" y1="450" x2="350" y2="420" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<path d="M 350,428 M 342,428 L 358,428 M 350,428 L 358,443 L 342,443 L 350,428" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<path d="M 350,430 M 361,432 L 369,424 M 370,423 L 368,427 L 366,425 L 370,423 L 368,427" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<path d="M 350,430 M 354,426 L 362,418 M 363,417 L 361,421 L 359,419 L 363,417 L 361,421" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="270" y1="150" x2="290" y2="150" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="270" y1="140" x2="280" y2="140" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="270" y1="160" x2="300" y2="160" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="280" y1="170" x2="290" y2="170" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="270" y1="170" x2="310" y2="170" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="270" y1="190" x2="310" y2="190" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="270" y1="220" x2="300" y2="220" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="270" y1="240" x2="320" y2="240" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<rect x="180" y="330" width="40" height="100" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-width:2" />
<line x1="220" y1="410" x2="250" y2="410" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="220" y1="380" x2="250" y2="380" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="220" y1="350" x2="250" y2="350" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<text x="200" y="410" style="font-family:Arial;font-size:11px;text-anchor:middle" dominant-baseline="middle" transform="rotate(0, 200, 410)">GND</text>
<text x="200" y="380" style="font-family:Arial;font-size:11px;text-anchor:middle" dominant-baseline="middle" transform="rotate(0, 200, 380)">VCC</text>
<text x="200" y="350" style="font-family:Arial;font-size:11px;text-anchor:middle" dominant-baseline="middle" transform="rotate(0, 200, 350)">DATA</text>
<path d="M 200,380 M 180,410 A 30,30 0 1 1 180,350" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<path d="M 150,380 M 146,342 L 154,350 M 155,351 L 153,347 L 151,349 L 155,351 L 153,347" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<path d="M 150,380 M 140,348 L 148,356 M 149,357 L 147,353 L 145,355 L 149,357 L 147,353" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="270" y1="250" x2="290" y2="250" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="270" y1="260" x2="340" y2="260" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<rect x="390" y="100" width="100" height="200" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-width:2" />
<line x1="380" y1="110" x2="390" y2="110" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="380" y1="130" x2="390" y2="130" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="380" y1="150" x2="390" y2="150" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="380" y1="170" x2="390" y2="170" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="380" y1="190" x2="390" y2="190" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="380" y1="210" x2="390" y2="210" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="380" y1="230" x2="390" y2="230" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="380" y1="250" x2="390" y2="250" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="380" y1="270" x2="390" y2="270" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="380" y1="290" x2="390" y2="290" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="490" y1="110" x2="500" y2="110" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="490" y1="130" x2="500" y2="130" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="490" y1="150" x2="500" y2="150" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="490" y1="170" x2="500" y2="170" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="490" y1="190" x2="500" y2="190" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="490" y1="210" x2="500" y2="210" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="490" y1="230" x2="500" y2="230" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="490" y1="250" x2="500" y2="250" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<text x="394" y="110" style="font-family:Arial;font-size:12px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 394, 110)">I<tspan baseline-shift="sub" style="font-size:0.8em">7</tspan></text>
<text x="394" y="130" style="font-family:Arial;font-size:12px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 394, 130)">I<tspan baseline-shift="sub" style="font-size:0.8em">6</tspan></text>
<text x="394" y="150" style="font-family:Arial;font-size:12px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 394, 150)">I<tspan baseline-shift="sub" style="font-size:0.8em">5</tspan></text>
<text x="394" y="170" style="font-family:Arial;font-size:12px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 394, 170)">I<tspan baseline-shift="sub" style="font-size:0.8em">4</tspan></text>
<text x="394" y="190" style="font-family:Arial;font-size:12px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 394, 190)">I<tspan baseline-shift="sub" style="font-size:0.8em">3</tspan></text>
<text x="394" y="210" style="font-family:Arial;font-size:12px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 394, 210)">I<tspan baseline-shift="sub" style="font-size:0.8em">2</tspan></text>
<text x="394" y="230" style="font-family:Arial;font-size:12px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 394, 230)">I<tspan baseline-shift="sub" style="font-size:0.8em">1</tspan></text>
<text x="394" y="250" style="font-family:Arial;font-size:12px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 394, 250)">I<tspan baseline-shift="sub" style="font-size:0.8em">0</tspan></text>
<text x="394" y="270" style="font-family:Arial;font-size:12px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 394, 270)">adc</text>
<text x="394" y="290" style="font-family:Arial;font-size:12px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 394, 290)">R</text>
<text x="486" y="110" style="font-family:Arial;font-size:12px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 486, 110)">Q<tspan baseline-shift="sub" style="font-size:0.8em">7</tspan></text>
<text x="486" y="130" style="font-family:Arial;font-size:12px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 486, 130)">Q<tspan baseline-shift="sub" style="font-size:0.8em">6</tspan></text>
<text x="486" y="150" style="font-family:Arial;font-size:12px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 486, 150)">Q<tspan baseline-shift="sub" style="font-size:0.8em">5</tspan></text>
<text x="486" y="170" style="font-family:Arial;font-size:12px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 486, 170)">Q<tspan baseline-shift="sub" style="font-size:0.8em">4</tspan></text>
<text x="486" y="190" style="font-family:Arial;font-size:12px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 486, 190)">Q<tspan baseline-shift="sub" style="font-size:0.8em">3</tspan></text>
<text x="486" y="210" style="font-family:Arial;font-size:12px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 486, 210)">Q<tspan baseline-shift="sub" style="font-size:0.8em">2</tspan></text>
<text x="486" y="230" style="font-family:Arial;font-size:12px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 486, 230)">Q<tspan baseline-shift="sub" style="font-size:0.8em">1</tspan></text>
<text x="486" y="250" style="font-family:Arial;font-size:12px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 486, 250)">Q<tspan baseline-shift="sub" style="font-size:0.8em">0</tspan></text>
<line x1="394" y1="282.5" x2="402" y2="282.5" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:1" />
<text x="486" y="290" style="font-family:Arial;font-size:12px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 486, 290)">Controller</text>
<rect x="145" y="105" width="120" height="195" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-width:2" />
<text x="181" y="135" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 181, 135)">ARDUINO</text>
<text x="188" y="145" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 188, 145)">UNO r3</text>
<line x1="140" y1="160" x2="145" y2="160" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="170" x2="145" y2="170" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="180" x2="145" y2="180" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="190" x2="145" y2="190" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="200" x2="145" y2="200" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="210" x2="145" y2="210" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="220" x2="145" y2="220" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="240" x2="145" y2="240" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="250" x2="145" y2="250" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="260" x2="145" y2="260" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="270" x2="145" y2="270" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="280" x2="145" y2="280" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="140" y1="290" x2="145" y2="290" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="130" x2="270" y2="130" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="140" x2="270" y2="140" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="150" x2="270" y2="150" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="160" x2="270" y2="160" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="170" x2="270" y2="170" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="180" x2="270" y2="180" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="190" x2="270" y2="190" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="200" x2="270" y2="200" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="220" x2="270" y2="220" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="230" x2="270" y2="230" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="240" x2="270" y2="240" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="250" x2="270" y2="250" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="260" x2="270" y2="260" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="270" x2="270" y2="270" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="280" x2="270" y2="280" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="265" y1="290" x2="270" y2="290" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<text x="149" y="160" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 160)">IOREF</text>
<text x="149" y="170" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 170)">RESET</text>
<text x="149" y="180" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 180)">3.3V</text>
<text x="149" y="190" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 190)">5V</text>
<text x="149" y="200" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 200)">GND</text>
<text x="149" y="210" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 210)">GND</text>
<text x="149" y="220" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 220)">Vin</text>
<text x="149" y="240" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 240)">A0</text>
<text x="149" y="250" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 250)">A1</text>
<text x="149" y="260" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 260)">A2</text>
<text x="149" y="270" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 270)">A3</text>
<text x="149" y="280" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 280)">A4</text>
<text x="149" y="290" style="font-family:Arial;font-size:11px;text-anchor:start" dominant-baseline="middle" transform="rotate(0, 149, 290)">A5</text>
<text x="261" y="130" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 130)">AREF</text>
<text x="261" y="140" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 140)">GND</text>
<text x="261" y="150" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 150)">D13</text>
<text x="261" y="160" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 160)">D12</text>
<text x="261" y="170" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 170)">PWM D11</text>
<text x="261" y="180" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 180)">PWM D10</text>
<text x="261" y="190" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 190)">PWM D9</text>
<text x="261" y="200" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 200)">D8</text>
<text x="261" y="220" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 220)">D7</text>
<text x="261" y="230" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 230)">PWM D6</text>
<text x="261" y="240" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 240)">PWM D5</text>
<text x="261" y="250" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 250)">D4</text>
<text x="261" y="260" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 260)">PWM D3</text>
<text x="261" y="270" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 270)">D2</text>
<text x="261" y="280" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 280)">TX D1</text>
<text x="261" y="290" style="font-family:Arial;font-size:11px;text-anchor:end" dominant-baseline="middle" transform="rotate(0, 261, 290)">RX D0</text>
<path d="M 205,120 M 205,120 c 2,9 15,9 15,0 c 0,-9 -13,-9 -15,0" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<path d="M 205,120 M 205,120 c -2,-9 -15,-9 -15,0 c 0,9 13,9 15,0" style="fill-opacity:0;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="211" y1="120" x2="215" y2="120" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="213" y1="122" x2="213" y2="118" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<line x1="199" y1="120" x2="195" y2="120" style="stroke:rgb(0, 0, 0);stroke-linecap:square;stroke-width:2" />
<ellipse cx="390" cy="430" rx="2" ry="2" style="fill-opacity:1;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-width:2" />
<ellipse cx="490" cy="430" rx="2" ry="2" style="fill-opacity:1;fill:rgb(0, 0, 0);stroke:rgb(0, 0, 0);stroke-width:2" />
</svg>
RC Car - Code
C/C++#include "IRremote.h"
int receiver = 9; // Signal Pin of IR receiver to Arduino Digital Pin 11
#define led 7 // Power LED
#define m1_pwm 3
#define m1_A 4
#define m1_B 5
#define m2_pwm 11
#define m2_A 12
#define m2_B 13
int power = 0; // controls except for toggling power can only be used when "on" (power = 1)
/*-----( Declare objects )-----*/
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
/*-----( Function )-----*/
void translateIR() // takes action based on IR code received
// describing Remote IR codes
{
switch(results.value)
{
case 0xFFA25D: // Power
Serial.println("POWER");
power += 1;
power = power % 2;
Serial.println(power);
break;
case 0xFFE21D:
Serial.println("FUNC/STOP");
Serial.println("f1");
break;
case 0xFF629D: // Forward
Serial.println("VOL+");
digitalWrite(m1_A, HIGH);
digitalWrite(m1_B, LOW);
digitalWrite(m2_A, HIGH);
digitalWrite(m2_B, LOW);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, HIGH);
break;
case 0xFF22DD: // Turn Left
Serial.println("FAST BACK");
digitalWrite(m2_A, LOW);
digitalWrite(m2_B, HIGH);
digitalWrite(m1_A, HIGH);
digitalWrite(m1_B, LOW);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, LOW);
break;
case 0xFF02FD: // Stop/Pause
Serial.println("PAUSE");
digitalWrite(m1_pwm, LOW);
digitalWrite(m2_pwm, LOW);
break;
case 0xFFC23D: // Turn Right
Serial.println("FAST FORWARD");
digitalWrite(m2_A, HIGH);
digitalWrite(m2_B, LOW);
digitalWrite(m1_A, LOW);
digitalWrite(m1_B, HIGH);
digitalWrite(m2_pwm, HIGH);
digitalWrite(m1_pwm, LOW);
break;
case 0xFFA857: // Baackward
Serial.println("VOL-");
digitalWrite(m1_A, LOW);
digitalWrite(m1_B, HIGH);
digitalWrite(m2_A, LOW);
digitalWrite(m2_B, HIGH);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, HIGH);
break;
case 0xFFFFFFFF: Serial.println(" REPEAT"); break;
default:
Serial.println(" other button ");
}// End Case
delay(500); // Do not get immediate repeat
} //END translateIR
void setup() /*----( SETUP: RUNS ONCE )----*/
{
pinMode(m1_pwm,OUTPUT);
pinMode(m1_A,OUTPUT);
pinMode(m1_B,OUTPUT);
pinMode(m2_pwm,OUTPUT);
pinMode(m2_A,OUTPUT);
pinMode(m2_B,OUTPUT);
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver
}/*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
if (power == 1) {
digitalWrite(led, HIGH);
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
irrecv.resume(); // receive the next value
}
}
if (power == 0) {
digitalWrite(led, LOW);
digitalWrite(m1_pwm, LOW);
digitalWrite(m2_pwm, LOW);
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
case 0xFFA25D:
Serial.println("POWER");
power += 1;
power = power % 2;
Serial.println(power);
break;
}
irrecv.resume(); // receive the next value
}
}
}/* --(end main loop )-- */
/*
* IRremote
* Version 0.11 August, 2009
* Copyright 2009 Ken Shirriff
* For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.html
*
* Modified by Paul Stoffregen <paul@pjrc.com> to support other boards and timers
* Modified by Mitra Ardron <mitra@mitra.biz>
* Added Sanyo and Mitsubishi controllers
* Modified Sony to spot the repeat codes that some Sony's send
*
* Interrupt code based on NECIRrcv by Joe Knapp
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
* Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
*
* JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
* LG added by Darryl Smith (based on the JVC protocol)
*/
#include "IRremote.h"
#include "IRremoteInt.h"
// Provides ISR
#include <avr/interrupt.h>
volatile irparams_t irparams;
// These versions of MATCH, MATCH_MARK, and MATCH_SPACE are only for debugging.
// To use them, set DEBUG in IRremoteInt.h
// Normally macros are used for efficiency
#ifdef DEBUG
int MATCH(int measured, int desired) {
Serial.print("Testing: ");
Serial.print(TICKS_LOW(desired), DEC);
Serial.print(" <= ");
Serial.print(measured, DEC);
Serial.print(" <= ");
Serial.println(TICKS_HIGH(desired), DEC);
return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);
}
int MATCH_MARK(int measured_ticks, int desired_us) {
Serial.print("Testing mark ");
Serial.print(measured_ticks * USECPERTICK, DEC);
Serial.print(" vs ");
Serial.print(desired_us, DEC);
Serial.print(": ");
Serial.print(TICKS_LOW(desired_us + MARK_EXCESS), DEC);
Serial.print(" <= ");
Serial.print(measured_ticks, DEC);
Serial.print(" <= ");
Serial.println(TICKS_HIGH(desired_us + MARK_EXCESS), DEC);
return measured_ticks >= TICKS_LOW(desired_us + MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us + MARK_EXCESS);
}
int MATCH_SPACE(int measured_ticks, int desired_us) {
Serial.print("Testing space ");
Serial.print(measured_ticks * USECPERTICK, DEC);
Serial.print(" vs ");
Serial.print(desired_us, DEC);
Serial.print(": ");
Serial.print(TICKS_LOW(desired_us - MARK_EXCESS), DEC);
Serial.print(" <= ");
Serial.print(measured_ticks, DEC);
Serial.print(" <= ");
Serial.println(TICKS_HIGH(desired_us - MARK_EXCESS), DEC);
return measured_ticks >= TICKS_LOW(desired_us - MARK_EXCESS) && measured_ticks <= TICKS_HIGH(desired_us - MARK_EXCESS);
}
#else
int MATCH(int measured, int desired) {return measured >= TICKS_LOW(desired) && measured <= TICKS_HIGH(desired);}
int MATCH_MARK(int measured_ticks, int desired_us) {return MATCH(measured_ticks, (desired_us + MARK_EXCESS));}
int MATCH_SPACE(int measured_ticks, int desired_us) {return MATCH(measured_ticks, (desired_us - MARK_EXCESS));}
// Debugging versions are in IRremote.cpp
#endif
void IRsend::sendNEC(unsigned long data, int nbits)
{
enableIROut(38);
mark(NEC_HDR_MARK);
space(NEC_HDR_SPACE);
for (int i = 0; i < nbits; i++) {
if (data & TOPBIT) {
mark(NEC_BIT_MARK);
space(NEC_ONE_SPACE);
}
else {
mark(NEC_BIT_MARK);
space(NEC_ZERO_SPACE);
}
data <<= 1;
}
mark(NEC_BIT_MARK);
space(0);
}
void IRsend::sendSony(unsigned long data, int nbits) {
enableIROut(40);
mark(SONY_HDR_MARK);
space(SONY_HDR_SPACE);
data = data << (32 - nbits);
for (int i = 0; i < nbits; i++) {
if (data & TOPBIT) {
mark(SONY_ONE_MARK);
space(SONY_HDR_SPACE);
}
else {
mark(SONY_ZERO_MARK);
space(SONY_HDR_SPACE);
}
data <<= 1;
}
}
void IRsend::sendRaw(unsigned int buf[], int len, int hz)
{
enableIROut(hz);
for (int i = 0; i < len; i++) {
if (i & 1) {
space(buf[i]);
}
else {
mark(buf[i]);
}
}
space(0); // Just to be sure
}
// Note: first bit must be a one (start bit)
void IRsend::sendRC5(unsigned long data, int nbits)
{
enableIROut(36);
data = data << (32 - nbits);
mark(RC5_T1); // First start bit
space(RC5_T1); // Second start bit
mark(RC5_T1); // Second start bit
for (int i = 0; i < nbits; i++) {
if (data & TOPBIT) {
space(RC5_T1); // 1 is space, then mark
mark(RC5_T1);
}
else {
mark(RC5_T1);
space(RC5_T1);
}
data <<= 1;
}
space(0); // Turn off at end
}
// Caller needs to take care of flipping the toggle bit
void IRsend::sendRC6(unsigned long data, int nbits)
{
enableIROut(36);
data = data << (32 - nbits);
mark(RC6_HDR_MARK);
space(RC6_HDR_SPACE);
mark(RC6_T1); // start bit
space(RC6_T1);
int t;
for (int i = 0; i < nbits; i++) {
if (i == 3) {
// double-wide trailer bit
t = 2 * RC6_T1;
}
else {
t = RC6_T1;
}
if (data & TOPBIT) {
mark(t);
space(t);
}
else {
space(t);
mark(t);
}
data <<= 1;
}
space(0); // Turn off at end
}
void IRsend::sendPanasonic(unsigned int address, unsigned long data) {
enableIROut(35);
mark(PANASONIC_HDR_MARK);
space(PANASONIC_HDR_SPACE);
for(int i=0;i<16;i++)
{
mark(PANASONIC_BIT_MARK);
if (address & 0x8000) {
space(PANASONIC_ONE_SPACE);
} else {
space(PANASONIC_ZERO_SPACE);
}
address <<= 1;
}
for (int i=0; i < 32; i++) {
mark(PANASONIC_BIT_MARK);
if (data & TOPBIT) {
space(PANASONIC_ONE_SPACE);
} else {
space(PANASONIC_ZERO_SPACE);
}
data <<= 1;
}
mark(PANASONIC_BIT_MARK);
space(0);
}
void IRsend::sendJVC(unsigned long data, int nbits, int repeat)
{
enableIROut(38);
data = data << (32 - nbits);
if (!repeat){
mark(JVC_HDR_MARK);
space(JVC_HDR_SPACE);
}
for (int i = 0; i < nbits; i++) {
if (data & TOPBIT) {
mark(JVC_BIT_MARK);
space(JVC_ONE_SPACE);
}
else {
mark(JVC_BIT_MARK);
space(JVC_ZERO_SPACE);
}
data <<= 1;
}
mark(JVC_BIT_MARK);
space(0);
}
void IRsend::sendSAMSUNG(unsigned long data, int nbits)
{
enableIROut(38);
mark(SAMSUNG_HDR_MARK);
space(SAMSUNG_HDR_SPACE);
for (int i = 0; i < nbits; i++) {
if (data & TOPBIT) {
mark(SAMSUNG_BIT_MARK);
space(SAMSUNG_ONE_SPACE);
}
else {
mark(SAMSUNG_BIT_MARK);
space(SAMSUNG_ZERO_SPACE);
}
data <<= 1;
}
mark(SAMSUNG_BIT_MARK);
space(0);
}
void IRsend::mark(int time) {
// Sends an IR mark for the specified number of microseconds.
// The mark output is modulated at the PWM frequency.
TIMER_ENABLE_PWM; // Enable pin 3 PWM output
if (time > 0) delayMicroseconds(time);
}
/* Leave pin off for time (given in microseconds) */
void IRsend::space(int time) {
// Sends an IR space for the specified number of microseconds.
// A space is no output, so the PWM output is disabled.
TIMER_DISABLE_PWM; // Disable pin 3 PWM output
if (time > 0) delayMicroseconds(time);
}
void IRsend::enableIROut(int khz) {
// Enables IR output. The khz value controls the modulation frequency in kilohertz.
// The IR output will be on pin 3 (OC2B).
// This routine is designed for 36-40KHz; if you use it for other values, it's up to you
// to make sure it gives reasonable results. (Watch out for overflow / underflow / rounding.)
// TIMER2 is used in phase-correct PWM mode, with OCR2A controlling the frequency and OCR2B
// controlling the duty cycle.
// There is no prescaling, so the output frequency is 16MHz / (2 * OCR2A)
// To turn the output on and off, we leave the PWM running, but connect and disconnect the output pin.
// A few hours staring at the ATmega documentation and this will all make sense.
// See my Secrets of Arduino PWM at http://arcfn.com/2009/07/secrets-of-arduino-pwm.html for details.
// Disable the Timer2 Interrupt (which is used for receiving IR)
TIMER_DISABLE_INTR; //Timer2 Overflow Interrupt
pinMode(TIMER_PWM_PIN, OUTPUT);
digitalWrite(TIMER_PWM_PIN, LOW); // When not sending PWM, we want it low
// COM2A = 00: disconnect OC2A
// COM2B = 00: disconnect OC2B; to send signal set to 10: OC2B non-inverted
// WGM2 = 101: phase-correct PWM with OCRA as top
// CS2 = 000: no prescaling
// The top value for the timer. The modulation frequency will be SYSCLOCK / 2 / OCR2A.
TIMER_CONFIG_KHZ(khz);
}
IRrecv::IRrecv(int recvpin)
{
irparams.recvpin = recvpin;
irparams.blinkflag = 0;
}
// initialization
void IRrecv::enableIRIn() {
cli();
// setup pulse clock timer interrupt
//Prescale /8 (16M/8 = 0.5 microseconds per tick)
// Therefore, the timer interval can range from 0.5 to 128 microseconds
// depending on the reset value (255 to 0)
TIMER_CONFIG_NORMAL();
//Timer2 Overflow Interrupt Enable
TIMER_ENABLE_INTR;
TIMER_RESET;
sei(); // enable interrupts
// initialize state machine variables
irparams.rcvstate = STATE_IDLE;
irparams.rawlen = 0;
// set pin modes
pinMode(irparams.recvpin, INPUT);
}
// enable/disable blinking of pin 13 on IR processing
void IRrecv::blink13(int blinkflag)
{
irparams.blinkflag = blinkflag;
if (blinkflag)
pinMode(BLINKLED, OUTPUT);
}
// TIMER2 interrupt code to collect raw data.
// Widths of alternating SPACE, MARK are recorded in rawbuf.
// Recorded in ticks of 50 microseconds.
// rawlen counts the number of entries recorded so far.
// First entry is the SPACE between transmissions.
// As soon as a SPACE gets long, ready is set, state switches to IDLE, timing of SPACE continues.
// As soon as first MARK arrives, gap width is recorded, ready is cleared, and new logging starts
ISR(TIMER_INTR_NAME)
{
TIMER_RESET;
uint8_t irdata = (uint8_t)digitalRead(irparams.recvpin);
irparams.timer++; // One more 50us tick
if (irparams.rawlen >= RAWBUF) {
// Buffer overflow
irparams.rcvstate = STATE_STOP;
}
switch(irparams.rcvstate) {
case STATE_IDLE: // In the middle of a gap
if (irdata == MARK) {
if (irparams.timer < GAP_TICKS) {
// Not big enough to be a gap.
irparams.timer = 0;
}
else {
// gap just ended, record duration and start recording transmission
irparams.rawlen = 0;
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_MARK;
}
}
break;
case STATE_MARK: // timing MARK
if (irdata == SPACE) { // MARK ended, record time
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_SPACE;
}
break;
case STATE_SPACE: // timing SPACE
if (irdata == MARK) { // SPACE just ended, record it
irparams.rawbuf[irparams.rawlen++] = irparams.timer;
irparams.timer = 0;
irparams.rcvstate = STATE_MARK;
}
else { // SPACE
if (irparams.timer > GAP_TICKS) {
// big SPACE, indicates gap between codes
// Mark current code as ready for processing
// Switch to STOP
// Don't reset timer; keep counting space width
irparams.rcvstate = STATE_STOP;
}
}
break;
case STATE_STOP: // waiting, measuring gap
if (irdata == MARK) { // reset gap timer
irparams.timer = 0;
}
break;
}
if (irparams.blinkflag) {
if (irdata == MARK) {
BLINKLED_ON(); // turn pin 13 LED on
}
else {
BLINKLED_OFF(); // turn pin 13 LED off
}
}
}
void IRrecv::resume() {
irparams.rcvstate = STATE_IDLE;
irparams.rawlen = 0;
}
// Decodes the received IR message
// Returns 0 if no data ready, 1 if data ready.
// Results of decoding are stored in results
int IRrecv::decode(decode_results *results) {
results->rawbuf = irparams.rawbuf;
results->rawlen = irparams.rawlen;
if (irparams.rcvstate != STATE_STOP) {
return ERR;
}
#ifdef DEBUG
Serial.println("Attempting NEC decode");
#endif
if (decodeNEC(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting Sony decode");
#endif
if (decodeSony(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting Sanyo decode");
#endif
if (decodeSanyo(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting Mitsubishi decode");
#endif
if (decodeMitsubishi(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting RC5 decode");
#endif
if (decodeRC5(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting RC6 decode");
#endif
if (decodeRC6(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting Panasonic decode");
#endif
if (decodePanasonic(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting LG decode");
#endif
if (decodeLG(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting JVC decode");
#endif
if (decodeJVC(results)) {
return DECODED;
}
#ifdef DEBUG
Serial.println("Attempting SAMSUNG decode");
#endif
if (decodeSAMSUNG(results)) {
return DECODED;
}
// decodeHash returns a hash on any input.
// Thus, it needs to be last in the list.
// If you add any decodes, add them before this.
if (decodeHash(results)) {
return DECODED;
}
// Throw away and start over
resume();
return ERR;
}
// NECs have a repeat only 4 items long
long IRrecv::decodeNEC(decode_results *results) {
long data = 0;
int offset = 1; // Skip first space
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], NEC_HDR_MARK)) {
return ERR;
}
offset++;
// Check for repeat
if (irparams.rawlen == 4 &&
MATCH_SPACE(results->rawbuf[offset], NEC_RPT_SPACE) &&
MATCH_MARK(results->rawbuf[offset+1], NEC_BIT_MARK)) {
results->bits = 0;
results->value = REPEAT;
results->decode_type = NEC;
return DECODED;
}
if (irparams.rawlen < 2 * NEC_BITS + 4) {
return ERR;
}
// Initial space
if (!MATCH_SPACE(results->rawbuf[offset], NEC_HDR_SPACE)) {
return ERR;
}
offset++;
for (int i = 0; i < NEC_BITS; i++) {
if (!MATCH_MARK(results->rawbuf[offset], NEC_BIT_MARK)) {
return ERR;
}
offset++;
if (MATCH_SPACE(results->rawbuf[offset], NEC_ONE_SPACE)) {
data = (data << 1) | 1;
}
else if (MATCH_SPACE(results->rawbuf[offset], NEC_ZERO_SPACE)) {
data <<= 1;
}
else {
return ERR;
}
offset++;
}
// Success
results->bits = NEC_BITS;
results->value = data;
results->decode_type = NEC;
return DECODED;
}
long IRrecv::decodeSony(decode_results *results) {
long data = 0;
if (irparams.rawlen < 2 * SONY_BITS + 2) {
return ERR;
}
int offset = 0; // Dont skip first space, check its size
// Some Sony's deliver repeats fast after first
// unfortunately can't spot difference from of repeat from two fast clicks
if (results->rawbuf[offset] < SONY_DOUBLE_SPACE_USECS) {
// Serial.print("IR Gap found: ");
results->bits = 0;
results->value = REPEAT;
results->decode_type = SANYO;
return DECODED;
}
offset++;
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], SONY_HDR_MARK)) {
return ERR;
}
offset++;
while (offset + 1 < irparams.rawlen) {
if (!MATCH_SPACE(results->rawbuf[offset], SONY_HDR_SPACE)) {
break;
}
offset++;
if (MATCH_MARK(results->rawbuf[offset], SONY_ONE_MARK)) {
data = (data << 1) | 1;
}
else if (MATCH_MARK(results->rawbuf[offset], SONY_ZERO_MARK)) {
data <<= 1;
}
else {
return ERR;
}
offset++;
}
// Success
results->bits = (offset - 1) / 2;
if (results->bits < 12) {
results->bits = 0;
return ERR;
}
results->value = data;
results->decode_type = SONY;
return DECODED;
}
// I think this is a Sanyo decoder - serial = SA 8650B
// Looks like Sony except for timings, 48 chars of data and time/space different
long IRrecv::decodeSanyo(decode_results *results) {
long data = 0;
if (irparams.rawlen < 2 * SANYO_BITS + 2) {
return ERR;
}
int offset = 0; // Skip first space
// Initial space
/* Put this back in for debugging - note can't use #DEBUG as if Debug on we don't see the repeat cos of the delay
Serial.print("IR Gap: ");
Serial.println( results->rawbuf[offset]);
Serial.println( "test against:");
Serial.println(results->rawbuf[offset]);
*/
if (results->rawbuf[offset] < SANYO_DOUBLE_SPACE_USECS) {
// Serial.print("IR Gap found: ");
results->bits = 0;
results->value = REPEAT;
results->decode_type = SANYO;
return DECODED;
}
offset++;
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], SANYO_HDR_MARK)) {
return ERR;
}
offset++;
// Skip Second Mark
if (!MATCH_MARK(results->rawbuf[offset], SANYO_HDR_MARK)) {
return ERR;
}
offset++;
while (offset + 1 < irparams.rawlen) {
if (!MATCH_SPACE(results->rawbuf[offset], SANYO_HDR_SPACE)) {
break;
}
offset++;
if (MATCH_MARK(results->rawbuf[offset], SANYO_ONE_MARK)) {
data = (data << 1) | 1;
}
else if (MATCH_MARK(results->rawbuf[offset], SANYO_ZERO_MARK)) {
data <<= 1;
}
else {
return ERR;
}
offset++;
}
// Success
results->bits = (offset - 1) / 2;
if (results->bits < 12) {
results->bits = 0;
return ERR;
}
results->value = data;
results->decode_type = SANYO;
return DECODED;
}
// Looks like Sony except for timings, 48 chars of data and time/space different
long IRrecv::decodeMitsubishi(decode_results *results) {
// Serial.print("?!? decoding Mitsubishi:");Serial.print(irparams.rawlen); Serial.print(" want "); Serial.println( 2 * MITSUBISHI_BITS + 2);
long data = 0;
if (irparams.rawlen < 2 * MITSUBISHI_BITS + 2) {
return ERR;
}
int offset = 0; // Skip first space
// Initial space
/* Put this back in for debugging - note can't use #DEBUG as if Debug on we don't see the repeat cos of the delay
Serial.print("IR Gap: ");
Serial.println( results->rawbuf[offset]);
Serial.println( "test against:");
Serial.println(results->rawbuf[offset]);
*/
/* Not seeing double keys from Mitsubishi
if (results->rawbuf[offset] < MITSUBISHI_DOUBLE_SPACE_USECS) {
// Serial.print("IR Gap found: ");
results->bits = 0;
results->value = REPEAT;
results->decode_type = MITSUBISHI;
return DECODED;
}
*/
offset++;
// Typical
// 14200 7 41 7 42 7 42 7 17 7 17 7 18 7 41 7 18 7 17 7 17 7 18 7 41 8 17 7 17 7 18 7 17 7
// Initial Space
if (!MATCH_MARK(results->rawbuf[offset], MITSUBISHI_HDR_SPACE)) {
return ERR;
}
offset++;
while (offset + 1 < irparams.rawlen) {
if (MATCH_MARK(results->rawbuf[offset], MITSUBISHI_ONE_MARK)) {
data = (data << 1) | 1;
}
else if (MATCH_MARK(results->rawbuf[offset], MITSUBISHI_ZERO_MARK)) {
data <<= 1;
}
else {
// Serial.println("A"); Serial.println(offset); Serial.println(results->rawbuf[offset]);
return ERR;
}
offset++;
if (!MATCH_SPACE(results->rawbuf[offset], MITSUBISHI_HDR_SPACE)) {
// Serial.println("B"); Serial.println(offset); Serial.println(results->rawbuf[offset]);
break;
}
offset++;
}
// Success
results->bits = (offset - 1) / 2;
if (results->bits < MITSUBISHI_BITS) {
results->bits = 0;
return ERR;
}
results->value = data;
results->decode_type = MITSUBISHI;
return DECODED;
}
// Gets one undecoded level at a time from the raw buffer.
// The RC5/6 decoding is easier if the data is broken into time intervals.
// E.g. if the buffer has MARK for 2 time intervals and SPACE for 1,
// successive calls to getRClevel will return MARK, MARK, SPACE.
// offset and used are updated to keep track of the current position.
// t1 is the time interval for a single bit in microseconds.
// Returns -1 for error (measured time interval is not a multiple of t1).
int IRrecv::getRClevel(decode_results *results, int *offset, int *used, int t1) {
if (*offset >= results->rawlen) {
// After end of recorded buffer, assume SPACE.
return SPACE;
}
int width = results->rawbuf[*offset];
int val = ((*offset) % 2) ? MARK : SPACE;
int correction = (val == MARK) ? MARK_EXCESS : - MARK_EXCESS;
int avail;
if (MATCH(width, t1 + correction)) {
avail = 1;
}
else if (MATCH(width, 2*t1 + correction)) {
avail = 2;
}
else if (MATCH(width, 3*t1 + correction)) {
avail = 3;
}
else {
return -1;
}
(*used)++;
if (*used >= avail) {
*used = 0;
(*offset)++;
}
#ifdef DEBUG
if (val == MARK) {
Serial.println("MARK");
}
else {
Serial.println("SPACE");
}
#endif
return val;
}
long IRrecv::decodeRC5(decode_results *results) {
if (irparams.rawlen < MIN_RC5_SAMPLES + 2) {
return ERR;
}
int offset = 1; // Skip gap space
long data = 0;
int used = 0;
// Get start bits
if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR;
if (getRClevel(results, &offset, &used, RC5_T1) != SPACE) return ERR;
if (getRClevel(results, &offset, &used, RC5_T1) != MARK) return ERR;
int nbits;
for (nbits = 0; offset < irparams.rawlen; nbits++) {
int levelA = getRClevel(results, &offset, &used, RC5_T1);
int levelB = getRClevel(results, &offset, &used, RC5_T1);
if (levelA == SPACE && levelB == MARK) {
// 1 bit
data = (data << 1) | 1;
}
else if (levelA == MARK && levelB == SPACE) {
// zero bit
data <<= 1;
}
else {
return ERR;
}
}
// Success
results->bits = nbits;
results->value = data;
results->decode_type = RC5;
return DECODED;
}
long IRrecv::decodeRC6(decode_results *results) {
if (results->rawlen < MIN_RC6_SAMPLES) {
return ERR;
}
int offset = 1; // Skip first space
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], RC6_HDR_MARK)) {
return ERR;
}
offset++;
if (!MATCH_SPACE(results->rawbuf[offset], RC6_HDR_SPACE)) {
return ERR;
}
offset++;
long data = 0;
int used = 0;
// Get start bit (1)
if (getRClevel(results, &offset, &used, RC6_T1) != MARK) return ERR;
if (getRClevel(results, &offset, &used, RC6_T1) != SPACE) return ERR;
int nbits;
for (nbits = 0; offset < results->rawlen; nbits++) {
int levelA, levelB; // Next two levels
levelA = getRClevel(results, &offset, &used, RC6_T1);
if (nbits == 3) {
// T bit is double wide; make sure second half matches
if (levelA != getRClevel(results, &offset, &used, RC6_T1)) return ERR;
}
levelB = getRClevel(results, &offset, &used, RC6_T1);
if (nbits == 3) {
// T bit is double wide; make sure second half matches
if (levelB != getRClevel(results, &offset, &used, RC6_T1)) return ERR;
}
if (levelA == MARK && levelB == SPACE) { // reversed compared to RC5
// 1 bit
data = (data << 1) | 1;
}
else if (levelA == SPACE && levelB == MARK) {
// zero bit
data <<= 1;
}
else {
return ERR; // Error
}
}
// Success
results->bits = nbits;
results->value = data;
results->decode_type = RC6;
return DECODED;
}
long IRrecv::decodePanasonic(decode_results *results) {
unsigned long long data = 0;
int offset = 1;
if (!MATCH_MARK(results->rawbuf[offset], PANASONIC_HDR_MARK)) {
return ERR;
}
offset++;
if (!MATCH_MARK(results->rawbuf[offset], PANASONIC_HDR_SPACE)) {
return ERR;
}
offset++;
// decode address
for (int i = 0; i < PANASONIC_BITS; i++) {
if (!MATCH_MARK(results->rawbuf[offset++], PANASONIC_BIT_MARK)) {
return ERR;
}
if (MATCH_SPACE(results->rawbuf[offset],PANASONIC_ONE_SPACE)) {
data = (data << 1) | 1;
} else if (MATCH_SPACE(results->rawbuf[offset],PANASONIC_ZERO_SPACE)) {
data <<= 1;
} else {
return ERR;
}
offset++;
}
results->value = (unsigned long)data;
results->panasonicAddress = (unsigned int)(data >> 32);
results->decode_type = PANASONIC;
results->bits = PANASONIC_BITS;
return DECODED;
}
long IRrecv::decodeLG(decode_results *results) {
long data = 0;
int offset = 1; // Skip first space
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], LG_HDR_MARK)) {
return ERR;
}
offset++;
if (irparams.rawlen < 2 * LG_BITS + 1 ) {
return ERR;
}
// Initial space
if (!MATCH_SPACE(results->rawbuf[offset], LG_HDR_SPACE)) {
return ERR;
}
offset++;
for (int i = 0; i < LG_BITS; i++) {
if (!MATCH_MARK(results->rawbuf[offset], LG_BIT_MARK)) {
return ERR;
}
offset++;
if (MATCH_SPACE(results->rawbuf[offset], LG_ONE_SPACE)) {
data = (data << 1) | 1;
}
else if (MATCH_SPACE(results->rawbuf[offset], LG_ZERO_SPACE)) {
data <<= 1;
}
else {
return ERR;
}
offset++;
}
//Stop bit
if (!MATCH_MARK(results->rawbuf[offset], LG_BIT_MARK)){
return ERR;
}
// Success
results->bits = LG_BITS;
results->value = data;
results->decode_type = LG;
return DECODED;
}
long IRrecv::decodeJVC(decode_results *results) {
long data = 0;
int offset = 1; // Skip first space
// Check for repeat
if (irparams.rawlen - 1 == 33 &&
MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK) &&
MATCH_MARK(results->rawbuf[irparams.rawlen-1], JVC_BIT_MARK)) {
results->bits = 0;
results->value = REPEAT;
results->decode_type = JVC;
return DECODED;
}
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], JVC_HDR_MARK)) {
return ERR;
}
offset++;
if (irparams.rawlen < 2 * JVC_BITS + 1 ) {
return ERR;
}
// Initial space
if (!MATCH_SPACE(results->rawbuf[offset], JVC_HDR_SPACE)) {
return ERR;
}
offset++;
for (int i = 0; i < JVC_BITS; i++) {
if (!MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK)) {
return ERR;
}
offset++;
if (MATCH_SPACE(results->rawbuf[offset], JVC_ONE_SPACE)) {
data = (data << 1) | 1;
}
else if (MATCH_SPACE(results->rawbuf[offset], JVC_ZERO_SPACE)) {
data <<= 1;
}
else {
return ERR;
}
offset++;
}
//Stop bit
if (!MATCH_MARK(results->rawbuf[offset], JVC_BIT_MARK)){
return ERR;
}
// Success
results->bits = JVC_BITS;
results->value = data;
results->decode_type = JVC;
return DECODED;
}
// SAMSUNGs have a repeat only 4 items long
long IRrecv::decodeSAMSUNG(decode_results *results) {
long data = 0;
int offset = 1; // Skip first space
// Initial mark
if (!MATCH_MARK(results->rawbuf[offset], SAMSUNG_HDR_MARK)) {
return ERR;
}
offset++;
// Check for repeat
if (irparams.rawlen == 4 &&
MATCH_SPACE(results->rawbuf[offset], SAMSUNG_RPT_SPACE) &&
MATCH_MARK(results->rawbuf[offset+1], SAMSUNG_BIT_MARK)) {
results->bits = 0;
results->value = REPEAT;
results->decode_type = SAMSUNG;
return DECODED;
}
if (irparams.rawlen < 2 * SAMSUNG_BITS + 4) {
return ERR;
}
// Initial space
if (!MATCH_SPACE(results->rawbuf[offset], SAMSUNG_HDR_SPACE)) {
return ERR;
}
offset++;
for (int i = 0; i < SAMSUNG_BITS; i++) {
if (!MATCH_MARK(results->rawbuf[offset], SAMSUNG_BIT_MARK)) {
return ERR;
}
offset++;
if (MATCH_SPACE(results->rawbuf[offset], SAMSUNG_ONE_SPACE)) {
data = (data << 1) | 1;
}
else if (MATCH_SPACE(results->rawbuf[offset], SAMSUNG_ZERO_SPACE)) {
data <<= 1;
}
else {
return ERR;
}
offset++;
}
// Success
results->bits = SAMSUNG_BITS;
results->value = data;
results->decode_type = SAMSUNG;
return DECODED;
}
/* -----------------------------------------------------------------------
* hashdecode - decode an arbitrary IR code.
* Instead of decoding using a standard encoding scheme
* (e.g. Sony, NEC, RC5), the code is hashed to a 32-bit value.
*
* The algorithm: look at the sequence of MARK signals, and see if each one
* is shorter (0), the same length (1), or longer (2) than the previous.
* Do the same with the SPACE signals. Hszh the resulting sequence of 0's,
* 1's, and 2's to a 32-bit value. This will give a unique value for each
* different code (probably), for most code systems.
*
* http://arcfn.com/2010/01/using-arbitrary-remotes-with-arduino.html
*/
// Compare two tick values, returning 0 if newval is shorter,
// 1 if newval is equal, and 2 if newval is longer
// Use a tolerance of 20%
int IRrecv::compare(unsigned int oldval, unsigned int newval) {
if (newval < oldval * .8) {
return 0;
}
else if (oldval < newval * .8) {
return 2;
}
else {
return 1;
}
}
// Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param
#define FNV_PRIME_32 16777619
#define FNV_BASIS_32 2166136261
/* Converts the raw code values into a 32-bit hash code.
* Hopefully this code is unique for each button.
* This isn't a "real" decoding, just an arbitrary value.
*/
long IRrecv::decodeHash(decode_results *results) {
// Require at least 6 samples to prevent triggering on noise
if (results->rawlen < 6) {
return ERR;
}
long hash = FNV_BASIS_32;
for (int i = 1; i+2 < results->rawlen; i++) {
int value = compare(results->rawbuf[i], results->rawbuf[i+2]);
// Add value into the hash
hash = (hash * FNV_PRIME_32) ^ value;
}
results->value = hash;
results->bits = 32;
results->decode_type = UNKNOWN;
return DECODED;
}
/* Sharp and DISH support by Todd Treece ( http://unionbridge.org/design/ircommand )
The Dish send function needs to be repeated 4 times, and the Sharp function
has the necessary repeat built in because of the need to invert the signal.
Sharp protocol documentation:
http://www.sbprojects.com/knowledge/ir/sharp.htm
Here are the LIRC files that I found that seem to match the remote codes
from the oscilloscope:
Sharp LCD TV:
http://lirc.sourceforge.net/remotes/sharp/GA538WJSA
DISH NETWORK (echostar 301):
http://lirc.sourceforge.net/remotes/echostar/301_501_3100_5100_58xx_59xx
For the DISH codes, only send the last for characters of the hex.
i.e. use 0x1C10 instead of 0x0000000000001C10 which is listed in the
linked LIRC file.
*/
void IRsend::sendSharpRaw(unsigned long data, int nbits) {
enableIROut(38);
// Sending codes in bursts of 3 (normal, inverted, normal) makes transmission
// much more reliable. That's the exact behaviour of CD-S6470 remote control.
for (int n = 0; n < 3; n++) {
for (int i = 1 << (nbits-1); i > 0; i>>=1) {
if (data & i) {
mark(SHARP_BIT_MARK);
space(SHARP_ONE_SPACE);
}
else {
mark(SHARP_BIT_MARK);
space(SHARP_ZERO_SPACE);
}
}
mark(SHARP_BIT_MARK);
space(SHARP_ZERO_SPACE);
delay(40);
data = data ^ SHARP_TOGGLE_MASK;
}
}
// Sharp send compatible with data obtained through decodeSharp
void IRsend::sendSharp(unsigned int address, unsigned int command) {
sendSharpRaw((address << 10) | (command << 2) | 2, 15);
}
void IRsend::sendDISH(unsigned long data, int nbits) {
enableIROut(56);
mark(DISH_HDR_MARK);
space(DISH_HDR_SPACE);
for (int i = 0; i < nbits; i++) {
if (data & DISH_TOP_BIT) {
mark(DISH_BIT_MARK);
space(DISH_ONE_SPACE);
}
else {
mark(DISH_BIT_MARK);
space(DISH_ZERO_SPACE);
}
data <<= 1;
}
}
/*
* IRremote
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.htm http://arcfn.com
* Edited by Mitra to add new controller SANYO
*
* Interrupt code based on NECIRrcv by Joe Knapp
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
* Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
*
* JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
* LG added by Darryl Smith (based on the JVC protocol)
*/
#ifndef IRremote_h
#define IRremote_h
// The following are compile-time library options.
// If you change them, recompile the library.
// If DEBUG is defined, a lot of debugging output will be printed during decoding.
// TEST must be defined for the IRtest unittests to work. It will make some
// methods virtual, which will be slightly slower, which is why it is optional.
// #define DEBUG
// #define TEST
// Results returned from the decoder
class decode_results {
public:
int decode_type; // NEC, SONY, RC5, UNKNOWN
union { // This is used for decoding Panasonic and Sharp data
unsigned int panasonicAddress;
unsigned int sharpAddress;
};
unsigned long value; // Decoded value
int bits; // Number of bits in decoded value
volatile unsigned int *rawbuf; // Raw intervals in .5 us ticks
int rawlen; // Number of records in rawbuf.
};
// Values for decode_type
#define NEC 1
#define SONY 2
#define RC5 3
#define RC6 4
#define DISH 5
#define SHARP 6
#define PANASONIC 7
#define JVC 8
#define SANYO 9
#define MITSUBISHI 10
#define SAMSUNG 11
#define LG 12
#define UNKNOWN -1
// Decoded value for NEC when a repeat code is received
#define REPEAT 0xffffffff
// main class for receiving IR
class IRrecv
{
public:
IRrecv(int recvpin);
void blink13(int blinkflag);
int decode(decode_results *results);
void enableIRIn();
void resume();
private:
// These are called by decode
int getRClevel(decode_results *results, int *offset, int *used, int t1);
long decodeNEC(decode_results *results);
long decodeSony(decode_results *results);
long decodeSanyo(decode_results *results);
long decodeMitsubishi(decode_results *results);
long decodeRC5(decode_results *results);
long decodeRC6(decode_results *results);
long decodePanasonic(decode_results *results);
long decodeLG(decode_results *results);
long decodeJVC(decode_results *results);
long decodeSAMSUNG(decode_results *results);
long decodeHash(decode_results *results);
int compare(unsigned int oldval, unsigned int newval);
}
;
// Only used for testing; can remove virtual for shorter code
#ifdef TEST
#define VIRTUAL virtual
#else
#define VIRTUAL
#endif
class IRsend
{
public:
IRsend() {}
void sendNEC(unsigned long data, int nbits);
void sendSony(unsigned long data, int nbits);
// Neither Sanyo nor Mitsubishi send is implemented yet
// void sendSanyo(unsigned long data, int nbits);
// void sendMitsubishi(unsigned long data, int nbits);
void sendRaw(unsigned int buf[], int len, int hz);
void sendRC5(unsigned long data, int nbits);
void sendRC6(unsigned long data, int nbits);
void sendDISH(unsigned long data, int nbits);
void sendSharp(unsigned int address, unsigned int command);
void sendSharpRaw(unsigned long data, int nbits);
void sendPanasonic(unsigned int address, unsigned long data);
void sendJVC(unsigned long data, int nbits, int repeat); // *Note instead of sending the REPEAT constant if you want the JVC repeat signal sent, send the original code value and change the repeat argument from 0 to 1. JVC protocol repeats by skipping the header NOT by sending a separate code value like NEC does.
// private:
void sendSAMSUNG(unsigned long data, int nbits);
void enableIROut(int khz);
VIRTUAL void mark(int usec);
VIRTUAL void space(int usec);
}
;
// Some useful constants
#define USECPERTICK 50 // microseconds per clock interrupt tick
#define RAWBUF 100 // Length of raw duration buffer
// Marks tend to be 100us too long, and spaces 100us too short
// when received due to sensor lag.
#define MARK_EXCESS 100
#endif
/*
* IRremote
* Version 0.1 July, 2009
* Copyright 2009 Ken Shirriff
* For details, see http://arcfn.com/2009/08/multi-protocol-infrared-remote-library.htm http://arcfn.com
* Edited by Mitra to add new controller SANYO
*
* Interrupt code based on NECIRrcv by Joe Knapp
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210243556
* Also influenced by http://zovirl.com/2008/11/12/building-a-universal-remote-with-an-arduino/
*
* JVC and Panasonic protocol added by Kristian Lauszus (Thanks to zenwheel and other people at the original blog post)
* LG added by Darryl Smith (based on the JVC protocol)
*/
#ifndef IRremote_h
#define IRremote_h
// The following are compile-time library options.
// If you change them, recompile the library.
// If DEBUG is defined, a lot of debugging output will be printed during decoding.
// TEST must be defined for the IRtest unittests to work. It will make some
// methods virtual, which will be slightly slower, which is why it is optional.
// #define DEBUG
// #define TEST
// Results returned from the decoder
class decode_results {
public:
int decode_type; // NEC, SONY, RC5, UNKNOWN
union { // This is used for decoding Panasonic and Sharp data
unsigned int panasonicAddress;
unsigned int sharpAddress;
};
unsigned long value; // Decoded value
int bits; // Number of bits in decoded value
volatile unsigned int *rawbuf; // Raw intervals in .5 us ticks
int rawlen; // Number of records in rawbuf.
};
// Values for decode_type
#define NEC 1
#define SONY 2
#define RC5 3
#define RC6 4
#define DISH 5
#define SHARP 6
#define PANASONIC 7
#define JVC 8
#define SANYO 9
#define MITSUBISHI 10
#define SAMSUNG 11
#define LG 12
#define UNKNOWN -1
// Decoded value for NEC when a repeat code is received
#define REPEAT 0xffffffff
// main class for receiving IR
class IRrecv
{
public:
IRrecv(int recvpin);
void blink13(int blinkflag);
int decode(decode_results *results);
void enableIRIn();
void resume();
private:
// These are called by decode
int getRClevel(decode_results *results, int *offset, int *used, int t1);
long decodeNEC(decode_results *results);
long decodeSony(decode_results *results);
long decodeSanyo(decode_results *results);
long decodeMitsubishi(decode_results *results);
long decodeRC5(decode_results *results);
long decodeRC6(decode_results *results);
long decodePanasonic(decode_results *results);
long decodeLG(decode_results *results);
long decodeJVC(decode_results *results);
long decodeSAMSUNG(decode_results *results);
long decodeHash(decode_results *results);
int compare(unsigned int oldval, unsigned int newval);
}
;
// Only used for testing; can remove virtual for shorter code
#ifdef TEST
#define VIRTUAL virtual
#else
#define VIRTUAL
#endif
class IRsend
{
public:
IRsend() {}
void sendNEC(unsigned long data, int nbits);
void sendSony(unsigned long data, int nbits);
// Neither Sanyo nor Mitsubishi send is implemented yet
// void sendSanyo(unsigned long data, int nbits);
// void sendMitsubishi(unsigned long data, int nbits);
void sendRaw(unsigned int buf[], int len, int hz);
void sendRC5(unsigned long data, int nbits);
void sendRC6(unsigned long data, int nbits);
void sendDISH(unsigned long data, int nbits);
void sendSharp(unsigned int address, unsigned int command);
void sendSharpRaw(unsigned long data, int nbits);
void sendPanasonic(unsigned int address, unsigned long data);
void sendJVC(unsigned long data, int nbits, int repeat); // *Note instead of sending the REPEAT constant if you want the JVC repeat signal sent, send the original code value and change the repeat argument from 0 to 1. JVC protocol repeats by skipping the header NOT by sending a separate code value like NEC does.
// private:
void sendSAMSUNG(unsigned long data, int nbits);
void enableIROut(int khz);
VIRTUAL void mark(int usec);
VIRTUAL void space(int usec);
}
;
// Some useful constants
#define USECPERTICK 50 // microseconds per clock interrupt tick
#define RAWBUF 100 // Length of raw duration buffer
// Marks tend to be 100us too long, and spaces 100us too short
// when received due to sensor lag.
#define MARK_EXCESS 100
#endif
#include "IRremote.h"
int receiver = 9; // Signal Pin of IR receiver to Arduino Digital Pin 11
#define led 7 // Power LED
#define m1_pwm 3
#define m1_A 4
#define m1_B 5
#define m2_pwm 11
#define m2_A 12
#define m2_B 13
int power = 0; // controls except for toggling power can only be used when "on" (power = 1)
/*-----( Declare objects )-----*/
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
/*-----( Function )-----*/
void translateIR() // takes action based on IR code received
// describing Remote IR codes
{
switch(results.value)
{
case 0xFFA25D:
Serial.println("POWER");
power += 1;
power = power % 2;
Serial.println(power);
break;
case 0xFFE21D:
Serial.println("FUNC/STOP");
Serial.println("f1");
break;
case 0xFF629D:
Serial.println("VOL+");
digitalWrite(m1_A, HIGH);
digitalWrite(m1_B, LOW);
digitalWrite(m2_A, HIGH);
digitalWrite(m2_B, LOW);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, HIGH);
break;
case 0xFF22DD:
Serial.println("FAST BACK");
digitalWrite(m2_A, LOW);
digitalWrite(m2_B, HIGH);
digitalWrite(m1_A, HIGH);
digitalWrite(m1_B, LOW);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, LOW);
break;
case 0xFF02FD:
Serial.println("PAUSE");
digitalWrite(m1_pwm, LOW);
digitalWrite(m2_pwm, LOW);
break;
case 0xFFC23D:
Serial.println("FAST FORWARD");
digitalWrite(m2_A, HIGH);
digitalWrite(m2_B, LOW);
digitalWrite(m1_A, LOW);
digitalWrite(m1_B, HIGH);
digitalWrite(m2_pwm, HIGH);
digitalWrite(m1_pwm, LOW);
break;
case 0xFFA857:
Serial.println("VOL-");
digitalWrite(m1_A, LOW);
digitalWrite(m1_B, HIGH);
digitalWrite(m2_A, LOW);
digitalWrite(m2_B, HIGH);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, HIGH);
break;
case 0xFFFFFFFF: Serial.println(" REPEAT"); break;
default:
Serial.println(" other button ");
}// End Case
delay(500); // Do not get immediate repeat
} //END translateIR
void setup() /*----( SETUP: RUNS ONCE )----*/
{
pinMode(m1_pwm,OUTPUT);
pinMode(m1_A,OUTPUT);
pinMode(m1_B,OUTPUT);
pinMode(m2_pwm,OUTPUT);
pinMode(m2_A,OUTPUT);
pinMode(m2_B,OUTPUT);
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver
}/*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
if (power == 1) {
digitalWrite(led, HIGH);
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
irrecv.resume(); // receive the next value
}
}
if (power == 0) {
digitalWrite(led, LOW);
digitalWrite(m1_pwm, LOW);
digitalWrite(m2_pwm, LOW);
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
case 0xFFA25D:
Serial.println("POWER");
power += 1;
power = power % 2;
Serial.println(power);
break;
}
irrecv.resume(); // receive the next value
}
}
}/* --(end main loop )-- */
#include "IRremote.h"
int receiver = 9; // Signal Pin of IR receiver to Arduino Digital Pin 11
#define led 7 // Power LED
#define m1_pwm 3
#define m1_A 4
#define m1_B 5
#define m2_pwm 11
#define m2_A 12
#define m2_B 13
int power = 0; // controls except for toggling power can only be used when "on" (power = 1)
/*-----( Declare objects )-----*/
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
/*-----( Function )-----*/
void translateIR() // takes action based on IR code received
// describing Remote IR codes
{
switch(results.value)
{
case 0xFFA25D:
Serial.println("POWER");
power += 1;
power = power % 2;
Serial.println(power);
break;
case 0xFFE21D:
Serial.println("FUNC/STOP");
Serial.println("f1");
break;
case 0xFF629D:
Serial.println("VOL+");
digitalWrite(m1_A, HIGH);
digitalWrite(m1_B, LOW);
digitalWrite(m2_A, HIGH);
digitalWrite(m2_B, LOW);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, HIGH);
break;
case 0xFF22DD:
Serial.println("FAST BACK");
digitalWrite(m2_A, LOW);
digitalWrite(m2_B, HIGH);
digitalWrite(m1_A, HIGH);
digitalWrite(m1_B, LOW);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, LOW);
break;
case 0xFF02FD:
Serial.println("PAUSE");
digitalWrite(m1_pwm, LOW);
digitalWrite(m2_pwm, LOW);
break;
case 0xFFC23D:
Serial.println("FAST FORWARD");
digitalWrite(m2_A, HIGH);
digitalWrite(m2_B, LOW);
digitalWrite(m1_A, LOW);
digitalWrite(m1_B, HIGH);
digitalWrite(m2_pwm, HIGH);
digitalWrite(m1_pwm, LOW);
break;
case 0xFFA857:
Serial.println("VOL-");
digitalWrite(m1_A, LOW);
digitalWrite(m1_B, HIGH);
digitalWrite(m2_A, LOW);
digitalWrite(m2_B, HIGH);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, HIGH);
break;
case 0xFFFFFFFF: Serial.println(" REPEAT"); break;
default:
Serial.println(" other button ");
}// End Case
delay(500); // Do not get immediate repeat
} //END translateIR
void setup() /*----( SETUP: RUNS ONCE )----*/
{
pinMode(m1_pwm,OUTPUT);
pinMode(m1_A,OUTPUT);
pinMode(m1_B,OUTPUT);
pinMode(m2_pwm,OUTPUT);
pinMode(m2_A,OUTPUT);
pinMode(m2_B,OUTPUT);
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver
}/*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
if (power == 1) {
digitalWrite(led, HIGH);
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
irrecv.resume(); // receive the next value
}
}
if (power == 0) {
digitalWrite(led, LOW);
digitalWrite(m1_pwm, LOW);
digitalWrite(m2_pwm, LOW);
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
case 0xFFA25D:
Serial.println("POWER");
power += 1;
power = power % 2;
Serial.println(power);
break;
}
irrecv.resume(); // receive the next value
}
}
}/* --(end main loop )-- */
#include "IRremote.h"
int receiver = 9; // Signal Pin of IR receiver to Arduino Digital Pin 11
#define led 7 // Power LED
#define m1_pwm 3
#define m1_A 4
#define m1_B 5
#define m2_pwm 11
#define m2_A 12
#define m2_B 13
int power = 0; // controls except for toggling power can only be used when "on" (power = 1)
/*-----( Declare objects )-----*/
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
/*-----( Function )-----*/
void translateIR() // takes action based on IR code received
// describing Remote IR codes
{
switch(results.value)
{
case 0xFFA25D:
Serial.println("POWER");
power += 1;
power = power % 2;
Serial.println(power);
break;
case 0xFFE21D:
Serial.println("FUNC/STOP");
Serial.println("f1");
break;
case 0xFF629D:
Serial.println("VOL+");
digitalWrite(m1_A, HIGH);
digitalWrite(m1_B, LOW);
digitalWrite(m2_A, HIGH);
digitalWrite(m2_B, LOW);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, HIGH);
break;
case 0xFF22DD:
Serial.println("FAST BACK");
digitalWrite(m2_A, LOW);
digitalWrite(m2_B, HIGH);
digitalWrite(m1_A, HIGH);
digitalWrite(m1_B, LOW);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, LOW);
break;
case 0xFF02FD:
Serial.println("PAUSE");
digitalWrite(m1_pwm, LOW);
digitalWrite(m2_pwm, LOW);
break;
case 0xFFC23D:
Serial.println("FAST FORWARD");
digitalWrite(m2_A, HIGH);
digitalWrite(m2_B, LOW);
digitalWrite(m1_A, LOW);
digitalWrite(m1_B, HIGH);
digitalWrite(m2_pwm, HIGH);
digitalWrite(m1_pwm, LOW);
break;
case 0xFFA857:
Serial.println("VOL-");
digitalWrite(m1_A, LOW);
digitalWrite(m1_B, HIGH);
digitalWrite(m2_A, LOW);
digitalWrite(m2_B, HIGH);
digitalWrite(m1_pwm, HIGH);
digitalWrite(m2_pwm, HIGH);
break;
case 0xFFFFFFFF: Serial.println(" REPEAT"); break;
default:
Serial.println(" other button ");
}// End Case
delay(500); // Do not get immediate repeat
} //END translateIR
void setup() /*----( SETUP: RUNS ONCE )----*/
{
pinMode(m1_pwm,OUTPUT);
pinMode(m1_A,OUTPUT);
pinMode(m1_B,OUTPUT);
pinMode(m2_pwm,OUTPUT);
pinMode(m2_A,OUTPUT);
pinMode(m2_B,OUTPUT);
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
irrecv.enableIRIn(); // Start the receiver
}/*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
if (power == 1) {
digitalWrite(led, HIGH);
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
irrecv.resume(); // receive the next value
}
}
if (power == 0) {
digitalWrite(led, LOW);
digitalWrite(m1_pwm, LOW);
digitalWrite(m2_pwm, LOW);
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
case 0xFFA25D:
Serial.println("POWER");
power += 1;
power = power % 2;
Serial.println(power);
break;
}
irrecv.resume(); // receive the next value
}
}
}/* --(end main loop )-- */
Comments