Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //code to allow Processing to control servos with mouse input.
- //PWM pins remaining are 3, 5, 6 and 9.
- #include <Servo.h>
- Servo servoX;
- Servo servoY;
- Servo servoA;
- Servo servoB;
- const int laserPin = 8;
- const int buzzerPin = 7;
- bool laser;
- int x1, y1, a1, b1;//servo position variables
- void setup() {
- servoX.attach(3);
- servoY.attach(5);
- servoA.attach(6);
- servoB.attach(9);
- pinMode(laserPin, OUTPUT);
- pinMode(buzzerPin, OUTPUT);
- digitalWrite(laserPin, LOW);
- laser = false;
- Serial.begin(19200);
- servoX.write(90);
- servoY.write(90);
- servoA.write(90);
- servoB.write(90);
- }
- void loop() {
- /* most of this loop reads incomming characters from Processing and decides what to do with them on a case by case basis. if they are numbers, process them as integers coordinates.
- if they are letters, they signify the end of a coordinate (ex. 45x, 125y), turn the laser on or off, or fire the buzzer.
- */
- a1 = servoA.read();//assigns actual position of servo to a1/b1.
- b1 = servoB.read();
- static int v = 0; //value to send to servo (0-180)
- if (Serial.available()){
- char ch = Serial.read();
- switch(ch){ //switch based on value of ch
- case '0'...'9':
- v = v * 10 + ch - '0';
- break;
- /* if chars sent are, for example, '45x' (turn servo x to 45)
- v is what we want to send to servoX, currently 0
- first digit is 10's place, second is 1's place. v now equals an integer.
- third is not numeric so it will be handled by next case.
- */
- case 'x': // if char is x, end of x coordinate recieved.
- //send value of v to x servo and reset v to 0
- x1 = map(v, 0, 180, 160, 0);//reverses x. reverse 160 and 0 if x is backwards. output values not symmetrical to account for servos actual range.
- servoX.write(x1);
- v = 0;
- break;
- case 'y': //write y value to servoY
- y1 = map(v, 0, 180, 10, 170);
- servoY.write(y1);
- v = 0;
- break;
- case 'l': //laserOnOff() function:
- laserOnOff();
- break;
- case 'u': //moves servoA up.
- a1 += 2; //increments a1 by 2 degrees
- if (a1 >= 150){ //limits servoA to 150
- a1 = 150;
- }
- servoA.write(a1);
- if (a1 < 150){//if servoA is in range, tell processing to move position slider
- Serial.write("2");
- }
- break;
- case 'd': //moves servoA down.
- a1 -= 2;//decrements a1 by 2
- if (a1 <= 20){//limits servoA to 20 degrees
- a1 = 20;
- }
- servoA.write(a1);
- if (a1 > 20){//if servoA is in range, tell processing to move position slider
- Serial.write("3");
- }
- break;
- case 'a': // moves servoB left.
- b1 -= 2; //decrements b1 by 2
- if (b1 <= 20){//limits servoB to 20 degrees.
- b1 = 20;
- }
- servoB.write(b1);
- if (b1 > 20){//if servoB is within range, tell processing to move slider
- Serial.write("4");
- }
- break;
- case 's': // moves servoB right.
- b1 += 2;//increments b1 by 2
- if (b1 >= 160){//limits servoB to 160 degrees.
- b1 = 160;
- }
- servoB.write(b1);
- if (b1 < 160){//if servoB is within range, tell processing to move slider
- Serial.write("5");
- }
- break;
- case 'b': //turns on buzzer while right click is held
- digitalWrite (buzzerPin, HIGH);
- break;
- case 'n': //turns off buzzer when right click is released.
- digitalWrite (buzzerPin, LOW);
- break;
- }
- }
- }
- void laserOnOff(){//toggles laser on or off and sends feed back to processing.
- if(laser == false){
- digitalWrite (laserPin, HIGH);
- Serial.print("1");
- laser = true;
- }
- else if (laser == true){
- digitalWrite (laserPin, LOW);
- Serial.print("0");
- laser = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement