Komisches Verhalten --> Erste Funktion in void setup() wird immer 2x aufgerufen.

Hallo zusammen

Ich habe ein seltsames Problem auf:

Port Type Board Name FQBN Core
/dev/ttyACM0 Serial Port (USB) Arduino Mega or Mega 2560 arduino:avr:mega arduino:avr

Die erste Funktion wird immer 2x aufgerufen in diesem Fall :

Go.advance(SPEED);
     delay(1000);
     Go.stop();
     delay(1000);

Wenn ich es auskommentiere:

/*
Go.advance(SPEED);
     delay(1000);
     Go.stop();
     delay(1000);
*/

läuft einfach die nächste Funktion -> Go.back zwei mal

An was kann das liegen ?

#define SPEED 200
#define TURN_SPEED 160

#include "MyWheels.h"

MyWheels Go;

void setup()
{
  Go.begin();

Go.advance(SPEED);
     delay(1000);
     Go.stop();
     delay(1000);

Go.back(SPEED);
      delay(1000);
      Go.stop();
      delay(1000);

Go.left_turn(TURN_SPEED);
      delay(1000);
      Go.stop();
      delay(1000);

Go.right_turn(TURN_SPEED);
     delay(1000);
     Go.stop();
     delay(1000);

Go.right(SPEED); //right shift
     delay(1000);
     Go.stop();
     delay(1000);

/* The same as Go.right
Go.right_shift(SPEED,SPEED,SPEED,SPEED); //right shift
     delay(1000);
     Go.stop();
     delay(1000);
*/


Go.left(SPEED); //left shift
     delay(1000);
     Go.stop();
     delay(1000);

/* The same as Go.left
Go.left_shift(SPEED,SPEED,SPEED,SPEED); //left shift
     delay(1000);
     Go.stop();
     delay(1000);
*/

Go.right_diag_advance(SPEED);
     delay(1000);
     Go.stop();
	 delay(1000);


/* Same as Go.right_shift
   Go.right_shift(0,SPEED,0,SPEED);
        delay(1000);
        Go.stop();
   	 delay(1000);
*/

Go.right_diag_back(SPEED);
     delay(1000);
     Go.stop();
	 delay(1000);

/* Same as Go.right_shift
   Go.right_shift(SPEED,0,SPEED,0);
        delay(1000);
        Go.stop();
   	 delay(1000);
*/


Go.left_diag_advance(SPEED);
     delay(1000);
     Go.stop();
	 delay(1000);


/* Same as Go.left_shift

   Go.left_shift(SPEED,0,SPEED,0);
        delay(1000);
        Go.stop();
   	 delay(1000);
*/

Go.left_diag_back(SPEED);
     delay(1000);
     Go.stop();
	 delay(1000);


/* Same as Go.left_shift

   Go.left_shift(0,SPEED,0,SPEED);
        delay(1000);
        Go.stop();
   	 delay(1000);
*/


Go.turn_clockwise(SPEED);
     delay(1000);
     Go.stop();
	 delay(1000);

Go.turn_counter_clockwise(SPEED);
     delay(1000);
     Go.stop();
	 delay(1000);

}

void loop(){

}
/*
  MyWheels.h
  Released into the public domain.
*/

#ifndef MyWheels_h
#define MyWheels_h

