Where Does Pin 0 Enter The Picture?

Just learning how to code an Arduino and attempting to revise someone else's code in an already functioning project. Full code and wiring diagram below.

Question regarding the line:

int pwm = 0;

As I understand it, this declares pin 0 as the pwm output. But as you'll see in the wiring diagram, pin 0 isn't being used. How does "0" play into things?

#define MTR 5 //motor right
#define MTL 6 //motor left
#define DIR_R 7 //right direction
#define DIR_L 8 //left direction
#define FWD LOW
#define RVS HIGH
void setup() {
  pinMode(10,INPUT); //Channel 1 (steer)
  pinMode(11,INPUT); //Channel 3 (throttle)
  pinMode(9,INPUT); //Channel 5 (direction)
  pinMode(MTR,OUTPUT);
  pinMode(MTL,OUTPUT);
  pinMode(FWD,OUTPUT);
  pinMode(RVS, OUTPUT);
  Serial.begin(9600);
}

int steer; //steering number
int throttle; //throttle number
int pwm = 0; // pwm number
int fwd = 1 ; 
int direc = analogRead(A0);
void speedFunct(){
  //full power
  if(pwm >= 250){
    analogWrite(MTR,255);
    analogWrite(MTL,255);
  }
  //no power
  else if(pwm <= 0){
    analogWrite(MTR,0);
    analogWrite(MTL,0);
  }
  //percentage of power (duty cycle)
  else{
    analogWrite(MTR,pwm);
    analogWrite(MTL,pwm);
  }
}
void steerFunct(){
  //drive straight
  if(1450 < steer < 1550){
    if(fwd == 1){
       digitalWrite(DIR_R, FWD);
       digitalWrite(DIR_L, FWD);
    }
    else{
       digitalWrite(DIR_R, RVS);
       digitalWrite(DIR_L, RVS);
    }
     speedFunct();
      delay(5);
  }
  //steer right
  if(steer >= 1550){
    digitalWrite(DIR_R, RVS);
    digitalWrite(DIR_L, FWD);
    speedFunct();
    delay(5);
  }
  //steer left
  if(steer <= 1450){ // 1400
    digitalWrite(DIR_R, FWD);
    digitalWrite(DIR_L, RVS);
    speedFunct();
    delay(5);
  }
}

void loop() {
  //Pin setup
  steer = pulseIn(10, HIGH, 250000);
  throttle = pulseIn(11, HIGH, 250000);
  direc = pulseIn(9, HIGH, 250000);
  Serial.println(throttle);
  Serial.println(steer);
  /***Direction control***/
  //Forward
  if(direc <= 1000){
    analogWrite(DIR_R, FWD);
    analogWrite(DIR_L, FWD);
    pwm = map(throttle, 990, 1983 ,0 ,255);
    fwd = 1;
    steerFunct();
    delay(5);
    Serial.print("Direction: ");
    Serial.println("Forward");
    Serial.println("\n");
    
  }
  //Reverse
  if(direc >= 1900){
    digitalWrite(DIR_R, RVS);
    digitalWrite(DIR_L, RVS);
    pwm = map(throttle, 990, 1983 ,0 ,255);
    fwd = 0;
    steerFunct();
    delay(5);
    Serial.print("Direction: ");
    Serial.println("Reverse");
    Serial.println("\n");
  }
  //Dead Zone(No movement)
  if(1400 < direc && direc < 1500){
    //pwm = 0;
    //speedFunct();
    digitalWrite(MTR, LOW);
    digitalWrite(MTL,LOW);
    delay(5);
    Serial.print("Direction: ");
    Serial.println("Stop");
    Serial.println("\n");
  }
  
  delay(5);
}

Nothing at all to do with pin 0, it is the value that represents the PWM duty cycle.

Pin 0 is the serial interface's receive pin.

In fact, the "= 0" is redundant - the variable is a global, and is automatically initialised to zero, because it hasn't been explicitly set to another value.

1 Like

No. :roll_eyes:

pwm is declared as a variable to use for the output value.

Oops

By looking at how pwm is used you can tell that it is NOT being used as a pin number.

1 Like

You're not the first (or even third) person to point this out. I've changed it before (and will again), but it doesn't seem to have any effect on the vehicle's functioning. Do you know how this error might manifest itself?

The expression evaluates left to right
"1450 < steer < 1550"
Is 1450 less than steer?
Answer is true (1) or false (0)
Is 1 or 0 less than 1550?
Yes. Always.
TL, DR - the expression is always true

1 Like