1
0
mirror of https://github.com/2martens/uni.git synced 2026-05-06 11:26:25 +02:00

[ES] Added 3.5

Signed-óff-by: Jim Martens <github@2martens.de>
This commit is contained in:
2015-05-29 14:25:24 +02:00
parent 854ee68ff5
commit 05ecd361b3
2 changed files with 110 additions and 0 deletions

View File

@ -16,3 +16,7 @@ generate_arduino_firmware(uebung3-3
generate_arduino_firmware(uebung3-4 generate_arduino_firmware(uebung3-4
SKETCH uebung3-4 SKETCH uebung3-4
PROGRAMMER usbtinyisp) PROGRAMMER usbtinyisp)
generate_arduino_firmware(uebung3-5
SKETCH uebung3-5
PROGRAMMER usbtinyisp)

View File

@ -0,0 +1,106 @@
#include <Servo.h>
// 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);
}
}
}