Go Down

Topic: Program (Read 571 times) previous topic - next topic

sentrygun

Quote

#include <Servo.h>

Servo myServo1;
Servo myServo2;

const int servoPin1 = 7; // the pin the servo is connected to
const int servoPin2 = 8;

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

// 5 degree increments
#define ANGLE_STEP 5
#define ANGLE_STEP2 2

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_STEP2)
      {
        angle1 = angle1 - ANGLE_STEP2;  // step left
        Serial.println(angle1);
      }
      else
      {
        angle1 = 0;
      }
      myServo1.write(angle1);
    }
    
    else if(ch == 'a' || ch == 'A')
    {
      Serial.println("RIGHT");
      if(angle1 < 180 - ANGLE_STEP2)
      {
        angle1 = angle1 + ANGLE_STEP2; // 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);
      delay(150);
      digitalWrite(3,LOW);
    }
    else
    {
      digitalWrite(3,LOW);
    }
    
  }




AWOL

Yes, very good.
Why do you keep posting this?
"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.

lloyddean

#2
May 28, 2011, 07:37 pm Last Edit: May 29, 2011, 12:46 am by lloyddean Reason: 1
Throwing the code into a bag of runes, a quick shake, compress and dumping it back here ...

Code: [Select]

#include <Servo.h>


// --- USEFULL ENUM'S (USES NO RUNTIME MEMORY)

enum
{
     pinServoOne   = 7
   , pinServoTwo   = 8

   , pinWHAT        = 3
};


// --- MACROS

#define SERVO_ONE_STEP      (2)     // 2 degrees
#define SERVO_TWO_STEP      (5)     // 5 degrees


// --- GLOBALS

Servo   servoOne;
Servo   servoTwo;

int     angleServoOne;
int     angleServoTwo;


// --- REQUIRED CODE

void loop()
{
   if ( Serial.available() )
   {
       switch ( toupper(Serial.read()) )
       {
           case 'D':
               angleServoOne = ((angleServoOne > SERVO_ONE_STEP) ? (angleServoOne - SERVO_ONE_STEP) : 0);
               Serial.println("LEFT");
               Serial.println(angleServoOne);

               servoOne.write(angleServoOne);
               break;

           case 'A':
               angleServoOne = ((angleServoOne < 180 - SERVO_ONE_STEP) ? (angleServoOne + SERVO_ONE_STEP) : 180);
               Serial.println("RIGHT");
               Serial.println(angleServoOne);

               servoOne.write(angleServoOne);
               break;

           case 'W':
               angleServoTwo = ((angleServoTwo > SERVO_TWO_STEP) ? (angleServoTwo - SERVO_TWO_STEP) : 0);
               Serial.println("UP");
               Serial.println(angleServoTwo);

               servoTwo.write(angleServoTwo);
               break;

           case 'S':
               angleServoTwo = ((angleServoTwo < 150 - SERVO_TWO_STEP) ? (angleServoTwo + SERVO_TWO_STEP) : 150);
               Serial.println("DOWN");
               Serial.println(angleServoTwo);
   
               servoTwo.write(angleServoTwo);
               break;

           case 'F':
               digitalWrite(pinWHAT, HIGH);
               delay(150);
               digitalWrite(pinWHAT, LOW);
               break;

           default:
               digitalWrite(pinWHAT, LOW);
       }
   }
}

void setup()
{
   Serial.begin(9600);

   pinMode(pinWHAT, OUTPUT);

   // center the servo
   angleServoOne               = 90;
   servoOne.attach(pinServoOne);
   servoOne.write(angleServoOne);

   angleServoTwo               = 75;
   servoTwo.attach(pinServoTwo);
   servoTwo.write(angleServoTwo);
}


I don't know why I did it either!

robtillaart

An online  Arduino code beautifier , I was looking for that :)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

AWOL

#4
May 28, 2011, 09:47 pm Last Edit: May 28, 2011, 09:50 pm by AWOL Reason: 1
Code: [Select]
// 2 degree's
Excuse me sir, but there's a gentleman from the Apostrophe Police would like a word with you.

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

lloyddean

#5
May 29, 2011, 12:44 am Last Edit: May 29, 2011, 01:02 am by lloyddean Reason: 1
A true Gentlemen wouldn't say a thing about it  !P

But thanks anyway!

Go Up