Hausaufgaben8 Lösung: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Die Seite wurde neu angelegt: „ ---- → zurück zum Hauptartikel: Termin 8: Joystick einlesen“ |
Keine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
== Quelltext == | |||
<div style="width:1200px; height:300px; overflow:auto; border: 2px solid #088"> | |||
<pre> | |||
/* Servo Bibliothek bereitstellen */ | |||
#include <Servo.h> | |||
Servo usServo; | |||
/* Globale Konstanten */ | |||
static const byte SERVO_PORT_u8 = 9; // Port des Servo Motors | |||
static const int COM_GESCHW_s16 = 9600; // Baude | |||
/* Deklaration der Joystick Ports in Fahrtrichtung */ | |||
#define JOYSTICK_RECHTS A1 | |||
#define JOYSTICK_UNTEN A2 | |||
#define JOYSTICK_OBEN A3 | |||
#define JOYSTICK_EINGABE A4 | |||
#define JOYSTICK_LINKS A5 | |||
/* Globale Variablen */ | |||
boolean JoystickFreigabe_bit = 1; // Entprellen des Joysticks | |||
int ServoPosition_s16 = 90; // Mittelstellung | |||
void setup(void) /* Einmalig ausgeführt */ | |||
{ | |||
/* Der Joystick wird initialisiert */ | |||
pinMode(JOYSTICK_OBEN, INPUT); | |||
digitalWrite(JOYSTICK_OBEN, HIGH); | |||
pinMode(JOYSTICK_UNTEN, INPUT); | |||
digitalWrite(JOYSTICK_UNTEN, HIGH); | |||
pinMode(JOYSTICK_LINKS, INPUT); | |||
digitalWrite(JOYSTICK_LINKS, HIGH); | |||
pinMode(JOYSTICK_RECHTS, INPUT); | |||
digitalWrite(JOYSTICK_RECHTS, HIGH); | |||
pinMode(JOYSTICK_EINGABE, INPUT); | |||
digitalWrite(JOYSTICK_EINGABE, HIGH); | |||
Serial.begin(COM_GESCHW_s16); // Start des seriellen Monitors e | |||
usServo.attach(SERVO_PORT_u8); // Servo einbinden | |||
usServo.write(ServoPosition_s16); // Der Servo soll sich an die Startposition drehen | |||
Serial.println("SETUP beendet"); | |||
Serial.println("Bitte Joystick bedienen (L, R, Eingabe)."); | |||
} | |||
void loop(void) /* Zyklische Abfrage */ | |||
{ | |||
/* Abfrage des Joysticks */ | |||
delay(1); | |||
if((digitalRead(JOYSTICK_LINKS) == LOW) || (digitalRead(JOYSTICK_RECHTS) == LOW) || (digitalRead(JOYSTICK_EINGABE) == LOW)) | |||
{ | |||
if(JoystickFreigabe_bit == true) | |||
{ | |||
delay(10); //10 ms warten - Taster entprellen | |||
if((digitalRead(JOYSTICK_LINKS) == LOW) || (digitalRead(JOYSTICK_RECHTS) == LOW) || (digitalRead(JOYSTICK_EINGABE) == LOW)) | |||
{ | |||
JoystickFreigabe_bit = false; | |||
if(digitalRead(JOYSTICK_LINKS) == LOW){ | |||
Serial.println("Joystick: LINKS gedrückt"); | |||
ServoPosition_s16=ServoPosition_s16+5; | |||
if (ServoPosition_s16>180){ | |||
ServoPosition_s16 = 180; // Oberes Limit erreicht | |||
} | |||
} | |||
else if(digitalRead(JOYSTICK_RECHTS) == LOW){ | |||
Serial.println("Joystick: RECHTS gedrückt"); | |||
ServoPosition_s16=ServoPosition_s16-5; | |||
if (ServoPosition_s16<0){ | |||
ServoPosition_s16 = 0; // Unteres Limit erreicht | |||
} | |||
} | |||
else if(digitalRead(JOYSTICK_EINGABE) == LOW){ | |||
Serial.println("Joystick: EINGABE gedrückt"); | |||
ServoPosition_s16 = 90; | |||
} | |||
usServo.write(ServoPosition_s16); | |||
Serial.print("ServoPosition_s16: "); | |||
Serial.println(ServoPosition_s16); | |||
} | |||
} | |||
} | |||
else { | |||
JoystickFreigabe_bit = true; // Taster wieder freigeben | |||
} | |||
} | |||
</pre> | |||
</div> | |||
---- | ---- | ||
→ zurück zum Hauptartikel: [[AlphaBot:_Joystick_einlesen|Termin 8: Joystick einlesen]] | → zurück zum Hauptartikel: [[AlphaBot:_Joystick_einlesen|Termin 8: Joystick einlesen]] | ||
Aktuelle Version vom 8. Mai 2022, 08:35 Uhr
Quelltext
/* Servo Bibliothek bereitstellen */
#include <Servo.h>
Servo usServo;
/* Globale Konstanten */
static const byte SERVO_PORT_u8 = 9; // Port des Servo Motors
static const int COM_GESCHW_s16 = 9600; // Baude
/* Deklaration der Joystick Ports in Fahrtrichtung */
#define JOYSTICK_RECHTS A1
#define JOYSTICK_UNTEN A2
#define JOYSTICK_OBEN A3
#define JOYSTICK_EINGABE A4
#define JOYSTICK_LINKS A5
/* Globale Variablen */
boolean JoystickFreigabe_bit = 1; // Entprellen des Joysticks
int ServoPosition_s16 = 90; // Mittelstellung
void setup(void) /* Einmalig ausgeführt */
{
/* Der Joystick wird initialisiert */
pinMode(JOYSTICK_OBEN, INPUT);
digitalWrite(JOYSTICK_OBEN, HIGH);
pinMode(JOYSTICK_UNTEN, INPUT);
digitalWrite(JOYSTICK_UNTEN, HIGH);
pinMode(JOYSTICK_LINKS, INPUT);
digitalWrite(JOYSTICK_LINKS, HIGH);
pinMode(JOYSTICK_RECHTS, INPUT);
digitalWrite(JOYSTICK_RECHTS, HIGH);
pinMode(JOYSTICK_EINGABE, INPUT);
digitalWrite(JOYSTICK_EINGABE, HIGH);
Serial.begin(COM_GESCHW_s16); // Start des seriellen Monitors e
usServo.attach(SERVO_PORT_u8); // Servo einbinden
usServo.write(ServoPosition_s16); // Der Servo soll sich an die Startposition drehen
Serial.println("SETUP beendet");
Serial.println("Bitte Joystick bedienen (L, R, Eingabe).");
}
void loop(void) /* Zyklische Abfrage */
{
/* Abfrage des Joysticks */
delay(1);
if((digitalRead(JOYSTICK_LINKS) == LOW) || (digitalRead(JOYSTICK_RECHTS) == LOW) || (digitalRead(JOYSTICK_EINGABE) == LOW))
{
if(JoystickFreigabe_bit == true)
{
delay(10); //10 ms warten - Taster entprellen
if((digitalRead(JOYSTICK_LINKS) == LOW) || (digitalRead(JOYSTICK_RECHTS) == LOW) || (digitalRead(JOYSTICK_EINGABE) == LOW))
{
JoystickFreigabe_bit = false;
if(digitalRead(JOYSTICK_LINKS) == LOW){
Serial.println("Joystick: LINKS gedrückt");
ServoPosition_s16=ServoPosition_s16+5;
if (ServoPosition_s16>180){
ServoPosition_s16 = 180; // Oberes Limit erreicht
}
}
else if(digitalRead(JOYSTICK_RECHTS) == LOW){
Serial.println("Joystick: RECHTS gedrückt");
ServoPosition_s16=ServoPosition_s16-5;
if (ServoPosition_s16<0){
ServoPosition_s16 = 0; // Unteres Limit erreicht
}
}
else if(digitalRead(JOYSTICK_EINGABE) == LOW){
Serial.println("Joystick: EINGABE gedrückt");
ServoPosition_s16 = 90;
}
usServo.write(ServoPosition_s16);
Serial.print("ServoPosition_s16: ");
Serial.println(ServoPosition_s16);
}
}
}
else {
JoystickFreigabe_bit = true; // Taster wieder freigeben
}
}
→ zurück zum Hauptartikel: Termin 8: Joystick einlesen