Advertisement
vulture2600

Network Mouse Control v 1.0.2 for Arduino

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