class MyWheels  {

#define speedPinR 9   //  Front Wheel PWM pin connect Right MODEL-X ENA
#define RightMotorDirPin1  22    //Front Right Motor direction pin 1 to Right MODEL-X IN1  (K1)
#define RightMotorDirPin2  24   //Front Right Motor direction pin 2 to Right MODEL-X IN2   (K1)
#define LeftMotorDirPin1  26    //Front Left Motor direction pin 1 to Right MODEL-X IN3 (K3)
#define LeftMotorDirPin2  28   //Front Left Motor direction pin 2 to Right MODEL-X IN4 (K3)
#define speedPinL 10   //  Front Wheel PWM pin connect Right MODEL-X ENB

#define speedPinRB 11   //  Rear Wheel PWM pin connect Left MODEL-X ENA
#define RightMotorDirPin1B  5    //Rear Right Motor direction pin 1 to Left  MODEL-X IN1 ( K1)
#define RightMotorDirPin2B 6    //Rear Right Motor direction pin 2 to Left  MODEL-X IN2 ( K1)
#define LeftMotorDirPin1B 7    //Rear Left Motor direction pin 1 to Left  MODEL-X IN3  (K3)
#define LeftMotorDirPin2B 8  //Rear Left Motor direction pin 2 to Left  MODEL-X IN4 (K3)
#define speedPinLB 12    //  Rear Wheel PWM pin connect Left MODEL-X ENB

private:

public:

/*motor control*/
void advance(int speed){
   RL_fwd(speed);
   RR_fwd(speed);
   FR_fwd(speed);
   FL_fwd(speed);
}

void back(int speed){
   RL_bck(speed);
   RR_bck(speed);
   FR_bck(speed);
   FL_bck(speed);
}

void left(int speed) {
  FL_fwd(speed);
  RL_bck(speed);
  RR_fwd(speed);
  FR_bck(speed);
}

void right(int speed){
   FL_bck(speed);
   RL_fwd(speed);
   RR_bck(speed);
   FR_fwd(speed);
}

void left_shift(int speed_fl_fwd,int speed_rl_bck ,int speed_rr_fwd,int speed_fr_bck) {
  FL_fwd(speed_fl_fwd);
  RL_bck(speed_rl_bck);
  RR_fwd(speed_rr_fwd);
  FR_bck(speed_fr_bck);
}

void right_shift(int speed_fl_bck,int speed_rl_fwd ,int speed_rr_bck,int speed_fr_fwd){
   FL_bck(speed_fl_bck);
   RL_fwd(speed_rl_fwd);
   RR_bck(speed_rr_bck);
   FR_fwd(speed_fr_fwd);
}

void left_diag_advance(int speed) {
  FL_fwd(speed);
  RL_bck(0);
  RR_fwd(speed);
  FR_bck(0);
}

void left_diag_back(int speed) {
  FL_fwd(0);
  RL_bck(speed);
  RR_fwd(0);
  FR_bck(speed);
}

void right_diag_advance(int speed) {
   FL_bck(0);
   RL_fwd(speed);
   RR_bck(0);
   FR_fwd(speed);
}

void right_diag_back(int speed) {
   FL_bck(speed);
   RL_fwd(0);
   RR_bck(speed);
   FR_fwd(0);
}


void right_turn(int speed){
   RL_bck(0);
   RR_fwd(speed);
   FR_fwd(speed);
   FL_bck(0);
}
void left_turn(int speed){
   RL_fwd(speed);
   RR_bck(0);
   FR_bck(0);
   FL_fwd(speed);
}
void left_back(int speed){
   RL_fwd(0);
   RR_bck(speed);
   FR_bck(speed);
   FL_fwd(0);
}
void right_back(int speed){
   RL_bck(speed);
   RR_fwd(0);
   FR_fwd(0);
   FL_bck(speed);
}

void turn_counter_clockwise(int speed){
   RL_fwd(speed);
   RR_bck(speed);
   FR_bck(speed);
   FL_fwd(speed);
}
void turn_clockwise(int speed){
   RL_bck(speed);
   RR_fwd(speed);
   FR_fwd(speed);
   FL_bck(speed);
}

void FR_fwd(int speed)  //front-right wheel forward turn
{
  digitalWrite(RightMotorDirPin1,HIGH);
  digitalWrite(RightMotorDirPin2,LOW);
  analogWrite(speedPinR,speed);
}

void FR_bck(int speed) // front-right wheel backward turn
{
  digitalWrite(RightMotorDirPin1,LOW);
  digitalWrite(RightMotorDirPin2,HIGH);
  analogWrite(speedPinR,speed);
}

void FL_fwd(int speed) // front-left wheel forward turn
{
  digitalWrite(LeftMotorDirPin1,HIGH);
  digitalWrite(LeftMotorDirPin2,LOW);
  analogWrite(speedPinL,speed);
}

void FL_bck(int speed) // front-left wheel backward turn
{
  digitalWrite(LeftMotorDirPin1,LOW);
  digitalWrite(LeftMotorDirPin2,HIGH);
  analogWrite(speedPinL,speed);
}

void RR_fwd(int speed)  //rear-right wheel forward turn
{
  digitalWrite(RightMotorDirPin1B, HIGH);
  digitalWrite(RightMotorDirPin2B,LOW);
  analogWrite(speedPinRB,speed);
}
void RR_bck(int speed)  //rear-right wheel backward turn
{
  digitalWrite(RightMotorDirPin1B, LOW);
  digitalWrite(RightMotorDirPin2B,HIGH);
  analogWrite(speedPinRB,speed);
}
void RL_fwd(int speed)  //rear-left wheel forward turn
{
  digitalWrite(LeftMotorDirPin1B,HIGH);
  digitalWrite(LeftMotorDirPin2B,LOW);
  analogWrite(speedPinLB,speed);
}
void RL_bck(int speed)    //rear-left wheel backward turn
{
  digitalWrite(LeftMotorDirPin1B,LOW);
  digitalWrite(LeftMotorDirPin2B,HIGH);
  analogWrite(speedPinLB,speed);
}

void stop()    //Stop
{
  analogWrite(speedPinLB,0);
  analogWrite(speedPinRB,0);
  analogWrite(speedPinL,0);
  analogWrite(speedPinR,0);
}


//Pins initialize
void begin()
{
  pinMode(RightMotorDirPin1, OUTPUT);
  pinMode(RightMotorDirPin2, OUTPUT);
  pinMode(speedPinL, OUTPUT);

  pinMode(LeftMotorDirPin1, OUTPUT);
  pinMode(LeftMotorDirPin2, OUTPUT);
  pinMode(speedPinR, OUTPUT);
  pinMode(RightMotorDirPin1B, OUTPUT);
  pinMode(RightMotorDirPin2B, OUTPUT);
  pinMode(speedPinLB, OUTPUT);

  pinMode(LeftMotorDirPin1B, OUTPUT);
  pinMode(LeftMotorDirPin2B, OUTPUT);
  pinMode(speedPinRB, OUTPUT);
  stop();

}

};

