Meine Homepage

Arduino 9

03,09,2017 ein altes Projekt als Zusatz mit weiteren Funktionen!
Für mein Feldbahn-Betriebswerk benötige ich auch für einen Wasserkran einen Kohlenkran und die Drehscheibe eine Steuerung wo ich mit der Hilfe von Potis die Servowege verstellen kann.

Hier ist nun der neue Sketch für den zweiten Arduino Nano:
###################################################


// Steuerung Nr2 für das Feldbahn Bw1
#include <Servo.h>

Servo myservo;  // servo1 poti a5
Servo myservo1; // servo1 poti a6
Servo myservo2; // servo1 poti a7
Servo myservo3; // servo1 taster 1+2
Servo myservo4; // servo1 taster 3+4
Servo myservo5; // servo1 taster 5+6
int tasterPin1 = 2; //Taster1 an Pin 2 angeschlossen
int tasterPin2 = 4; //Taster2 an Pin 4 angeschlossen
int tasterStatus1, tasterStatus2;
int tasterPin3 = 7; //Taster3 an Pin 7 angeschlossen
int tasterPin4 = 8; //Taster4 an Pin 8 angeschlossen
int tasterStatus3, tasterStatus4;
int tasterPin5 = 14; //Taster5 an Pin 14 angeschlossen
int tasterPin6 = 15; //Taster6 an Pin 15 angeschlossen
int tasterStatus5, tasterStatus6;

int potpin = 5;  // analog pin5 used to connect the potentiometer
int val;    // variable0 to read the value from the analog pin

int potpin1 = 6;  // analog pin6 used to connect the potentiometer
int val1;    // variable1 to read the value from the analog pin

int potpin2 = 7;  // analog pin7 used to connect the potentiometer
int val2;    // variable2 to read the value from the analog pin

void setup() {
  myservo.attach(11);  // attaches the servo on pin 11 to the servo object
  myservo1.attach(10);  // attaches1 the servo on pin 12 to the servo object
  myservo2.attach(9);  // attaches2 the servo on pin 3 to the servo object
  myservo3.attach(5);  // attaches3 the servo on pin 5 to the servo object
  myservo4.attach(6);  // attaches4 the servo on pin 6 to the servo object
  myservo4.attach(3);  // attaches4 the servo on pin 6 to the servo object
 
  pinMode(tasterPin1, INPUT);
  pinMode(tasterPin2, INPUT);
 myservo3.attach(3); //Servomotor0 wurde an Pin 3 angeschlossen
  pinMode(tasterPin3, INPUT);
  pinMode(tasterPin4, INPUT);
 myservo4.attach(5); //Servomotor1 wurde an Pin 6 angeschlossen
  pinMode(tasterPin5, INPUT);
  pinMode(tasterPin6, INPUT);
 myservo5.attach(6); //Servomotor2 wurde an Pin 9 angeschlossen

 digitalWrite(tasterPin1, true);  // pullup Wiederstand einschalten
 digitalWrite(tasterPin2, true);  // pullup Wiederstand einschalten
 digitalWrite(tasterPin3, true);  // pullup Wiederstand einschalten
 digitalWrite(tasterPin4, true);  // pullup Wiederstand einschalten
 digitalWrite(tasterPin5, true);  // pullup Wiederstand einschalten
 digitalWrite(tasterPin6, true);  // pullup Wiederstand einschalten
}

