10. November 2010
Gruppe: Sven, Tobi, Cedric Normaler DC Motor an einer H-Brücke Der Motor wechselt alle 2 Sekunden die Richtung#define ENABLE 12 #define DIR1 11 #define DIR2 10 int richtung = 1; void setup() { pinMode(ENABLE, OUTPUT); digitalWrite(ENABLE, HIGH); pinMode(DIR1, OUTPUT); digitalWrite(DIR1, HIGH); pinMode(DIR2, OUTPUT);DIR2DIR2 digitalWrite(DIR2, LOW); Serial.begin(9600); } void loop() { if (richtung == 1) { digitalWrite(DIR1, HIGH); digitalWrite(DIR2, LOW); richtung = 2; Serial.print("digitalRead = "); Serial.print(digitalRead(DIR1)); Serial.print(","); Serial.print(digitalRead(DIR2)); Serial.print("richtung = "); Serial.print(richtung); Serial.println(""); } else { digitalWrite(DIR1,LOW); digitalWrite(DIR2, HIGH); richtung = 1; Serial.print("digitalRead = "); Serial.print(digitalRead(DIR1)); Serial.print(","); Serial.print(digitalRead(DIR2)); Serial.print("richtung = "); Serial.print(richtung); Serial.println(""); } delay(2000); }Servomotor mit zwei Taster Mit dem linken Taster geht der Servomotor auf Position 0°. Mit dem rechten Taster auf die Position 90°.
#include <Servo.h> const int KNOB0 = 2; const int KNOB90 = 3; const int SERVO = 9; Servo myservo; // create servo object to control a servo int val; // variable to read the value from the analog pin void setup() { myservo.attach(SERVO); pinMode(KNOB0, INPUT); pinMode(KNOB90, INPUT); digitalWrite(KNOB0, HIGH); digitalWrite(KNOB90, HIGH); } void loop() { if(val == 89 && digitalRead(KNOB0) == 0){ val = 0; myservo.write(val); } else if(val == 0 && digitalRead(KNOB90) == 0){ val = 89; myservo.write(val); } }Steppermotor mit Treiberplatte Der Steppermotor (Schrittmotor) macht jede Sekunde einen Schritt nach Rechts. Nach 5 Sekunden stellt er sich auf die Ausgangsstellung zurück.
#define DIR 3 // Direction Pin #define STEP 2 // Constant #define BUT 4 // Direction Button int count = 0; void setup(){ pinMode(DIR, OUTPUT); // Direction is an output pinMode(STEP, OUTPUT); // Step is an output } void loop(){ if(count < 5){ digitalWrite(DIR, HIGH); // set direction stepOnce(); // Step count++; } else{ digitalWrite(DIR, LOW); // set direction while(count > 0){ stepOnce(); // Step count--; } } delay(1000); } // Function Definition --------------------------- // the specifications says the low to high transition // must be at least 1 microsecond void stepOnce(){ digitalWrite(STEP, HIGH); delay(1); digitalWrite(STEP, LOW); delay(1); }