Advertisement
vulture2600

arduino ethernet mouse control

Oct 25th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.56 KB | None | 0 0
  1. /* attempting to program UNO with ethernet shield to accept commands from a client.
  2. copied code almost exactly from mouse_test
  3. added a power transistor to the external voltage regulator to power them up only when a client is connected. this stops the jitter of servos while idle.
  4. PWM pins remaining after ethernetshield are 3, 5, 6, and 9.
  5.  
  6. */
  7. #include <Servo.h>
  8. #include <SPI.h>
  9. #include <Ethernet.h>
  10.  
  11. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //mac address of eth shield
  12. IPAddress ip (192,168,1,177); //permanent local IP address
  13. IPAddress gateway (192,168,1,1); //router address
  14. IPAddress subnet (255,255,255,0);
  15. EthernetServer server(80); //using port 80
  16.  
  17. Servo servoX, servoY, servoA, servoB; //global variables:
  18. int x1, y1, a1, b1;
  19. const int servoPower = 2;
  20. const int laserPin = 8;
  21. const int buzzerPin = 7;
  22. bool laser = false;
  23. boolean alreadyConnected = false;
  24.  
  25. void setup() {
  26.   servoX.attach(3);
  27.   servoY.attach(5);
  28.   servoA.attach(6);
  29.   servoB.attach(6);
  30.   servoX.write(90);
  31.   servoY.write(90);
  32.  
  33.   pinMode(laserPin, OUTPUT);
  34.   pinMode(buzzerPin, OUTPUT);
  35.   pinMode(servoPower, OUTPUT);
  36.   digitalWrite(laserPin, LOW);
  37.  
  38.   Ethernet.begin(mac, ip, gateway, subnet);
  39.   server.begin();
  40.  
  41.   Serial.begin(19200);
  42.   Serial.print("Web server address:");
  43.   Serial.println(Ethernet.localIP());
  44. }
  45.  
  46. void loop() {
  47.   a1 = servoA.read(); //gets actual position of servoA/B to send back to processing.
  48.   b1 = servoB.read();
  49.   EthernetClient client = server.available();
  50.   if (client) {
  51.     if (!alreadyConnected){
  52.       client.flush();
  53.       Serial.println("Disconnected.");
  54.       alreadyConnected = true;
  55.     }
  56.    
  57.     while (!client.connected()){
  58.       digitalWrite(servoPower, LOW); //turns off servos and laser while no client is connected.
  59.       laser = false;
  60.       digitalWrite(laserPin, LOW);
  61.     }
  62.    
  63.     while (client.connected()){
  64.       digitalWrite(servoPower, HIGH); //powers on servos.
  65.       delay(1);
  66.       static int v = 0;
  67.       if (client.available()>0){
  68.         char ch = client.read();
  69.         switch(ch) { //read incomming characters from processing and handle them case by case.
  70.           case '0'...'9': //0-9 are coordinates.
  71.             v = v *10 + ch - '0';
  72.             break;
  73.            
  74.           case 'x': //signifies end of x coordinate.
  75.             x1 = map(v, 0, 180, 160, 0);
  76.             servoX.write(x1);
  77.             v = 0;
  78.             break;
  79.            
  80.           case 'y': //end of y coordinate.
  81.             y1 = map(v, 0, 180, 10, 170);
  82.             servoY.write(y1);
  83.             v = 0;
  84.             break;
  85.            
  86.           case 'l': //turns laser on or off.
  87.             laserOnOff();
  88.             if (laser){
  89.               client.write("1"); //tells processing laser is on.
  90.             }
  91.             else if (!laser){
  92.               client.write("0"); //tells processing laser is off.
  93.             }
  94.             break;
  95.            
  96.           case 'u': //moves servoA up and sends data back to processing to draw a position bar on screen if in range.
  97.             a1 += 2;
  98.             if (a1 >= 150){
  99.               a1 = 150;
  100.             }
  101.             servoA.write(a1);
  102.             if (a1 < 150){ //if servoA is in range, tell processing to move position bar.
  103.               client.write("2");
  104.             }
  105.             break;
  106.            
  107.           case 'd':
  108.             a1 -= 2;
  109.             if (a1 <= 20){
  110.               a1 = 20;
  111.             }
  112.             servoA.write(a1);
  113.             if (a1 > 20){
  114.               client.write("3");
  115.             }
  116.             break;
  117.            
  118.           case 'a':
  119.             b1 -= 2;
  120.             if (b1 <= 20){
  121.               b1 = 20;
  122.             }
  123.             servoB.write(b1); //same for servoB:
  124.             if (b1 > 20){
  125.               client.write("4");
  126.             }
  127.             break;
  128.            
  129.           case 's':
  130.             b1 += 2;
  131.             if (b1 >= 160){
  132.               b1 = 160;
  133.             }
  134.             servoB.write(b1);
  135.             if (b1 < 160){
  136.               client.write("5");
  137.             }
  138.             break;
  139.          
  140.           case 'b': //fires buzzer while mouse button is held.
  141.             digitalWrite(buzzerPin, HIGH);
  142.             break;
  143.            
  144.           case 'n':
  145.             digitalWrite (buzzerPin, LOW);
  146.             break;
  147.         }  
  148.       }
  149.     } client.flush();
  150.   }        
  151. }
  152.  
  153. void laserOnOff(){ //turns laser on/off.
  154.   if (laser == false){
  155.     digitalWrite (laserPin, HIGH);
  156.     laser = true;
  157.   }
  158.   else if (laser == true){
  159.     digitalWrite(laserPin, LOW);
  160.     laser = false;
  161.   }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement