Pages: [1]   Go Down
Author Topic: Gun  (Read 406 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
Gun
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
    }
    
  }



Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24452
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

"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.

Pages: [1]   Go Up
Jump to: