Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //Arduino code to control a helicotper.
  2. int IRledPin =  12;    
  3. int incomingByte = 0;
  4. String incomingString;
  5. int pulseValues[33];
  6. int pulseLength = 0;
  7.  
  8. void setup()   {                
  9.   // initialize the IR digital pin as an output:
  10.   pinMode(IRledPin, OUTPUT);      
  11.   pinMode(13, OUTPUT);  
  12.   Serial.begin(9600);
  13.  
  14.   for (int i=0; i < 13; i++)
  15.     pulseValues[i] = 0;
  16.  
  17.  
  18. }
  19.  
  20. void loop()                    
  21. {
  22.   SendCode();
  23. }
  24.  
  25.  
  26. void pulseIR(long microsecs) {
  27.   cli();  // this turns off any background interrupts
  28.  
  29.   while (microsecs > 0) {
  30.     // 38 kHz is about 13 microseconds high and 13 microseconds low
  31.     digitalWrite(IRledPin, HIGH);  // this takes about 3 microseconds to happen
  32.     delayMicroseconds(10);         // hang out for 10 microseconds
  33.     digitalWrite(IRledPin, LOW);   // this also takes about 3 microseconds
  34.     delayMicroseconds(10);         // hang out for 10 microseconds
  35.  
  36.     // so 26 microseconds altogether
  37.     microsecs -= 26;
  38.  
  39.   }
  40.  
  41.   sei();  // this turns them back on
  42. }
  43.  
  44. void Zero()
  45. {  
  46.   pulseIR(300);
  47.   delayMicroseconds(300);
  48.   pulseLength += 600;
  49. }
  50.  
  51. void One()
  52. {
  53.   pulseIR(300);
  54.   delayMicroseconds(600);
  55.   pulseLength += 900;
  56. }
  57.  
  58. void sendPulseValue(int pulseValue)
  59. {
  60.   if (pulseValue == 1)
  61.     One();
  62.   else
  63.     Zero();
  64. }
  65.  
  66. void checkPulseChanges()
  67. {
  68.   if (Serial.available() > 0)
  69.   {
  70.     incomingByte = Serial.read();
  71.  
  72.     //Pulse 1
  73.     if (incomingByte == 'a')
  74.       pulseValues[0] = 0;
  75.     if (incomingByte == 'A')
  76.       pulseValues[0] = 1;
  77.  
  78.     //Pulse 2
  79.     if (incomingByte == 'b')
  80.       pulseValues[1] = 0;
  81.     if (incomingByte =='B')
  82.       pulseValues[1] = 1;
  83.  
  84.     //Pulse 3
  85.     if (incomingByte == 'c')
  86.       pulseValues[2] = 0;
  87.     if (incomingByte == 'C')
  88.       pulseValues[2] = 1;
  89.  
  90.     //Pulse 4
  91.     if (incomingByte == 'd')
  92.       pulseValues[3] = 0;
  93.     if (incomingByte == 'D')
  94.       pulseValues[3] = 1;
  95.  
  96.     //Pulse 5
  97.     if (incomingByte == 'e')
  98.       pulseValues[4] = 0;
  99.     if (incomingByte == 'E')
  100.       pulseValues[4] = 1;
  101.  
  102.     //Pulse 6
  103.     if (incomingByte == 'f')
  104.       pulseValues[5] = 0;
  105.     if (incomingByte == 'F')
  106.       pulseValues[5] = 1;
  107.  
  108.     //Pulse 7
  109.     if (incomingByte == 'g')
  110.       pulseValues[6] = 0;
  111.     if (incomingByte == 'G')
  112.       pulseValues[6] = 1;    
  113.  
  114.     //Pulse 8
  115.     if (incomingByte == 'h')
  116.       pulseValues[7] = 0;
  117.     if (incomingByte == 'H')
  118.       pulseValues[7] = 1;
  119.  
  120.     //Pulse 9
  121.     if (incomingByte == 'i')
  122.       pulseValues[8] = 0;
  123.     if (incomingByte == 'I')
  124.       pulseValues[8] = 1;
  125.  
  126.     //Pulse 10
  127.     if (incomingByte == 'j')
  128.       pulseValues[9] = 0;
  129.     if (incomingByte == 'J')
  130.       pulseValues[9] = 1;
  131.  
  132.     //Pulse 11
  133.     if (incomingByte == 'k')
  134.       pulseValues[10] = 0;
  135.     if (incomingByte == 'K')
  136.       pulseValues[10] = 1;
  137.  
  138.     //Pulse 12
  139.     if (incomingByte == 'l')
  140.       pulseValues[11] = 0;
  141.     if (incomingByte == 'L')
  142.       pulseValues[11] = 1;
  143.  
  144.     //Pulse 13
  145.     if (incomingByte == 'm')
  146.       pulseValues[12] = 0;
  147.     if (incomingByte == 'M')
  148.       pulseValues[12] = 1;
  149.  
  150.     //Pulse 14
  151.     if (incomingByte == 'o')
  152.       pulseValues[13] = 0;
  153.     if (incomingByte == 'O')
  154.       pulseValues[13] = 1;
  155.  
  156.     //Pulse 15
  157.     if (incomingByte == 'p')
  158.       pulseValues[14] = 0;
  159.     if (incomingByte == 'P')
  160.       pulseValues[14] = 1;
  161.  
  162.     //Pulse 16
  163.     if (incomingByte == 'q')
  164.       pulseValues[15] = 0;
  165.     if (incomingByte == 'Q')
  166.       pulseValues[15] = 1;
  167.  
  168.     //Pulse 17
  169.     if (incomingByte == 'r')
  170.       pulseValues[16] = 0;
  171.     if (incomingByte == 'R')
  172.       pulseValues[16] = 1;
  173.  
  174.     //Pulse 18
  175.     if (incomingByte == 's')
  176.       pulseValues[17] = 0;
  177.     if (incomingByte == 'S')
  178.       pulseValues[17] = 1;
  179.  
  180.     //Pulse 19
  181.     if (incomingByte == 't')
  182.       pulseValues[18] = 0;
  183.     if (incomingByte == 'T')
  184.       pulseValues[18] = 1;
  185.  
  186.     //Pulse 20
  187.     if (incomingByte == 'u')
  188.       pulseValues[19] = 0;
  189.     if (incomingByte == 'U')
  190.       pulseValues[19] = 1;
  191.  
  192.     //Pulse 21
  193.     if (incomingByte == 'v')
  194.       pulseValues[20] = 0;
  195.     if (incomingByte == 'V')
  196.       pulseValues[20] = 1;
  197.  
  198.     //Pulse 22
  199.     if (incomingByte == 'w')
  200.       pulseValues[21] = 0;
  201.     if (incomingByte == 'W')
  202.       pulseValues[21] = 1;
  203.  
  204.     //Pulse 23
  205.     if (incomingByte == 'x')
  206.       pulseValues[22] = 0;
  207.     if (incomingByte == 'X')
  208.       pulseValues[22] = 1;
  209.  
  210.     //Pulse 24
  211.     if (incomingByte == 'y')
  212.       pulseValues[23] = 0;
  213.     if (incomingByte == 'Y')
  214.       pulseValues[23] = 1;
  215.  
  216.     //Pulse 25
  217.     if (incomingByte == 'z')
  218.       pulseValues[24] = 0;
  219.     if (incomingByte == 'Z')
  220.       pulseValues[24] = 1;
  221.  
  222.     //Pulse 26
  223.     if (incomingByte == '1')
  224.       pulseValues[25] = 0;
  225.     if (incomingByte == '2')
  226.       pulseValues[25] = 1;
  227.  
  228.     //Pulse 27
  229.     if (incomingByte == '3')
  230.       pulseValues[26] = 0;
  231.     if (incomingByte == '4')
  232.       pulseValues[26] = 1;
  233.  
  234.     //Pulse 28
  235.     if (incomingByte == '5')
  236.       pulseValues[27] = 0;
  237.     if (incomingByte == '6')
  238.       pulseValues[27] = 1;
  239.  
  240.     //Pulse 29
  241.     if (incomingByte == '7')
  242.       pulseValues[28] = 0;
  243.     if (incomingByte == '8')
  244.       pulseValues[28] = 1;
  245.  
  246.     //Pulse 30
  247.     if (incomingByte == '9')
  248.       pulseValues[29] = 0;
  249.     if (incomingByte == '!')
  250.       pulseValues[29] = 1;
  251.  
  252.     //Pulse 31
  253.     if (incomingByte == '@')
  254.       pulseValues[30] = 0;
  255.     if (incomingByte == '#')
  256.       pulseValues[30] = 1;
  257.  
  258.     //Pulse 32
  259.     if (incomingByte == '$')
  260.       pulseValues[31] = 0;
  261.     if (incomingByte == '%')
  262.       pulseValues[31] = 1;
  263.  
  264.     //Pulse 33
  265.     if (incomingByte == '^')
  266.       pulseValues[32] = 0;
  267.     if (incomingByte == '&')
  268.       pulseValues[32] = 1;      
  269.   }
  270.  
  271. }
  272.  
  273. void SendCode() {
  274.  
  275.   while (true)
  276.   {
  277.     checkPulseChanges();
  278.  
  279.     pulseIR(4000);
  280.     delayMicroseconds(2000);
  281.     pulseLength=6000;
  282.  
  283.     sendPulseValue(pulseValues[0]);
  284.     sendPulseValue(pulseValues[1]);
  285.     sendPulseValue(pulseValues[2]);
  286.     sendPulseValue(pulseValues[3]);
  287.     sendPulseValue(pulseValues[4]);
  288.     sendPulseValue(pulseValues[5]);
  289.     sendPulseValue(pulseValues[6]);
  290.     sendPulseValue(pulseValues[7]);
  291.     sendPulseValue(pulseValues[8]);
  292.     sendPulseValue(pulseValues[9]);
  293.     sendPulseValue(pulseValues[10]);
  294.     sendPulseValue(pulseValues[11]);
  295.     sendPulseValue(pulseValues[12]);
  296.     sendPulseValue(pulseValues[13]);
  297.     sendPulseValue(pulseValues[14]);
  298.     sendPulseValue(pulseValues[15]);
  299.     sendPulseValue(pulseValues[16]);
  300.     sendPulseValue(pulseValues[17]);
  301.     sendPulseValue(pulseValues[18]);
  302.     sendPulseValue(pulseValues[19]);
  303.     sendPulseValue(pulseValues[20]);
  304.     sendPulseValue(pulseValues[21]);
  305.     sendPulseValue(pulseValues[22]);
  306.     sendPulseValue(pulseValues[23]);
  307.     sendPulseValue(pulseValues[24]);
  308.     sendPulseValue(pulseValues[25]);
  309.     sendPulseValue(pulseValues[26]);
  310.     sendPulseValue(pulseValues[27]);
  311.     sendPulseValue(pulseValues[28]);
  312.     sendPulseValue(pulseValues[29]);
  313.     sendPulseValue(pulseValues[30]);
  314.     sendPulseValue(pulseValues[31]);
  315.  
  316.     //Footer
  317.     pulseIR(360);
  318.     delayMicroseconds( (28600 - pulseLength) );
  319.    }
  320. }