void loop() {
  val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
  val = map(val, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo3.write(val);                  // servo1 pin 3
  delayMicroseconds(1);                           // waits for the servo to get there

 val1 = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
 val1 = map(val1, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo4.write(val1);                  // servo2 pin 5
  delayMicroseconds(1);                           // waits for the servo to get there

 val2 = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023)
 val2 = map(val2, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
    myservo5.write(val2);                  // servo3 pin 6
  delayMicroseconds(1);  // waits for the servo to get there

// servo0  
{
tasterStatus1 = digitalRead(tasterPin1);//servo3
tasterStatus2 = digitalRead(tasterPin2);

if (tasterStatus1 == 0)
{
myservo.write(157); //Dreht nach links
delayMicroseconds(20);
}

if (tasterStatus2 == 0)
{
myservo.write(0); //Dreht nach rechts
delayMicroseconds(20);
}

delayMicroseconds(10);
//servo1
tasterStatus3 = digitalRead(tasterPin3);//servo4
tasterStatus4 = digitalRead(tasterPin4);

if (tasterStatus3 == 0)
{
myservo1.write(157); //Dreht nach links
delayMicroseconds(20);
}

if (tasterStatus4 == 0)
{
myservo1.write(0); //Dreht nach rechts
delayMicroseconds(20);
}
delayMicroseconds(10);
// servo2
tasterStatus5 = digitalRead(tasterPin5);//servo5
tasterStatus6 = digitalRead(tasterPin6);
if (tasterStatus5 == 0)
{
myservo2.write(157); //Dreht nach links
delayMicroseconds(20);
}
if (tasterStatus6 == 0)
{
myservo2.write(0); //Dreht nach rechts
delayMicroseconds(20);
}
delayMicroseconds(10);
}
}

--------------------------------------------------------------------------------------------
30,12,2020 Es gibt zum Glück etwas für die Servoweichenantriebe der LGB Bahn!
//ServoWeichenSteuerung ARDUINO 30,12,2020
#include <MobaTools.h>

// Die Taster müssen so angeschlossen sein, dass der Eingang bei gedrücktem
// Taster auf LOW (= 0/GRND) geht.
const int tasterPin1 = 20;    //Taster1 Pin 14
const int tasterPin2 = 21;    //Taster2  Pin 15
const int servoPin1 =  2;  // Anschluß für den Servo
const int tasterPin3 = 22;    //Taster1 Pin 16
const int tasterPin4 = 23;    //Taster2  Pin 17
const int servoPin2 =  3;  // Anschluß für den Servo
const int tasterPin5 = 24;    //Taster1 Pin 18
const int tasterPin6 = 25;    //Taster2  Pin 19
const int servoPin3 =  4;  // Anschluß für den Servo
const int tasterPin7 = 26;    //Taster1 Pin 14
const int tasterPin8 = 27;    //Taster2  Pin 15
const int servoPin4 =  5;  // Anschluß für den Servo
const int tasterPin9 = 28;    //Taster1 Pin 16
const int tasterPin10 = 29;    //Taster2  Pin 17
const int servoPin5 =  6;  // Anschluß für den Servo
const int tasterPin11 = 30;    //Taster1 Pin 18
const int tasterPin12 = 31;    //Taster2  Pin 19
const int servoPin6 =  7;  // Anschluß für den Servo
const int tasterPin13 = 32;    //Taster1 Pin 14
const int tasterPin14 = 33;    //Taster2  Pin 15
const int servoPin7 =  8;  // Anschluß für den Servo
const int tasterPin15 = 34;    //Taster1 Pin 16
const int tasterPin16 = 35;    //Taster2  Pin 17
const int servoPin8 =  9;  // Anschluß für den Servo
const int tasterPin17 = 36;    //Taster1 Pin 18
const int tasterPin18 = 37;    //Taster2  Pin 19
const int servoPin9 =  10;  // Anschluß für den Servo
const int tasterPin19 = 38;    //Taster1 Pin 14
const int tasterPin20 = 39;    //Taster2  Pin 15
const int servoPin10 =  11;  // Anschluß für den Servo
const int tasterPin21 = 40;    //Taster1 Pin 16
const int tasterPin22 = 41;    //Taster2  Pin 17
const int servoPin11 =  12;  // Anschluß für den Servo

// bei Werten, die sich im Programm nie verändern, sollte immer 'const' vorangestellt
// werden


int tasterStatus1, tasterStatus2;
Servo8 meinServo;

int tasterStatus3, tasterStatus4;
Servo8 meinServo1;

int tasterStatus5, tasterStatus6;
Servo8 meinServo2;

int tasterStatus7, tasterStatus8;
Servo8 meinServo3;

int tasterStatus9, tasterStatus10;
Servo8 meinServo4;

int tasterStatus11, tasterStatus12;
Servo8 meinServo5;

int tasterStatus13, tasterStatus14;
Servo8 meinServo6;

int tasterStatus15, tasterStatus16;
Servo8 meinServo7;

int tasterStatus17, tasterStatus18;
Servo8 meinServo8;

int tasterStatus19, tasterStatus20;
Servo8 meinServo9;

int tasterStatus21, tasterStatus22;
Servo8 meinServo10;


void setup() {
    pinMode(tasterPin1, INPUT_PULLUP); // so ist kein externer pullup Widerstand am
    pinMode(tasterPin2, INPUT_PULLUP); // Taster erforderlich
    
    meinServo.attach(servoPin1); //Servo an Pin 2
    meinServo.setSpeed( 15 );    // Verfahrgeschwindigkeit einstellen
   //
    pinMode(tasterPin3, INPUT_PULLUP); // so ist kein externer pullup Widerstand am
    pinMode(tasterPin4, INPUT_PULLUP); // Taster erforderlich
    
    meinServo1.attach(servoPin2); //Servo an Pin 3
    meinServo1.setSpeed( 15 );    // Verfahrgeschwindigkeit einstellen
  //
    pinMode(tasterPin5, INPUT_PULLUP); // so ist kein externer pullup Widerstand am
    pinMode(tasterPin6, INPUT_PULLUP); // Taster erforderlich
    
    meinServo2.attach(servoPin3); //Servo an Pin 4
    meinServo2.setSpeed( 15 );    // Verfahrgeschwindigkeit einstellen
    pinMode(tasterPin7, INPUT_PULLUP); // so ist kein externer pullup Widerstand am
    pinMode(tasterPin8, INPUT_PULLUP); // Taster erforderlich
    
    meinServo3.attach(servoPin4); //Servo an Pin 5
    meinServo3.setSpeed( 15 );    // Verfahrgeschwindigkeit einstellen
   //
    pinMode(tasterPin9, INPUT_PULLUP); // so ist kein externer pullup Widerstand am
    pinMode(tasterPin10, INPUT_PULLUP); // Taster erforderlich
    
    meinServo4.attach(servoPin5); //Servo an Pin 6
    meinServo4.setSpeed( 15 );    // Verfahrgeschwindigkeit einstellen
  //
    pinMode(tasterPin11, INPUT_PULLUP); // so ist kein externer pullup Widerstand am
    pinMode(tasterPin12, INPUT_PULLUP); // Taster erforderlich
    
    meinServo5.attach(servoPin6); //Servo an Pin 7
    meinServo5.setSpeed( 15 );    // Verfahrgeschwindigkeit einstellen
    pinMode(tasterPin13, INPUT_PULLUP); // so ist kein externer pullup Widerstand am
    pinMode(tasterPin14, INPUT_PULLUP); // Taster erforderlich
    
    meinServo6.attach(servoPin7); //Servo an Pin 8
    meinServo6.setSpeed( 15 );    // Verfahrgeschwindigkeit einstellen
   //
    pinMode(tasterPin15, INPUT_PULLUP); // so ist kein externer pullup Widerstand am
    pinMode(tasterPin16, INPUT_PULLUP); // Taster erforderlich
    
    meinServo7.attach(servoPin8); //Servo an Pin 9
    meinServo7.setSpeed( 15 );    // Verfahrgeschwindigkeit einstellen
  //
    pinMode(tasterPin17, INPUT_PULLUP); // so ist kein externer pullup Widerstand am
    pinMode(tasterPin18, INPUT_PULLUP); // Taster erforderlich
    
    meinServo9.attach(servoPin9); //Servo an Pin 10
    meinServo9.setSpeed( 15 );    // Verfahrgeschwindigkeit einstellen
    pinMode(tasterPin19, INPUT_PULLUP); // so ist kein externer pullup Widerstand am
    pinMode(tasterPin20, INPUT_PULLUP); // Taster erforderlich
    
    meinServo10.attach(servoPin10); //Servo an Pin 11
    meinServo10.setSpeed( 15 );    // Verfahrgeschwindigkeit einstellen
   //
 
}

void loop() {
    tasterStatus1 = digitalRead(tasterPin1);
    tasterStatus2 = digitalRead(tasterPin2);

    if (tasterStatus1 == LOW && meinServo.moving() == 0 ) {
        meinServo.attach(2);
        meinServo.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus2 == LOW && meinServo.moving() == 0) {
        meinServo.attach(2);
        meinServo.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                          // Weiche Nr.1 Pin2 Schalterpin 20 21
     tasterStatus3 = digitalRead(tasterPin3);
     tasterStatus4 = digitalRead(tasterPin4);

    if (tasterStatus3 == LOW && meinServo.moving() == 0 ) {
        meinServo1.attach(3);
        meinServo1.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus4 == LOW && meinServo.moving() == 0) {
        meinServo1.attach(3);
        meinServo1.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                         // Weiche Nr.2 Pin3 Schalterpin 22 23
     tasterStatus5 = digitalRead(tasterPin5);
     tasterStatus6 = digitalRead(tasterPin6);

    if (tasterStatus5 == LOW && meinServo.moving() == 0 ) {
        meinServo2.attach(4);
        meinServo2.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus6 == LOW && meinServo.moving() == 0) {
        meinServo2.attach(4);
        meinServo2.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                          // Weiche Nr. Pin4 Schalterpin 24 25
    tasterStatus7 = digitalRead(tasterPin7);
    tasterStatus8 = digitalRead(tasterPin8);

    if (tasterStatus7 == LOW && meinServo.moving() == 0 ) {
        meinServo3.attach(5);
        meinServo3.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus7 == LOW && meinServo.moving() == 0) {
        meinServo3.attach(5);
        meinServo3.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                        // Weiche Nr. Pin5 Schalterpin 26 27
    tasterStatus9 = digitalRead(tasterPin9);
    tasterStatus10 = digitalRead(tasterPin10);

    if (tasterStatus9 == LOW && meinServo.moving() == 0 ) {
        meinServo4.attach(6);
        meinServo4.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus10 == LOW && meinServo.moving() == 0) {
        meinServo4.attach(6);
        meinServo4.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                        // Weiche Nr. Pin6 Schalterpin 28 29
    tasterStatus11 = digitalRead(tasterPin11);
    tasterStatus12 = digitalRead(tasterPin12);

    if (tasterStatus11 == LOW && meinServo.moving() == 0 ) {
        meinServo5.attach(7);
        meinServo5.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus12 == LOW && meinServo.moving() == 0) {
        meinServo5.attach(7);
        meinServo5.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                        // Weiche Nr. Pin7 Schalterpin 30 31
    tasterStatus13 = digitalRead(tasterPin13);
    tasterStatus14 = digitalRead(tasterPin14);

    if (tasterStatus13 == LOW && meinServo.moving() == 0 ) {
        meinServo6.attach(8);
        meinServo6.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus14 == LOW && meinServo.moving() == 0) {
        meinServo6.attach(8);
        meinServo6.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                       // Weiche Nr Pin8 Schalterpin 32 33
    tasterStatus15 = digitalRead(tasterPin15);
    tasterStatus16 = digitalRead(tasterPin16);

    if (tasterStatus15 == LOW && meinServo.moving() == 0 ) {
        meinServo7.attach(9);
        meinServo7.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus16 == LOW && meinServo.moving() == 0) {
        meinServo7.attach(9);
        meinServo7.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                      // Weiche Nr. Pin9 Schalterpin 34 35
    tasterStatus17 = digitalRead(tasterPin17);
    tasterStatus18 = digitalRead(tasterPin18);

    if (tasterStatus17 == LOW && meinServo.moving() == 0 ) {
        meinServo8.attach(10);
        meinServo8.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus18 == LOW && meinServo.moving() == 0) {
        meinServo8.attach(10);
        meinServo8.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                     // Weiche Nr. Pin10 Schalterpin 36 37
    tasterStatus19 = digitalRead(tasterPin19);
    tasterStatus20 = digitalRead(tasterPin20);

    if (tasterStatus19 == LOW && meinServo.moving() == 0 ) {
        meinServo9.attach(11);
        meinServo9.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus20 == LOW && meinServo.moving() == 0) {
        meinServo9.attach(11);
        meinServo9.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                      // Weiche Nr. Pin11 Schalterpin 38 39
    tasterStatus21 = digitalRead(tasterPin21);
    tasterStatus22 = digitalRead(tasterPin22);

    if (tasterStatus21 == LOW && meinServo.moving() == 0 ) {
        meinServo10.attach(12);
        meinServo10.write(0); //wird langsam  drehen
        delay(500);
    meinServo.detach();
    }

    if (tasterStatus22 == LOW && meinServo.moving() == 0) {
        meinServo10.attach(12);
        meinServo10.write(175); //wird langsam drehen
        delay(500);
    meinServo.detach();
    }                                       // Weiche Nr. Pin12 Schalterpin 40 41        
   
}

------------------------------------------------------------------------------------------
Ist etwas groß geworden aber ich kann nun damit 10 Weichen ansteuern da gibt es auch noch viel zu löten!

 
Zurück zur Startseite
Diese Webseite wurde kostenlos mit Homepage-Baukasten.de erstellt. Willst du auch eine eigene Webseite?
Gratis anmelden