Go Down

Topic: Gun (Read 508 times) previous topic - next topic

sentrygun

Quote

#include <Servo.h>

Servo myServo1;
Servo myServo2;

const int servoPin1 = 9; // the pin the servo is connected to
const int servoPin2 = 10;

int angle1 = 90; // the current angle of the servo
int angle2 = 75;

// 5 degree increments
#define ANGLE_STEP 9

void setup()
{
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  myServo1.attach(servoPin1);
  myServo1.write(angle1);  // center the servo
  myServo2.attach(servoPin2);
  myServo2.write(angle2);
}


void loop()
{
  char ch;

  if(Serial.available())
  {
    ch = Serial.read();
    
    // to find values for the arrow keys:
    // Serial.println(ch, HEX);

    if(ch == 'd' || ch == 'D')
    {
      Serial.println("LEFT");
      if(angle1 > ANGLE_STEP)
      {
        angle1 = angle1 - ANGLE_STEP;  // step left
        Serial.println(angle1);
      }
      else
      {
        angle1 = 0;
      }
      myServo1.write(angle1);
    }
    
    else if(ch == 'a' || ch == 'A')
    {
      Serial.println("RIGHT");
      if(angle1 < 180 - ANGLE_STEP)
      {
        angle1 = angle1 + ANGLE_STEP; // step right
        Serial.println(angle1);
      }
      else
      {
        angle1 = 180;
      }
      myServo1.write(angle1);
    }
    
    else if(ch == 'w' || ch == 'W')
    {
      Serial.println("UP");
      if(angle2 > ANGLE_STEP)
      {
        angle2 = angle2 - ANGLE_STEP;  // step left
        Serial.println(angle2);
      }
      else
      {
        angle2 = 0;
      }
      myServo2.write(angle2);
    }
    
    else if(ch == 's' || ch == 'S')
    {
      Serial.println("DOWN");
      if(angle2 < 150 - ANGLE_STEP)
      {
        angle2 = angle2 + ANGLE_STEP; // step right
        Serial.println(angle2);
      }
      else
      {
        angle2 = 150;
      }
      myServo2.write(angle2);
    }   
   
   else if(ch == 'f' || ch == 'F')
    {
      digitalWrite(3, HIGH);
    }
    else
    {
      digitalWrite(3,LOW);
    }
    
  }




AWOL

Now, go back, click on "modify", delete the [ quote ] braces, highlight the code and then click on the # icon on the editot's toolbar.

Is this a question, or a post that should be in the Exhibition section?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up