Servo-Ansteuerung mit Dateneingabe?

Hi,

ich habe mal eine Frage. Es gibt ja bei Arduino das Beispiel für die
Servo Ansteuerung.

Jetzt habe ich das Beispiel schon mal erweitert, das es für zwei Motoren
sein soll.

Die Daten für den Servo sollen seriell hinein kommen.

Wie schreibe ich das aber bei Arduino?
Ich bin total neu in Arduino und hab leider noch überhaupt keine
Erfahrungen. :frowning:

Der Code sieht erstmal folgendermaßen aus:

#include <Servo.h> 
 
Servo myservo_links;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
Servo myservo_rechts;
 
int pos_links = 0;    // variable to store the servo position 
int pos_rechts = 0;
 
void setup() 
{ 
  myservo_links.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo_rechts.attach(10);
} 
 
 
void loop() 
{ 
  
  ///////////////////////////////
  //Eingabe muss erfolgen
  //
  //////////////////////////////
  
  // linker Servo:
  
  for(pos_links = 0; pos_links < 180; pos_links += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo_links.write(pos_links);              // tell servo to go to position in variable 'pos_links' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos_links = 180; pos_links >= 1; pos_links-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo_links.write(pos_links);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 

// rechter Servo:  
  
  for(pos_rechts = 0; pos_rechts < 180; pos_rechts += 1)  // goes from 0 degrees to 180 degrees 
  {                                                       // in steps of 1 degree 
    myservo_rechts.write(pos_rechts);                     // tell servo to go to position in variable 'pos_rechts' 
    delay(15);                                            // waits 15ms for the servo to reach the position 
  } 
  for(pos_rechts = 180; pos_rechts >= 1; pos_rechts -= 1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo_rechts.write(pos_rechts);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  
}

hi

schade das noch keiner geschrieben hat.

ich habe den Code nun etwas umgewandelt, bei dem nun bei einem Eingang immer eine Flanke, bzw. ein bit mit "1" anliegen muss und solange soll sich der servo bewegen bis los gelassen wird.

#include <Servo.h> 
 
Servo myservo_links;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
Servo myservo_rechts;
 
int pos_links = 0;    // variable to store the servo position 
int pos_rechts = 0;

int tasterlinks_hoch;    
int tasterlinks_runter;
int tasterrechts_hoch;
int tasterrechts_runter;
 
void setup() 
{ 
  myservo_links.attach(9);  // attaches the servo on pin 9 to the servo object 
  myservo_rechts.attach(10);
 
} 
 
 
void loop() 
{ 
  
  ///////////////////////////////
  //Eingabe muss erfolgen
  
  //
  
  //
  //////////////////////////////

// linker Servo

    if (pos_links >= 0 || pos_links <= 180){
      if (tasterlinks_hoch == 1)
        {
          pos_links += 1;
          myservo_links.write(pos_links);
          delay(15);
        }
    }
    
    if (pos_links >= 0 || pos_links <= 180){
      if (tasterlinks_runter == 1)
        {
          pos_links -= 1;
          myservo_links.write(pos_links);
          delay(15);
        }
    }
    
    // rechter Servo
    
    if (pos_rechts >= 0 || pos_rechts <= 180){
      if (tasterrechts_hoch == 1)
        {
          pos_rechts += 1;
          myservo_rechts.write(pos_rechts);
          delay(15);
        }
    }
    
    if (pos_rechts >= 0 || pos_rechts <= 180){
      if (tasterrechts_runter == 1)
        {
          pos_rechts -= 1;
          myservo_rechts.write(pos_rechts);
          delay(15);
        }
    }

} //void loop

die:

int tasterlinks_hoch;
int tasterlinks_runter;
int tasterrechts_hoch;
int tasterrechts_runter;

müssten dann als Pin zugewiesen werden, welches registrieren soll, ob dort ein high an liegt.
und das würde ich gerne wissen wie?
Bei Qartus mit VHDL ist das Problem ziemlich einfach zu lösen :wink:
Ich habe auch schon etwas zu bitread gefunden, aber irgendwie komm ich damit nicht klar, bzw. kann ich dies nicht auf mein Problem anwenden.

Programmiert wird das Board "Mega 2560 R3"

Ich freu mich über jede Antwort.

Danke :slight_smile:

Entprellte Taster:
http://playground.arduino.cc/code/bounce

Mit digitalRead kannst du das auch per Hand machen, aber da wird das Entprellen dann unübersichtlicher.