#endif

ardudummy:
Ich habe ein seltsames Problem auf:
Port Type Board Name FQBN Core
/dev/ttyACM0 Serial Port (USB) Arduino Mega or Mega 2560 arduino:avr:mega arduino:avr

Die erste Funktion wird immer 2x aufgerufen in diesem Fall :

Nimm es hin.
Alternativ schieb ein delay() - mit eigener Zeit (testen) - an den Anfang von setup().

Wenn Du den RESET-Knopf auf dem MEGA drückst, passiert das nicht.
Das Ganze hängt mit dem Seriellen Monitor bzw. der Schnittstelle zusammen.
Es wird zweimal ein Reset nach dem Hochladen ausgeführt.
Einmal durch den Bootloader und einmal durch die IDE.

Welche Version benutzt Du?

Hallo my_xy_projekt

Danke für die Erklährung - ich habe einen delay(5000) hinzugefügt und das Problem ist weg.

Ich weiss nicht wie ich die Version heraus bekomme :slight_smile: - Aber mit dem delay() kann ich leben.

Gruss

ardudummy:
Ich weiss nicht wie ich die Version heraus bekomme :slight_smile: - Aber mit dem delay() kann ich leben.

Also 5 Sekunden sind zuviel :wink: Fang mal mit 1Sek an.

Deine Arduino-Version steht oben neben dem Namen des Sketches oben im Fensterrahmen...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.