#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 2void 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);
}}
}
Yes, very good.
Why do you keep posting this?
Throwing the code into a bag of runes, a quick shake, compress and dumping it back here ...
#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!
An online Arduino code beautifier , I was looking for that
// 2 degree's
Excuse me sir, but there's a gentleman from the Apostrophe Police would like a word with you.
A true Gentlemen wouldn't say a thing about it !P
But thanks anyway!