Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Network mouse control of laser pointer.
- This sketch listens for incomming requests sent from Processing control sketch and handles them accordingly to control servos to move a laser pointer.
- Version 1.0
- 10/30/2013
- steve.a.mccluskey@gmail.com
- Code adapted from sketches found at http://www.instructables.com/id/Overview/
- Revisions:
- V1.0.1: Switched to port 5000 to see if lag decreases, detached servos while client disconnected. 10/31/13
- V1.0.2: Added garage door sensor input and another LED. 11/28/13
- V1.0.2.5: Added functionality to control two OptoIsolator circuits. 12/14/13
- Pin layout is as follows:
- 0) :
- 1) :
- 2) : Garage door sensor.
- 3) ~: Servo X.
- 4) : SD Card CS.
- 5) ~: Servo Y.
- 6) ~: Servo A, not connected.
- 7) : Buzzer.
- 8) : Laser.
- 9) ~: Servo B, not connected.
- 10-13): Ethernet shield.
- A0) :
- A1) :
- A2) : Opto Pin 1
- A3) : Opto Pin 2
- A4) : Yellow client connected indicator LED.
- A5) : Red garage door open indicator LED.
- */
- #include <Servo.h>
- #include <SPI.h>
- #include <Ethernet.h>
- byte mac[] = { 0xDE, 0xAD, 0xBD, 0xEF, 0xFE, 0xED }; //mac address of eth shield
- IPAddress ip (192, 168, 1, 176); //permanent local IP address
- IPAddress gateway (192, 168, 1, 1); //router address
- IPAddress subnet (255, 255, 255, 0);
- EthernetServer server(5000); //port 5000
- //Global variables:
- Servo servoX, servoY, servoA, servoB;
- uint8_t x1, y1, a1, b1;
- const uint8_t garage = 2;
- const uint8_t laserPin = 8;
- const uint8_t buzzerPin = 7;
- const uint8_t yellowLed = A4;
- const uint8_t redLed = A5;
- unsigned long timeNow = 0;
- EthernetClient client;
- void setup() {
- servoX.attach(3);
- servoY.attach(5);
- servoA.attach(6); //not connected.
- servoB.attach(9); //not connected.
- servoX.write(90);
- servoY.write(90);
- delay(250);
- servoX.detach();
- servoY.detach();
- pinMode(laserPin, OUTPUT);
- pinMode(buzzerPin, OUTPUT);
- pinMode(yellowLed, OUTPUT);
- pinMode(redLed, OUTPUT);
- pinMode(garage, INPUT_PULLUP);
- Ethernet.begin(mac, ip, gateway, subnet);
- server.begin();
- Serial.begin(19200);
- Serial.print(F("Device IP address: ")); //for debugging.
- Serial.println(Ethernet.localIP());
- if (Ethernet.localIP() != 0) {
- for(uint8_t i = 1; i <= 4; i++) { //blink LEDs 4 times when connected to ethernet.
- digitalWrite(redLed, HIGH);
- digitalWrite(yellowLed, HIGH);
- delay(100);
- digitalWrite(redLed, LOW);
- digitalWrite(yellowLed, LOW);
- delay(100);
- } //end for.
- } //end if.
- } //end setup.
- void loop() {
- //a1 = servoA.read(); //gets actual position of servoA/B to send back to processing.
- //b1 = servoB.read();
- if (digitalRead (garage) == HIGH) {
- digitalWrite(redLed, HIGH);
- } //end if.
- else {
- digitalWrite(redLed, LOW);
- } //end else.
- client = server.available();
- if (client) {
- servoX.attach(3);
- servoY.attach(5);
- while (client.connected()) {
- if (digitalRead(garage) == HIGH) {
- digitalWrite(redLed, HIGH); //turn on redLed while door open,
- } //end if.
- else {
- digitalWrite(redLed, LOW);
- } //end else.
- digitalWrite(yellowLed, HIGH); //turn on yellowLed while client connected.
- delay(1); //lets server catch up.
- static uint8_t v = 0;
- if (client.available() > 0){
- char ch = client.read();
- //Serial.println(ch);
- switch(ch) { //read incomming characters from processing and handle them case by case.
- case '0'...'9': //0-9 are coordinates.
- v = v * 10 + ch - '0';
- break;
- case 'x': //signifies end of x coordinate.
- 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.
- servoX.write(x1);
- v = 0;
- break;
- case 'y': //end of y coordinate.
- y1 = map(v, 0, 180, 150, 20);
- servoY.write(y1);
- v = 0;
- break;
- case 'l': //processing sends laser on command while mouse left is held.
- digitalWrite(laserPin, HIGH);
- client.write("1");
- break;
- case 'k': //laser off command from processing.
- digitalWrite(laserPin, LOW);
- client.write("0");
- break;
- /* left mouse in Processing toggles laser on/off but never worked consistently.
- case 'l': //turns laser on or off.
- laserOnOff();
- if (laser) {
- client.write("1"); //tells processing laser is on.
- }
- else if (!laser) {
- client.write("0"); //tells processing laser is off.
- }
- break;
- */
- case 'u': //moves servoA up and sends data back to processing to draw a position bar on screen if in range.
- a1 += 2;
- if (a1 >= 150) {
- a1 = 150;
- } //end if.
- servoA.write(a1);
- if (a1 < 150) { //if servoA is in range, tell processing to move position bar.
- client.write("2");
- } //end if.
- break;
- case 'd': //moves servoA down.
- a1 -= 2;
- if (a1 <= 20) {
- a1 = 20;
- } //end if.
- servoA.write(a1);
- if (a1 > 20) {
- client.write("3");
- } //end if.
- break;
- case 'a': //same for servoB.
- b1 -= 2;
- if (b1 <= 20) {
- b1 = 20;
- } //end if.
- servoB.write(b1);
- if (b1 > 20) {
- client.write("4");
- } //end if.
- break;
- case 's': //servoB right.
- b1 += 2;
- if (b1 >= 160) {
- b1 = 160;
- } //end if.
- servoB.write(b1);
- if (b1 < 160) {
- client.write("5");
- } //end if.
- break;
- case 'b': //fires buzzer while mouse button is held.
- digitalWrite(buzzerPin, HIGH);
- break;
- case 'n':
- digitalWrite (buzzerPin, LOW);
- break;
- } //end switch.
- } //end if client.available().
- } //end while client.connected().
- client.flush(); //resets everything when client disconnects.
- digitalWrite(laserPin, LOW);
- digitalWrite(yellowLed, LOW);
- servoX.write(90);
- servoY.write(90);
- delay(250);
- servoX.detach(); //detach servos to eliminate twitching while idle.
- servoY.detach();
- } //end if.
- } //end loop.
- /* never worked consistently.
- void laserOnOff() { //turns laser on/off.
- if (laser == false) {
- digitalWrite (laserPin, HIGH);
- laser = true;
- }
- else if (laser == true) {
- digitalWrite(laserPin, LOW);
- laser = false;
- }
- } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement