vulture2600

Network Mouse Control v 1.0.2 for Arduino

Jan 10th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.00 KB | None | 0 0
  1.  
  2. /* Network mouse control of laser pointer.
  3. This sketch listens for incomming requests sent from Processing control sketch and handles them accordingly to control servos to move a laser pointer.
  4. Version 1.0
  5. 10/30/2013
  6. Code adapted from sketches found at http://www.instructables.com/id/Overview/
  7.  
  8. Revisions:
  9. V1.0.1: Switched to port 5000 to see if lag decreases, detached servos while client disconnected. 10/31/13
  10. V1.0.2: Added garage door sensor input and another LED. 11/28/13
  11. V1.0.2.5: Added functionality to control two OptoIsolator circuits. 12/14/13
  12.  
  13. Pin layout is as follows:
  14. 0)  :
  15. 1)  :
  16. 2)  : Garage door sensor.
  17. 3) ~: Servo X.
  18. 4)  : SD Card CS.
  19. 5) ~: Servo Y.
  20. 6) ~: Servo A, not connected.
  21. 7)  : Buzzer.
  22. 8)  : Laser.
  23. 9) ~: Servo B, not connected.
  24. 10-13): Ethernet shield.
  25. A0) :
  26. A1) :
  27. A2) : Opto Pin 1
  28. A3) : Opto Pin 2
  29. A4) : Yellow client connected indicator LED.
  30. A5) : Red garage door open indicator LED.
  31.  
  32. */
  33.  
  34. #include <Servo.h>
  35. #include <SPI.h>
  36. #include <Ethernet.h>
  37.  
  38. byte mac[] = { 0xDE, 0xAD, 0xBD, 0xEF, 0xFE, 0xED }; //mac address of eth shield
  39. IPAddress ip (192, 168, 1, 176); //permanent local IP address
  40. IPAddress gateway (192, 168, 1, 1); //router address
  41. IPAddress subnet (255, 255, 255, 0);
  42. EthernetServer server(5000); //port 5000
  43.  
  44. //Global variables:
  45. Servo servoX, servoY, servoA, servoB;
  46. uint8_t x1, y1, a1, b1;
  47.  
  48. const uint8_t garage = 2;
  49. const uint8_t laserPin = 8;
  50. const uint8_t buzzerPin = 7;
  51. const uint8_t yellowLed = A4;
  52. const uint8_t redLed = A5;
  53.  
  54. unsigned long timeNow = 0;
  55.  
  56. EthernetClient client;
  57.  
  58. void setup() {
  59.   servoX.attach(3);
  60.   servoY.attach(5);
  61.   servoA.attach(6); //not connected.
  62.   servoB.attach(9); //not connected.
  63.   servoX.write(90);
  64.   servoY.write(90);
  65.   delay(250);
  66.   servoX.detach();
  67.   servoY.detach();
  68.  
  69.   pinMode(laserPin, OUTPUT);
  70.   pinMode(buzzerPin, OUTPUT);
  71.   pinMode(yellowLed, OUTPUT);
  72.   pinMode(redLed, OUTPUT);
  73.   pinMode(garage, INPUT_PULLUP);
  74.  
  75.   Ethernet.begin(mac, ip, gateway, subnet);
  76.   server.begin();
  77.  
  78.   Serial.begin(19200);
  79.   Serial.print(F("Device IP address: ")); //for debugging.
  80.   Serial.println(Ethernet.localIP());
  81.   if (Ethernet.localIP() != 0) {
  82.     for(uint8_t i = 1; i <= 4; i++) { //blink LEDs 4 times when connected to ethernet.
  83.       digitalWrite(redLed, HIGH);
  84.       digitalWrite(yellowLed, HIGH);
  85.       delay(100);
  86.       digitalWrite(redLed, LOW);
  87.       digitalWrite(yellowLed, LOW);
  88.       delay(100);
  89.     } //end for.
  90.   } //end if.
  91. } //end setup.
  92.  
  93. void loop() {
  94.   //a1 = servoA.read(); //gets actual position of servoA/B to send back to processing.
  95.   //b1 = servoB.read();
  96.   if (digitalRead (garage) == HIGH) {
  97.     digitalWrite(redLed, HIGH);
  98.   } //end if.
  99.   else {
  100.     digitalWrite(redLed, LOW);
  101.   } //end else.
  102.  
  103.   client = server.available();
  104.      
  105.   if (client) {
  106.     servoX.attach(3);
  107.     servoY.attach(5);
  108.    
  109.    
  110.     while (client.connected()) {    
  111.       if (digitalRead(garage) == HIGH) {
  112.         digitalWrite(redLed, HIGH); //turn on redLed while door open,
  113.       } //end if.
  114.       else {
  115.         digitalWrite(redLed, LOW);
  116.       } //end else.
  117.      
  118.       digitalWrite(yellowLed, HIGH); //turn on yellowLed while client connected.
  119.       delay(1); //lets server catch up.
  120.       static uint8_t v = 0;
  121.      
  122.       if (client.available() > 0){
  123.         char ch = client.read();
  124.         //Serial.println(ch);
  125.        
  126.         switch(ch) { //read incomming characters from processing and handle them case by case.
  127.           case '0'...'9': //0-9 are coordinates.
  128.             v = v * 10 + ch - '0';
  129.             break;
  130.            
  131.           case 'x': //signifies end of x coordinate.
  132.             x1 = map(v, 0, 180, 20, 160); //takes input of 0-180 and maps to 160-0 to limit range of servo movement and reverse X.
  133.             servoX.write(x1);
  134.             v = 0;
  135.             break;
  136.            
  137.           case 'y': //end of y coordinate.
  138.             y1 = map(v, 0, 180, 150, 20);
  139.             servoY.write(y1);
  140.             v = 0;
  141.             break;
  142.            
  143.           case 'l': //processing sends laser on command while mouse left is held.
  144.             digitalWrite(laserPin, HIGH);
  145.             client.write("1");
  146.             break;
  147.            
  148.           case 'k': //laser off command from processing.
  149.              digitalWrite(laserPin, LOW);
  150.              client.write("0");
  151.              break;
  152.              
  153.           /*  left mouse in Processing toggles laser on/off but never worked consistently.
  154.           case 'l': //turns laser on or off.
  155.             laserOnOff();
  156.             if (laser) {
  157.               client.write("1"); //tells processing laser is on.
  158.             }
  159.             else if (!laser) {
  160.               client.write("0"); //tells processing laser is off.
  161.             }
  162.             break;
  163.             */
  164.            
  165.           case 'u': //moves servoA up and sends data back to processing to draw a position bar on screen if in range.
  166.             a1 += 2;
  167.             if (a1 >= 150) {
  168.               a1 = 150;
  169.             } //end if.
  170.             servoA.write(a1);
  171.             if (a1 < 150) { //if servoA is in range, tell processing to move position bar.
  172.               client.write("2");
  173.             } //end if.
  174.             break;
  175.            
  176.           case 'd': //moves servoA down.
  177.             a1 -= 2;
  178.             if (a1 <= 20) {
  179.               a1 = 20;
  180.             } //end if.
  181.             servoA.write(a1);
  182.             if (a1 > 20) {
  183.               client.write("3");
  184.             } //end if.
  185.             break;
  186.            
  187.           case 'a': //same for servoB.
  188.             b1 -= 2;
  189.             if (b1 <= 20) {
  190.               b1 = 20;
  191.             } //end if.
  192.             servoB.write(b1);
  193.             if (b1 > 20) {
  194.               client.write("4");
  195.             } //end if.
  196.             break;
  197.            
  198.           case 's': //servoB right.
  199.             b1 += 2;
  200.             if (b1 >= 160) {
  201.               b1 = 160;
  202.             } //end if.
  203.             servoB.write(b1);
  204.             if (b1 < 160) {
  205.               client.write("5");
  206.             } //end if.
  207.             break;
  208.          
  209.           case 'b': //fires buzzer while mouse button is held.
  210.             digitalWrite(buzzerPin, HIGH);
  211.             break;
  212.            
  213.           case 'n':
  214.             digitalWrite (buzzerPin, LOW);
  215.             break;
  216.         } //end switch.  
  217.       } //end if client.available().
  218.     } //end while client.connected().
  219.    
  220.     client.flush(); //resets everything when client disconnects.
  221.     digitalWrite(laserPin, LOW);
  222.     digitalWrite(yellowLed, LOW);
  223.     servoX.write(90);
  224.     servoY.write(90);
  225.     delay(250);
  226.     servoX.detach(); //detach servos to eliminate twitching while idle.
  227.     servoY.detach();  
  228.   } //end if.        
  229. } //end loop.
  230.  
  231. /* never worked consistently.
  232. void laserOnOff() { //turns laser on/off.
  233.   if (laser == false) {
  234.     digitalWrite (laserPin, HIGH);
  235.     laser = true;
  236.   }
  237.   else if (laser == true) {
  238.     digitalWrite(laserPin, LOW);
  239.     laser = false;
  240.   }
  241. } */
Advertisement
Add Comment
Please, Sign In to add comment