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

[ES] Aufgaben 2.2 und 2.3 fertiggestellt

This commit is contained in:
2015-04-28 19:04:35 +02:00
parent 800f2283d3
commit d1d2842417
2 changed files with 252 additions and 19 deletions

View File

@ -1,23 +1,25 @@
// these variables describe the used hardware pins
// adjust them when you use other pins
// hardware pin for the LED
int ledPin = 7;
// hardware pin for button 1
int buttonOnePin = 5;
// hardware pin for button 2
int buttonTwoPin = 3;
// hardware pins
int pwm = 5;
int cw = 3;
int ccw = 4;
int standby = 2;
// this variables are affected by the buttonOne and buttonTwo actions
// represents the motor state, can be 0 (clockwise rotation), 1 (counter-clockwise rotation) or 2 (motor stopped)
int volatile motorState = 2;
// represents the power of the motor (range from 0 to 100)
int volatile motorPower = 0;
bool volatile motorPowerMaxReached = false;
// used to achieve a 1kHz frequency
// don't touch them
int compareValue = 60;
int rc = 10499;
// this variables are affected by the buttonOne and buttonTwo actions
// represents the motor state, can be 0 (clockwise rotation), 1 (anti-clockwise rotation) or 2 (motor stopped)
int volatile motorState = 0;
// represents the power of the motor (range from 0 to 100)
int volatile motorPower = 0;
bool volatile motorPowerMaxReached = false;
int buttonOnePin = 8;
int buttonTwoPin = 9;
// these values are used by the TC0_Handler
// do not use them at all
@ -49,6 +51,14 @@ void setup() {
// Configure button pins for input mode
pinMode(buttonOnePin, INPUT);
pinMode(buttonTwoPin, INPUT);
// configure pins
pinMode(pwm, OUTPUT);
pinMode(cw, OUTPUT);
pinMode(ccw, OUTPUT);
pinMode(standby, OUTPUT);
digitalWrite(standby, LOW);
analogWrite(pwm, motorPower);
// initialize serial port
Serial.begin(9600);
@ -58,11 +68,10 @@ void setup() {
* Loop function for main code
*/
void loop() {
// write the current counter to the LED pin
analogWrite(ledPin, counter);
// print the current LED value in the serial for debugging purposes
Serial.print("LED: ");
Serial.println(counter);
Serial.print("buttonTwo: ");
Serial.println(motorPower);
Serial.print("buttonOne: ");
Serial.println(motorState);
}
/**
@ -75,8 +84,40 @@ void buttonOneAction() {
motorState += 1;
}
else {
motorPower = 0;
motorState = 0;
}
turnaround();
}
void turnaround() {
int power = motorPower;
for (motorPower; motorPower > 0; motorPower--) {
analogWrite(pwm, motorPower);
}
switch (motorState) {
case 0:
digitalWrite(standby, HIGH);
digitalWrite(cw, true);
digitalWrite(ccw, false);
break;
case 1:
digitalWrite(standby, HIGH);
digitalWrite(cw, false);
digitalWrite(ccw, true);
break;
case 2:
digitalWrite(standby, LOW);
break;
}
if (motorState < 2) {
for (motorPower; motorPower < power; motorPower++) {
analogWrite(pwm, motorPower);
}
}
}
/**
@ -85,7 +126,7 @@ void buttonOneAction() {
* Has to be changed for the specific use case.
*/
void buttonTwoAction() {
if (!motorPowerMaxReached && motorPower < 100) {
if (!motorPowerMaxReached && motorPower < 254) {
motorPower += 1;
}
if (motorPowerMaxReached && motorPower > 0) {
@ -94,9 +135,10 @@ void buttonTwoAction() {
if (motorPowerMaxReached && motorPower == 0) {
motorPowerMaxReached = false;
}
if (!motorPowerMaxReached && motorPower == 100) {
if (!motorPowerMaxReached && motorPower == 254) {
motorPowerMaxReached = true;
}
analogWrite(pwm, motorPower);
}
/**