How the f**k does the Arduino execute code?

I'm trying desperately to get my PWM-control working. Here's my code:

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int start = 2;
int inputValue = 1;
int pwmM1 = 5;
int pwmM2 = 6;
int dir1M1 = 4;
int dir2M1 = 3;
int dir1M2 = 8;
int dir2M2 = 9;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT); 
  pinMode(start, INPUT);
  pinMode(pwmM1, OUTPUT);
  pinMode(pwmM2, OUTPUT);
  pinMode(dir1M1, OUTPUT);
  pinMode(dir2M1, OUTPUT);
  pinMode(dir1M2, OUTPUT);
  pinMode(dir2M2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  delay(5000);
  
  while(1){    
  digitalWrite(dir1M1, LOW);
  digitalWrite(dir2M1, HIGH);
  analogWrite(pwmM1, 255);
  delay(1000);
  digitalWrite(dir2M1, LOW);
  digitalWrite(dir1M1, HIGH);
  delay(1000);
  }
}

The idea is that after 5 seconds, one motor should start rotating in one direction for 1 second and then change direction and rotate for another second and then repeat. In reality it does whatever the hell it wants. It starts out fine the first cycle but then the motor mostly rotate in the same direction all the time but not always. Can someone please tell me what the hell is going on?

and then repeat. I

It's in a "while (1)" loop - how is it expected to repeat the 5 second delay?

while(1){}

This blook of code will be run over and over again until power is removed

There are a few things that trigger me.

  1. add a "stop command" in between the turning directions.
  2. I only see one analogWrite(), so it can be moved outside the while(1) loop
    Or are you missing one?
  3. I miss the hardware schema, how is it connected, do the motors get enough power?
  4. as void loop() is an eternal loop in itself the while(1) is not needed when you move the delay(5000) to the setup()
  5. pwmM2 is not used ????

HugoTP:

Yes it should run over and over until the power is removed but it doesn't look like it does since it runs the motor in only one direction after a while and sometimes it stops completely.

Robtillaart:

  1. How would a stop command help?
  2. Only one analogWrite() is used but the idea is to change it. Let's say that I'll use analogWrite(full speed) in one direction and analogWrite(Low speed) in the other direction. Then I'll need to keep the analogWrite inside the loop(), or do I?
  3. The motors are connected to a L293 H-bridge. It is lego motors that are powered with a regular 9 volt battery so there should be sufficient power to run them.
  4. Yes, the loop() should be an eternal loop but it does not behave like that right now. Sometimes it only execute part of what's inside the loop() and sometimes it just stops. Thats why I added the while(1), to make an eternal loop of my own. No improvement though.
  5. pwmM2 will be used when I get pwmM1 working.

Thank you for your time.

Don't try to get your motor to change direction.
Just get it to turn one way.
Does the motor turn as expected?

Change the code to make the motor turn in the opposite direction.
Does the motor turn as expected?

If you cannot get the above to work then you probably have some kind of wiring problem.

Once you have the simple code working combine the two, to get the direction change.

but it doesn't look like it does since it runs the motor in only one direction after a while and sometimes it stops completely.

"like" doesn't come into it - it does what it is told, and doesn't do what it isn't told.

How is the "pwmM1" wired?

Problem solved, but god knows how. When was about to change the code as per your recommendations I first powered up the Arduino again. It worked like a charm. I examined why and I saw that a cable had come loose. The cable that had come loose was the cable connected from the 5V on the Arduino to pin 16 on the L293B H-bridge (since that's what the wiring diagram in the data sheet says). As far as I know, it's the power supply for the logic part of the H-brigde. Well, now it is disconnected and it works perfectly. Go figure. I reconnected it again and got the same random behavior. Can anyone explain this to me?

Anyway, thank you for your time gentlemen.

You obviously have a wiring problem.

For people to help you will need to provide details of how you have wired the system and double check that it really is wired the way you think it is.

I have found this for a LT293D (you are using B)