Advertisement
Tarielect

ArduinoPCMediaControl

Oct 6th, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.43 KB | None | 0 0
  1. //*************************************************************************
  2. //  Name     : ArduinoPCMediaControl                                      *
  3. //  Author   : Tarilayefa Edward                                          *
  4. //  Notice   : Tari Electronics & Embedded Systems (TEES), 2018.          *
  5. //           : tarielectronics@yahoo.com                                  *
  6. //           : tarielect.edward@gmail.com                                 *
  7. //           : +23408184754883,+23408062251186, Nigeria.                  *
  8. //           : https//:web.facebook.com/groups/teestraining/              *
  9. //           : https//:web.facebook.com/groups/picarduino/                *
  10. //  Date     : 01/10/2018                                                 *
  11. //  Version  : 1.0                                                        *
  12. //  Notes    : A program that adds Serial IR (Serial Infra-Red) input to  *
  13. //           : your computer to control windows programs like Power Point *
  14. //           : Presenter, vlc media player and Jet Audio Media Player     *
  15. //           : using any arduino board, IR remote sensor & any IR remote  *
  16. //           : controller. The front end (client side on arduino) is      *
  17. //           : developed with arduino while the back end (server side on  *
  18. //           : pc) is developed with Processing. arduino & processing     *
  19. //           : software.                                                  *                                      *
  20. //  Reference: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx         *
  21. //  Compiler : avrgcc                                                     *
  22. //  IDE      : Arduino 1.8.5                                              *
  23. //  Target   : Atmega 328, Arduino Uno, Nano.                             *
  24. //*************************************************************************
  25. //This version controls Only Power Point Presenter Program
  26. //You can modify it to control media player like vlc media player or
  27. //you can modify the hotKeys in your favourite media player like vlc to
  28. //that of the Power Point Presenter hot keys.
  29. //The trick i used here is to send commands from IR remote controller to
  30. //computer through serial using arduino & then using processing to capture
  31. //the commands and convert them to keyboard keystrokes in the backgroung
  32. //so that the highlighted currently running program will be controlled
  33. //by the keystrokes.
  34. //This device can also function as IR remote control tester
  35. //------------------------------------------------------------------------
  36. # include <IRLib.h>  //include the ir library
  37. int RECV_PIN = 2;  //ir remote sensor pin
  38. //-----------------------------------------------------------------------
  39. int irled = 3;  //signal led pin
  40. int arrowUp = 11;  //arrow up key
  41. int arrowDown = 12; //arrow down key
  42. int arrowRight = 10; //arrow right key
  43. int arrowLeft = 9; //arrow left key
  44. int f5 = 8; //f5 key
  45. int esc = 7; //escape key
  46. int shift_f5 = 6; //shift + f5 key combination
  47. int home = 5;
  48. int end = 4;
  49. //-----------------------------------------------------------------------
  50. int t1 = 300; //debounce delay
  51. int t2 = 300;
  52. int led1 = 13; //user led indicator
  53. //put these before setup
  54. //--------------------------------
  55. IRrecv My_Receiver(RECV_PIN);
  56. IRdecode My_Decoder;
  57. IRdecodeHash My_Hash_Decoder;
  58. //-----------------------------------------------------------------------
  59. //-----------------------------------------------------------------------
  60. void setup() {  //setup function
  61.     pinMode(arrowUp, INPUT_PULLUP);  
  62.     pinMode(arrowDown, INPUT_PULLUP);
  63.     pinMode(arrowLeft, INPUT_PULLUP);  
  64.     pinMode(arrowRight, INPUT_PULLUP);
  65.     pinMode(f5, INPUT_PULLUP);
  66.     pinMode(esc, INPUT_PULLUP);
  67.     pinMode(shift_f5, INPUT_PULLUP);
  68.     pinMode(home, INPUT_PULLUP);
  69.     pinMode(end, INPUT_PULLUP);
  70.     pinMode(led1, 1);
  71.     pinMode(irled, 1);
  72.     digitalWrite(irled, 1);
  73.     Serial.begin(9600);  
  74.     My_Receiver.enableIRIn(); // Start the receiver
  75. }
  76. //-----------------------------------------------------------------------
  77. //-----------------------------------------------------------------------
  78. void loop() { //main loop function
  79.     buttons();
  80.     ir_remote_dec();
  81. }
  82.  
  83. void arrUp(){
  84.     digitalWrite(led1, 1);
  85.     Serial.write(1); //send command 1 to pc serial terminal
  86. }
  87. //-----------------------------------------------------------------------
  88. void arrDwn(){
  89.     digitalWrite(led1, 1);
  90.     Serial.write(2); //send command to pc serial terminal
  91. }
  92. //-----------------------------------------------------------------------
  93. void arrRight(){
  94.     digitalWrite(led1, 1);
  95.     Serial.write(3);//send command 3 to pc serial terminal
  96. }
  97. //-----------------------------------------------------------------------
  98. void arrLeft(){
  99.     digitalWrite(led1, 1);
  100.     Serial.write(4);//send command 4 to pc serial terminal
  101. }
  102. //-----------------------------------------------------------------------
  103. void F5(){
  104.     digitalWrite(led1, 1);
  105.     Serial.write(5);//send command 5 to pc serial terminal
  106. }
  107. //-----------------------------------------------------------------------
  108. void ESC(){
  109.     digitalWrite(led1, 1);
  110.     Serial.write(6);//send command 6 to pc serial terminal
  111. }
  112. //-----------------------------------------------------------------------
  113. void SHIFT_F5(){
  114.     digitalWrite(led1, 1);
  115.     Serial.write(7);//send command 7 to pc serial terminal
  116. }
  117. //-----------------------------------------------------------------------
  118. void HOMe(){
  119.     digitalWrite(led1, 1);
  120.     Serial.write(8);//send command 8 to pc serial terminal
  121. }
  122. //-----------------------------------------------------------------------
  123. void ENd(){
  124.     digitalWrite(led1, 1);
  125.     Serial.write(9);//send command to 9 pc serial terminal
  126. }
  127. //-----------------------------------------------------------------------
  128. //-----------------------------------------------------------------------
  129. void buttons(){ //push button command function
  130.     int arrowUpVal = digitalRead(arrowUp);
  131.     int arrowDownVal = digitalRead(arrowDown);
  132.     int arrowRightVal = digitalRead(arrowRight);
  133.     int arrowLeftVal = digitalRead(arrowLeft);
  134.     int f5Val = digitalRead(f5);
  135.     int escVal = digitalRead(esc);
  136.     int shift_f5Val = digitalRead(shift_f5);
  137.     int homeVal = digitalRead(home);
  138.     int endVal = digitalRead(end);
  139.     //-----------------------------------------------------------------------
  140.     if(arrowUpVal == 0){ //push button for arrow up key
  141.         delay(t1);
  142.         arrUp();
  143.     }
  144.     //-----------------------------------------------------------------------  
  145.     else if(arrowDownVal == 0){ //push button for arrow down key
  146.         delay(t1);
  147.         arrDwn();
  148.     }
  149.     //-----------------------------------------------------------------------  
  150.     else if(arrowRightVal == 0){ //push button for arrow right key
  151.         delay(t1);
  152.         arrRight();
  153.     }
  154.     //-----------------------------------------------------------------------
  155.     else if(arrowLeftVal == 0){ //push button for arrow left key
  156.         delay(t1);
  157.         arrLeft();
  158.     }
  159.     //-----------------------------------------------------------------------
  160.     else if(f5Val == 0){  //push button for f5 key
  161.         delay(t1);
  162.         F5();
  163.     }
  164.     //-----------------------------------------------------------------------
  165.     else if(escVal == 0){  //push button for esc key
  166.         delay(t1);
  167.         ESC();
  168.     }
  169.     //-----------------------------------------------------------------------
  170.     else if(shift_f5Val == 0){  //push button for shift + f5 key combination
  171.         delay(t1);
  172.         SHIFT_F5();
  173.     }
  174.     //-----------------------------------------------------------------------
  175.     else if(homeVal == 0){  //push button for home key
  176.         delay(t1);
  177.         HOMe();
  178.     }
  179.     //-----------------------------------------------------------------------
  180.     else if(endVal == 0){  //push button for end key
  181.         delay(t1);
  182.         ENd();
  183.     }
  184.     //-----------------------------------------------------------------------
  185.     else{
  186.         digitalWrite(led1, 0);
  187.         Serial.write(0);
  188.     }
  189.     //-----------------------------------------------------------------------
  190.     delay(100);
  191. }
  192. //-----------------------------------------------------------------------
  193. //-----------------------------------------------------------------------
  194. void ir_remote_dec(){  //ir remote decoding function
  195.     if (My_Receiver.GetResults(&My_Decoder)) {//Puts results in My_Decoder
  196.         My_Hash_Decoder.copyBuf(&My_Decoder);//copy the results to the hash decoder
  197.         My_Decoder.decode();
  198.         //Serial.print(Pnames(My_Decoder.decode_type));
  199.         My_Hash_Decoder.decode();
  200.         //Serial.print(": ");
  201.         //Serial.println(My_Hash_Decoder.hash, DEC); // Do something with this value
  202.         ir_rem_btn_control(); //call ir rem button control function
  203.         My_Receiver.resume();
  204.         sigled();
  205.     }
  206. }
  207. //-----------------------------------------------------------------------
  208. //-----------------------------------------------------------------------
  209. void ir_rem_btn_control(){  //ir rem button control function
  210.     switch (My_Hash_Decoder.hash) {
  211.  
  212.     case 3843267751:;  //arrow up button PHILIPS RC7940 REMOTE CONTROL
  213.         delay(t2);  //button debounce delay
  214.         arrUp(); //call arrow up function
  215.         break;
  216.  
  217.     case 2891014758:;
  218.         delay(t2);  
  219.         arrUp();
  220.         break;
  221.         //-----------------------------------------------------------------------
  222.     case 3826490130:;  //arrow down
  223.         delay(t2);
  224.         arrDwn();//call arrow down function
  225.         break;
  226.  
  227.     case 2907792379:;
  228.         delay(t2);
  229.         arrDwn();
  230.         break;
  231.         //-----------------------------------------------------------------------
  232.     case 10463599:; //arrow right
  233.         delay(t2);
  234.         arrRight();//call arrow right function
  235.         break;
  236.  
  237.     case 2568141210:;
  238.         delay(t2);
  239.         arrRight();
  240.         break;
  241.         //-----------------------------------------------------------------------
  242.     case 3684511895:;  //arrow left
  243.         delay(t2);
  244.         arrLeft();//call arrow left function
  245.         break;
  246.     case 3189060206:;
  247.         delay(t2);
  248.         arrLeft();
  249.         break;
  250.         //-----------------------------------------------------------------------
  251.     case 3657003019:;  //f5
  252.         delay(t2);
  253.         F5();//call f5 function
  254.         break;
  255.     case 3216569086:;
  256.         delay(t2);
  257.         F5();
  258.         break;
  259.         //-----------------------------------------------------------------------
  260.     case 1794360052:; //shift + f5
  261.         delay(t2);
  262.         SHIFT_F5();//call shift + f5 function
  263.         break;
  264.     case 3604291944:;
  265.         delay(t2);
  266.         SHIFT_F5();
  267.         break;
  268.         //-----------------------------------------------------------------------
  269.     case 543645334:; //home
  270.         delay(t2);
  271.         HOMe();//call home function
  272.         break;
  273.     case 1539338986:;
  274.         delay(t2);
  275.         HOMe();
  276.         break;
  277.         //-----------------------------------------------------------------------
  278.     case 2808911318:; //end
  279.         delay(t2);
  280.         ENd();//call end function
  281.         break;
  282.     case  2672577770:;
  283.         delay(t2);
  284.         ENd();
  285.         break;
  286.         //-----------------------------------------------------------------------
  287.     case 2157964078:; //esc
  288.         delay(t2);
  289.         ESC();//call escape function
  290.         break;
  291.     case 784960098:; //esc
  292.         delay(t2);
  293.         ESC();
  294.         break;
  295.     }
  296. }
  297. //-----------------------------------------------------------------------
  298. //-----------------------------------------------------------------------
  299. void sigled(){  //signal led function
  300.     digitalWrite(irled, 0);
  301.     delay(50);
  302.     digitalWrite(irled, 1);
  303. }
  304. //-----------------------------------------------------------------------
  305. //########################################################################
  306. //Below is Processing code that will run on pc
  307. /*
  308. //*************************************************************************
  309. //  Name     : ArduinoPCMediaControl                                      *
  310. //  Author   : Tarilayefa Edward                                          *
  311. //  Notice   : Tari Electronics & Embedded Systems (TEES), 2018.          *
  312. //           : tarielectronics@yahoo.com                                  *
  313. //           : tarielect.edward@gmail.com                                 *
  314. //           : +23408184754883,+23408062251186, Nigeria.                  *
  315. //           : https//:web.facebook.com/groups/teestraining/              *
  316. //           : https//:web.facebook.com/groups/picarduino/                *
  317. //  Date     : 01/10/2018                                                 *
  318. //  Version  : 1.0                                                        *
  319. //  Notes    : A program to control Power Point Presenter Program on PC   *
  320. //           : with any arduino board using arduino & processing software.*                                       *
  321. //  Reference: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx         *
  322. //  Compiler : Java                                                       *
  323. //  IDE      : Processing 3.3.6                                           *
  324. //  Target   : Atmega 328, Arduino Uno, Nano.                             *
  325. //*************************************************************************
  326. import java.awt.Robot;
  327. import java.awt.AWTException;
  328. import processing.serial.*;
  329. import java.awt.event.KeyEvent;
  330. import java.awt.event.InputEvent;
  331. //-----------------------------------------------------------------------
  332. Serial myPort;  // Create object from Serial class
  333. int x;      // Data received from the serial port
  334. int t1 = 10;
  335. //-----------------------------------------------------------------------
  336. Robot robot;
  337. //-----------------------------------------------------------------------
  338. void setup() {
  339.   String portName = Serial.list()[0];
  340.   myPort = new Serial(this, portName, 9600);
  341.  
  342.   size(200, 200);
  343. }
  344. //-----------------------------------------------------------------------
  345. //-----------------------------------------------------------------------
  346. void draw()
  347. {
  348.   try {
  349.     robot = new Robot();
  350.     readSerial();
  351.   }
  352.   catch (AWTException e) {
  353.     e.printStackTrace();
  354.     exit();
  355.   }
  356. }
  357. //-----------------------------------------------------------------------
  358. //-----------------------------------------------------------------------
  359. void readSerial() {
  360.   if ( myPort.available() > 0) {  // If data is available,
  361.     x = myPort.read();         // read it and store it in val
  362.     //-------------------------------------------------------------------
  363.     if (x == 1) {
  364.       arrowKeyUp();
  365.     }
  366.     //-------------------------------------------------------------------
  367.     if (x == 2) {
  368.       arrowKeyDwn();
  369.     }
  370.     //-------------------------------------------------------------------
  371.     if (x == 3) {
  372.       arrowKeyRight();
  373.     }
  374.     //-------------------------------------------------------------------
  375.     if (x == 4) {
  376.       arrowKeyLeft();
  377.     }
  378.     //-------------------------------------------------------------------
  379.     if (x == 5) {
  380.       f5();
  381.     }
  382.     //-------------------------------------------------------------------
  383.     if (x == 6) {
  384.       esc();
  385.     }
  386.     //-------------------------------------------------------------------
  387.     if (x == 7) {
  388.       shift_f5();
  389.     }
  390.     //-------------------------------------------------------------------
  391.     if (x == 8) {
  392.       home();
  393.     }
  394.     //-------------------------------------------------------------------
  395.     if (x == 9) {
  396.       end();
  397.     }
  398.     //------------------------------------------------------------------
  399.     if (x == 38) {
  400.       pageup();
  401.     }
  402.     //------------------------------------------------------------------
  403.     if (x == 39) {
  404.       pagedown();
  405.     }
  406.     //------------------------------------------------------------------
  407.  
  408.     println("val = "+x);
  409.   }
  410. }
  411. //-----------------------------------------------------------------------
  412. //-----------------------------------------------------------------------
  413. void arrowKeyUp() {//1 (power point/windows)
  414.   robot.keyPress(KeyEvent.VK_UP);  
  415.   delay(t1);
  416.   robot.keyRelease(KeyEvent.VK_UP);
  417. }
  418. //-----------------------------------------------------------------------
  419. //-----------------------------------------------------------------------
  420. void arrowKeyDwn() {//2 (power point/windows)
  421.   robot.keyPress(KeyEvent.VK_DOWN);  
  422.   delay(t1);
  423.   robot.keyRelease(KeyEvent.VK_DOWN);
  424. }
  425. //-----------------------------------------------------------------------
  426. //-----------------------------------------------------------------------
  427. void arrowKeyRight() {//3 (power point/windows)
  428.   robot.keyPress(KeyEvent.VK_RIGHT);  
  429.   delay(t1);
  430.   robot.keyRelease(KeyEvent.VK_RIGHT);
  431. }
  432. //-----------------------------------------------------------------------
  433. //-----------------------------------------------------------------------
  434. void arrowKeyLeft() {//4 (power point/windows)
  435.   robot.keyPress(KeyEvent.VK_LEFT);  
  436.   delay(t1);
  437.   robot.keyRelease(KeyEvent.VK_LEFT);
  438. }
  439. //-----------------------------------------------------------------------
  440. //-----------------------------------------------------------------------
  441. void f5() {//5 (power point/windows)
  442.   robot.keyPress(KeyEvent.VK_F5);  
  443.   delay(t1);
  444.   robot.keyRelease(KeyEvent.VK_F5);
  445. }
  446. //-----------------------------------------------------------------------
  447. //-----------------------------------------------------------------------
  448. void esc() {//6 (power point/windows)
  449.   robot.keyPress(KeyEvent.VK_ESCAPE);  
  450.   delay(t1);
  451.   robot.keyRelease(KeyEvent.VK_ESCAPE);
  452. }
  453. //-----------------------------------------------------------------------
  454. //-----------------------------------------------------------------------
  455. void shift_f5() {//7
  456.   robot.keyPress(KeyEvent.VK_SHIFT);  
  457.   delay(t1);
  458.   robot.keyPress(KeyEvent.VK_F5);
  459.   delay(t1);
  460.   robot.keyRelease(KeyEvent.VK_F5);
  461.   robot.keyRelease(KeyEvent.VK_SHIFT);
  462. }
  463. //-----------------------------------------------------------------------
  464. //-----------------------------------------------------------------------
  465. void home() {//8 (power point/windows)
  466.   robot.keyPress(KeyEvent.VK_HOME);  
  467.   delay(t1);
  468.   robot.keyRelease(KeyEvent.VK_HOME);
  469. }
  470. //-----------------------------------------------------------------------
  471. //-----------------------------------------------------------------------
  472. void end() {//9 (power point/windows)
  473.   robot.keyPress(KeyEvent.VK_END);  
  474.   delay(t1);
  475.   robot.keyRelease(KeyEvent.VK_END);
  476. }
  477. //-----------------------------------------------------------------------
  478. //-----------------------------------------------------------------------
  479. void pageup() {//38 (power point/windows)
  480.   robot.keyPress(KeyEvent.VK_PAGE_UP);  
  481.   delay(t1);
  482.   robot.keyRelease(KeyEvent.VK_PAGE_UP);
  483. }
  484. //-----------------------------------------------------------------------
  485. //-----------------------------------------------------------------------
  486. void pagedown() {//39 (power point/windows)
  487.   robot.keyPress(KeyEvent.VK_PAGE_DOWN);  
  488.   delay(t1);
  489.   robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
  490. }
  491. //-----------------------------------------------------------------------
  492. //-----------------------------------------------------------------------
  493.  
  494. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement