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

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



Logged

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

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

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Throwing the code into a bag of runes, a quick shake, compress and dumping it back here ...

Code:
#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!
« Last Edit: May 28, 2011, 05:46:27 pm by lloyddean » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An online  Arduino code beautifier , I was looking for that smiley
Logged

Rob Tillaart

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

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

Code:
// 2 degree's
Excuse me sir, but there's a gentleman from the Apostrophe Police would like a word with you.

« Last Edit: May 28, 2011, 02:50:03 pm by AWOL » 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.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A true Gentlemen wouldn't say a thing about it  !P

But thanks anyway!
« Last Edit: May 28, 2011, 06:02:15 pm by lloyddean » Logged

Pages: [1]   Go Up
Jump to: