Skip to content

Variablen im Arduino

In meinem ersten Blog Beitrag habe ich ein einfaches Programm geschrieben um die LED auf dem Arduino an- und auszuschalten. Das Programm ist recht simpel, zum Beispiel wird die Pin Nummer überall manuell eingesetzt. Das ist unschön, weil wenn sich die Pin Nummer ändert, muss man jede Stelle im Programm ändern.

Um das Programmieren zu vereinfachen, kann man die Pin Nummer in einer Variable ablegen.

 

Bei Code.org war das alles sehr einfach. Eine Variable erstellen, zwei Klicks, Fertig.

Beim Arduino muss man den Typ der Variable festlegen. Im Fall der Pin Nummer ist das eine ganze Zahl, ein sogenannter Integer.

Am besten legt man die Variablen am Anfang des Programms fest:

int ledPin = 13;
int delaytime = 0.5 * 1000;

Hier lege ich die Pin in der Variable "ledPin" ab. Die Wartezeit zwischen LED an und aus steht in "delaytime". Gleichzeitig berechne ich die Wartezeit indem ich eine halbe Sekunde (0.5) mit 1000 Millisekunden multipliziere. Das Programm sieht dann folgendermaßen aus:

int ledPin = 13;
int delaytime = 0.5 * 1000;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  digitalWrite(ledPin, HIGH);
  delay (delaytime);
  digitalWrite(ledPin, LOW);
  delay (delaytime);
}

 

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Form options