Motor shield rev3 not working

I am trying to make a Rc car with help of arduino for a school project and I have run into a problem with the motor. The code running the motor does run if isolated in its own sketch. But it does not work in this way.

#include <IRremote.hpp>
#define IR_RECEIVE_PIN 4
int directionPin = 12;
int pwmPin = 3;
int brakePin = 9;

String todo;
String received;

int voren() {
  digitalWrite(directionPin, HIGH); // naar voren
  digitalWrite(brakePin, LOW); // remmen uit
  analogWrite(pwmPin, 40); // kracht in de motor
  delay(1000);
  digitalWrite(brakePin, HIGH); // remmen aan
  analogWrite(pwmPin, 0); // kracht 0
  Serial.println("ik zit in voren"); // test
}

int achter() {
  digitalWrite(directionPin, LOW); // naar achter
  digitalWrite(brakePin, LOW); // remmen uit
  analogWrite(pwmPin, 30); // kracht in de motor
  delay(1000);
  digitalWrite(brakePin, HIGH); // remmen aan
  analogWrite(pwmPin, 0); // kracht uit de motor
  Serial.println("ik zit in achter");
}

void setup() {

  todo = "niets";
  received = "niets";
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
  //define pins
  pinMode(directionPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
  pinMode(brakePin, OUTPUT);
}

void loop() {
  if (IrReceiver.decode()) {
      received = IrReceiver.decodedIRData.decodedRawData;
      Serial.println(received); // Print "old" raw data om te kijken wat het is TODO verwijder
      if (IrReceiver.decodedIRData.decodedRawData == 3074981122) {
        todo = "boven"; // zet todo als boven als bepaalde knop ingedrukt wordt
      }
      else if (IrReceiver.decodedIRData.decodedRawData == 2991422722) {
        todo = "onder"; // zet todo als onder als bepaalde knop ingedrukt wordt
      }
      
      

      if (todo == "boven") {
        voren(); // run door de voren functie
      }
      else if (todo == "onder") {
        achter(); // run door de achter functie
      }
      else if (todo == "niets") {
        Serial.println("niks te doen");
      }
      
      Serial.println(todo);

      IrReceiver.resume(); // Enable receiving of the next value
  }
}

You can add Serial.println at strategic points in the code telling how the execution proceeds. Serial monitor shows them.

Depending which library you are using for IR, there are limitations what pwm pins you can use. Probably pins 3 and 11 are not available for PWM.

I believe this is it, unfortunately I have not found a similar library to read an IRreceiver, and those pins are the only pins that work on the shield for driving the motor. Do you have any idea on how to make the IR library not use specific pins?

because IR receiver uses timer2 of atmega.
you can change it to timer1 in IRTimer.hpp

add this as first line of your code:
#define IR_USE_AVR_TIMER1

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