Arduino: Variablen und Konstanten: Unterschied zwischen den Versionen

Aus HSHL Mechatronik
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Zeile 10: Zeile 10:


== Quelltext ==
== Quelltext ==
<div style="width:1200px; height:300px; overflow:auto; border: 2px solid #088">
<pre>
/* Deklaration */
byte Variable_u8;          // 0..255 Variable
const byte KONSTANTE = 0;  // 0..255 Konstante
void setup() {
  // Wird einmalig beim Start ausgeführt
  Serial.begin(9600);      // Serielle Übertragung mit 9600 Bit/s starten
  Variable_u8 = 0;
  Serial.println("SETUP");
}
void loop() {
  // Wird zyklisch ausgeführt
  float Variable_s32 = 0; // lokale Hilfsvariable
 
  //Variable_u8++;
  //Variable_u8 = Addiere(Variable_u8,5);
  Variable_u8 = Subtrahiere(Variable_u8,5);
  //Variable_u8 = Variable_u8 + 5;
  Variable_s32 =(float)Variable_u8/2;
  Serial.println(Variable_u8);
  Serial.println(Variable_s32);
  delay(1000); // Pause in ms (0,001 s)
}
byte Addiere(byte In1_u8,byte In2_u8){
  byte Ergebnis_u8=0;          // Hilfsvarianble anlegen
  Ergebnis_u8 = In1_u8+In2_u8; // +
  return Ergebnis_u8;          // Rückgabe des Ergebnisses
}
byte Subtrahiere(byte In1_u8,byte In2_u8){
  byte Ergebnis_u8=0;
  Ergebnis_u8 = In1_u8-In2_u8; // -
  return Ergebnis_u8;
}
</pre>
</div>


== Hausaufgaben bis zum 3. Termin ==
== Hausaufgaben bis zum 3. Termin ==

Version vom 15. März 2022, 10:35 Uhr

Autor: Prof. Dr.-Ing. Schneider

Inhalt des ersten Termins

  1. Vorstellung der Arduino Sprach-Referenz
  2. Datentypen des Arduino (byte, int, float)
  3. Globale und lokale Variablen
  4. Konstanten
  5. Funktionen
  6. #include

Quelltext

/* Deklaration */
byte Variable_u8;          // 0..255 Variable
const byte KONSTANTE = 0;  // 0..255 Konstante

void setup() {
  // Wird einmalig beim Start ausgeführt
  Serial.begin(9600);      // Serielle Übertragung mit 9600 Bit/s starten
  Variable_u8 = 0;
  Serial.println("SETUP");
}

void loop() {
  // Wird zyklisch ausgeführt
  float Variable_s32 = 0; // lokale Hilfsvariable
  
  //Variable_u8++;
  //Variable_u8 = Addiere(Variable_u8,5);
  Variable_u8 = Subtrahiere(Variable_u8,5);
  //Variable_u8 = Variable_u8 + 5;
  Variable_s32 =(float)Variable_u8/2;
  Serial.println(Variable_u8);
  Serial.println(Variable_s32);
  delay(1000); // Pause in ms (0,001 s)
}
byte Addiere(byte In1_u8,byte In2_u8){
  byte Ergebnis_u8=0;          // Hilfsvarianble anlegen
  Ergebnis_u8 = In1_u8+In2_u8; // +
  return Ergebnis_u8;          // Rückgabe des Ergebnisses
}
byte Subtrahiere(byte In1_u8,byte In2_u8){
  byte Ergebnis_u8=0;
  Ergebnis_u8 = In1_u8-In2_u8; // -
  return Ergebnis_u8;
}

Hausaufgaben bis zum 3. Termin

  1. Schreibe eine Funktion float Dividiere(float In1_s32, float In2_s32), die beide übergebene Argumente (In1_s32, In2_s32) dividiert und das Ergebnis als float zurückgibt.
  2. Teste die Funktion mit beliebigen Zahlen (Dividiere(100, 3)).
  3. Binde den ArduinoLibOrdner ein. Die Anleitung findest Du hier.

Musterlösung


→ zurück zum Hauptartikel: Projekt Alf