Beispiel-Programme und -Aufbauten
Blink-LED am Arduino
LED mit Taster am Arduino
/*
Button
Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.
The circuit:
- LED attached from pin 13 to ground
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
- Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Button
*/
int buttonPin = 2; // the number of the pushbutton pin
int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
LED-Lauflicht am Arduino
Ampel mit dem Arduino
Einfacher Beispiel Sketch zur Servo-Steuerung
#include <Servo.h>
Servo MeinServo;
int Position = 0;
void setup() {
// put your setup code here, to run once:
MeinServo.attach(9); // Servo hängt an Pin 9
}
void loop() {
// put your main code here, to run repeatedly:
MeinServo.write(Position);
delay(1000);
MeinServo.write(Position + 45);
delay(1000);
MeinServo.write(Position + 90);
delay(1000);
MeinServo.write(Position + 135);
delay(1000);
}
Erweiterter Beispiel-Sketch zu Servo-Steuerung
#include <Servo.h>
// Servo dem Arduino bekannt geben
Servo servoMotor;
int Sensor = A0;
int DigitalPin = 7;
int SensorWert = 0;
void setup() {
// put your setup code here, to run once:
pinMode(DigitalPin, OUTPUT);
Serial.begin(9600);
// Servo-Motor an Pin 9 angeschlossen
servoMotor.attach(9);
}
void loop() {
// put your main code here, to run repeatedly:
SensorWert = analogRead(Sensor);
// Trick 17 von Olaf
digitalWrite(DigitalPin, HIGH);
delay(SensorWert);
digitalWrite(DigitalPin, LOW);
delay(SensorWert);
Serial.println(SensorWert, DEC);
if (SensorWert < 100) {
servoMotor.write(45);
delay(1000);
servoMotor.write(0);
delay(1000);
}
servoMotor.write(90);
}
LED-Steuerung mit Poti
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 9; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup ()
{
pinMode (ledPin, OUTPUT);
Serial.begin (9600);
}
void loop ()
{
sensorValue = analogRead (sensorPin);
digitalWrite (ledPin, HIGH);
delay (sensorValue);
digitalWrite (ledPin, LOW);
delay (sensorValue);
Serial.println (sensorValue, DEC);
}