[SOLVED]PWM stopping suddenly

I have a chinese arduino nano clone connected to an L293D motor controller module, which I can confirm is working fine.

Today, suddenly, pwm stopped working. It was working until I toggled some other pins. Now it will push HIGH when I pass >127, or low otherwise. I have pin 3 connected to the EN1 pin. I didn’t modify the circuit from the time it worked.

Pin3 → EN1
Pin6 → C2A
Pin7 → C2B
Pin8 → C1A
Pin9 → C1B

The code:

carctl.ino:

#include "cmds.h"

// Main Vars
byte speed  = 0;
byte incmd;


void setup() {
  Serial.begin(115200);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  analogWrite(3, speed);
}


void speedSet() {
  analogWrite(3, speed);
}

void speedUp() {
  speed++;
  speedSet();
}

void speedDown() {
  speed--;
  speedSet();
}

void dirToggle() {
  digitalWrite(8, digitalRead(9));
  digitalWrite(9, !digitalRead(8));
}

void dirBackward() {
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
}

void dirForward() {
  digitalWrite(8, LOW);
  digitalWrite(9, HIGH);
}

void turnLeft() {
  digitalWrite(6,HIGH);
  digitalWrite(7,LOW);
}

void turnRight() {
  digitalWrite(6,LOW);
  digitalWrite(7,HIGH);
}

void turnForward() {
  digitalWrite(6,LOW);
  digitalWrite(7,LOW);
}



void loop() {
  if(Serial.available()) {
    incmd = Serial.read();
    if(incmd != CMD_NOP) {
      switch(incmd) {
        case CMD_DFW:
          dirForward();
          break;
        case CMD_DBW:
          dirBackward();
          break;
        case CMD_DTG:
          dirToggle();
          break;
        case CMD_TRI:
          turnRight();
          break;
        case CMD_TLE:
          turnLeft();
          break;
        case CMD_TFW:
          turnForward();
          break;
        case CMD_SPU:
          speedUp();
          break;
        case CMD_SPD:
          speedDown();
          break;
        case CMD_HLT:
          speed = 0;
          speedSet();
          break;
        case CMD_SPS:
          speed = Serial.read();
          speedSet();
          break;
      }
    }
  }
}

cmds.h:

#define CMD_DFW 100 //d
#define CMD_DBW 101 //e
#define CMD_DTG 102 //f
#define CMD_TRI 103 //g
#define CMD_TLE 104 //h
#define CMD_TFW 105 //i
#define CMD_SPU 106 //j
#define CMD_SPD 107 //k
#define CMD_HLT 108 //j
#define CMD_SPS 109 //m
#define CMD_NOP 110 //n

UPDATE: I tried the code on an arduino mega 2560, and this time it didn't even work the first few seconds

Also, one more strange thing: If it put "mz" into serial (the char values from the commands), which should put it into about 60% duty cycle, it will power the motor at HIGH. But when I put in 'j', which would reduce speed by 1 (and for the vim users out there, it was a total coincedence that j and k were speedup and speeddown, but in the wrong order). So I assumed any value less than the char value of 'z' would put it to low. However, it runs at high even if I put in "ma".

UPDATE2:
I think this is related to:

Hi,
Pin3 where do you assign it as an output.

Tom.... :slight_smile:

It says:

"You do not need to call pinMode() to set the pin as an output before calling analogWrite()."

I fugured it out:

Turns out the baud rate was too slow for the processor, and even though I sent "mz", the m reached and was interpreted and serial was read before z arrived, so it set the speed to -1. But speed was a byte, so it went all y2k over it. Fixed now.