Stuck in a Problem

Hey, I’m doing this robot car controlled by an infrared remote and I want to set the dc motors speed from its default 255 to 100. When my code didn’t have the analog statements, it was working fine but after I added them even a single dc motor wasn’t working.

The code I use:

#include <IRremote.h>

#define forward 6375
#define backward 19125
#define left  4335
#define right  23205
#define stop_button  14535
#define rag_button 26775
#define scrubber_button 45135

const int EN_A_1 = 3;
const int EN_B_1 = 13;
const int IN_1 = 12;
const int IN_2 = 11;
const int IN_3 = 10;
const int IN_4 = 9;
const int IN_1_1 = 7;
const int IN_2_2 = 6;
const int IN_3_3 = 5;
const int IN_4_4 = 4;


int itsON[] = {0,0,0};

char command;
const int receiver_pin = 2;  
IRrecv receiver(receiver_pin); //Arduino will take output of IR receiver from pin 2
decode_results output;
void setup()
{
  Serial.begin(9600);
  receiver.enableIRIn();  // Start to take the output from IR receiver
  //initializing all the pins as output pins
  pinMode(EN_A_1, OUTPUT);
  pinMode(EN_B_1, OUTPUT);
  pinMode(IN_1, OUTPUT);
  pinMode(IN_2, OUTPUT);
  pinMode(IN_3, OUTPUT);
  pinMode(IN_4, OUTPUT);
  pinMode(IN_1_1, OUTPUT);
  pinMode(IN_2_2, OUTPUT);
  pinMode(IN_3_3, OUTPUT);
  pinMode(IN_4_4, OUTPUT);
  
}
void loop() {
  if (receiver.decode(&output)) {
    unsigned int value = output.value;
    switch(value) {
     

case forward:
      //Moving Forward
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW); 
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
  delay(50);
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, HIGH);
  analogWrite(EN_A_1, 100); 
  digitalWrite(IN_3, HIGH);
  digitalWrite(IN_4, LOW);
  analogWrite(EN_B_1, 100);
   
          break;
       
case backward:
      //Moving backward
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW); 
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
  delay(50);
  digitalWrite(IN_1, HIGH);
  digitalWrite(IN_2, LOW);
  analogWrite(EN_A_1, 100); 
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, HIGH);
  analogWrite(EN_B_1, 100);
         break;
       
case left:
       //Turning left
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW); 
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
  delay(50);
  digitalWrite(IN_3, HIGH);
  digitalWrite(IN_4, LOW);
  analogWrite(EN_B_1, 100);
          break;  
        
case right:
        //Turning Right
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW); 
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
  delay(50);
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, HIGH);
  analogWrite(EN_A_1, 100);
          break;
       
case stop_button:
        //Stop
  digitalWrite(IN_1, LOW);
  digitalWrite(IN_2, LOW); 
  digitalWrite(IN_3, LOW);
  digitalWrite(IN_4, LOW);
          break;

case rag_button:
  if(itsON[1] == 1){
    digitalWrite(IN_1_1, HIGH);
    itsON[1] = 0;
  }else{
    digitalWrite(IN_1_1, LOW);
    itsON[1] = 1;
  }
  break;

case scrubber_button:
  if(itsON[1] == 1){
    digitalWrite(IN_3_3, HIGH);
    itsON[1] = 0;
  }else{
    digitalWrite(IN_3_3, LOW);
    itsON[1] = 1;
  }
  break;
    }          

    Serial.println(value);
    receiver.resume();
}
}

If you replace the 100 in the following line with 255 does it work?

analogWrite(EN_A_1, 100);

If so then maybe you just need a value somewhere between 100 and 255?

You should also be aware that motors can require extra power to get them started and once moving the power can be reduced.

...R

Robin2:
If you replace the 100 in the following line with 255 does it work?

analogWrite(EN_A_1, 100);

If so then maybe you just need a value somewhere between 100 and 255?

You should also be aware that motors can require extra power to get them started and once moving the power can be reduced.

…R

Only one side works… :frowning:

Pin 13 is not a PWM output pin on the Arduino UNO.

If you try to use analogWrite() on a non-PWM pin you will get LOW for values below 128 and HIGH for values 128 and above.

johnwasser:
Pin 13 is not a PWM output pin on the Arduino UNO.

If you try to use analogWrite() on a non-PWM pin you will get LOW for values below 128 and HIGH for values 128 and above.

So should I use another pin? What pins are PWN output pins?

These are (depicted in Fig-1) valid PWM pins of UNO on which analogWrite() function works.
pwm328.png
Figure-1:

pwm328.png

sioty:
So should I use another pin? What pins are PWN output pins?

See: analogWrite() - Arduino Reference

What pins are PWN output pins?

The ones marked with a ~ next to the pin number

UKHeliBob:
The ones marked with a ~ next to the pin number

GolamMostafa:
These are (depicted in Fig-1) valid PWM pins of UNO on which analogWrite() function works.
pwm328.png

johnwasser:
See: analogWrite() - Arduino Reference

Used pin 9 but it only works if the value is 255.

analogWrite(EN_A_1, 255);

If it's <255 such as 254, it doesn't work.

analogWrite(EN_A_1, 254);

Any ideas onto why? And what to do?

Can you see the ~ marks in the following picture of UNO?
unoPicture.png

unoPicture.png

If it’s <255 such as 254, it doesn’t work.

What does “doesn’t work” mean ?

#include <IRremote.h>Uh-oh

UKHeliBob:
What does "doesn't work" mean ?

One side (the IN1 and IN2) doesn't move at all.

GolamMostafa:
Can you see the ~ marks in the following picture of UNO?
unoPicture.png

Yeah... I used pin 9

Which timer does the the IR library use?

TheMemberFormerlyKnownAsAWOL:
Which timer does the the IR library use?

What do you mean by timer?

sioty:
What do you mean by timer?

IRremote uses Timer1 to sample the received signal. That means that the two pins that rely on Timer1 for PWM (9 and 10) can't be used for analogWrite() when you use the IRremote library for receiving IR. You will have to use 3, 11, 5 or 6 for your two 'enable' outputs.

johnwasser:
IRremote uses Timer1 to sample the received signal. That means that the two pins that rely on Timer1 for PWM (9 and 10) can't be used for analogWrite() when you use the IRremote library for receiving IR. You will have to use 3, 11, 5 or 6 for your two 'enable' outputs.

Only pin 3 works. I tried using all the other PWM pins (5, 6, 9, 10, 11) and none of them work. Someone please give me an IRremote library that works with other PWM pins. Thanks!

sioty:
Only pin 3 works. I tried using all the other PWM pins (5, 6, 9, 10, 11) and none of them work. Someone please give me an IRremote library that works with other PWM pins. Thanks!

I'm really desperate for someone to send me an IRremote library that supports all PWN pins or atleast 2. :sweat_smile:

TheMemberFormerlyKnownAsAWOL:
#include <IRremote.h>Uh-oh

What does “uh-oh” mean?? If you have a working IRremote library that works with all or two PWN pins, please I need the link to download.