From 05ecd361b343806764a247d719012e95a527883a Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Fri, 29 May 2015 14:25:24 +0200 Subject: [PATCH] [ES] Added 3.5 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-óff-by: Jim Martens --- es/blatt3/CMakeLists.txt | 4 ++ es/blatt3/uebung3-5/uebung3-5.ino | 106 ++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 es/blatt3/uebung3-5/uebung3-5.ino diff --git a/es/blatt3/CMakeLists.txt b/es/blatt3/CMakeLists.txt index 6132968..0bcfedd 100644 --- a/es/blatt3/CMakeLists.txt +++ b/es/blatt3/CMakeLists.txt @@ -16,3 +16,7 @@ generate_arduino_firmware(uebung3-3 generate_arduino_firmware(uebung3-4 SKETCH uebung3-4 PROGRAMMER usbtinyisp) + +generate_arduino_firmware(uebung3-5 + SKETCH uebung3-5 + PROGRAMMER usbtinyisp) diff --git a/es/blatt3/uebung3-5/uebung3-5.ino b/es/blatt3/uebung3-5/uebung3-5.ino new file mode 100644 index 0000000..54574bf --- /dev/null +++ b/es/blatt3/uebung3-5/uebung3-5.ino @@ -0,0 +1,106 @@ +#include + +// these variables describe the used hardware pins +// adjust them when you use other pins +// hardware pins +int az = 50; + +int ledPin = 13; + +// servo +Servo ourServo; +int servoPin = 11; + +// used to achieve a 10 Hz frequency +// don't touch them +long rc = 1049999; + +// flags for servo +bool volatile cwMaxReached = false; +bool volatile ccwMaxReached = false; +bool volatile lightLED = false; + +bool volatile read_ready = false; + + +/** + * Validates the new servo position. + * + * @param int newServoPos + */ +int validate_new_servo_pos(int newServoPos) { + + if (newServoPos > 159) { + newServoPos = 159; + cwMaxReached = true; + lightLED = true; + } + + if (newServoPos < 25) { + newServoPos = 25; + ccwMaxReached = true; + lightLED = true; + } + + return newServoPos; +} + +/** + * Parses the angle of the moveTo command. + * + * @param char command + */ +int parse_angle(char* command) { + // get position of ( and extract string until position of ) + char angle[5]; + + return (int) angle; +} + +/** + * Setup function for initial setup code + */ +void setup() { + // Configure pins + pinMode(ledPin, OUTPUT); + pinMode(az, OUTPUT); + digitalWrite(az, HIGH); + digitalWrite(ledPin, LOW); + + ourServo.attach(servoPin); + ourServo.write(90); + + // initialize serial port + Serial.begin(9600); +} + +/** + * Loop function for main code + */ +void loop() { + if (Serial.available() > 0) { + char command[8]; + char currentChar; + int i = 0; + bool readable = true; + while (readable) { + currentChar = Serial.read(); + readable = (currentChar != -1); + command[i] = currentChar; + i++; + } + command[i] = '\0'; + + int servoPos = parse_angle(command); + int validatedServoPos = validate_new_servo_pos(servoPos); + + ourServo.write(validatedServoPos); + + if (lightLED) { + lightLED = false; + digitalWrite(ledPin, HIGH); + delay(500); + digitalWrite(ledPin, LOW); + } + } +}