Hello there. Recently I added an IR sensor to control a four wheel drive (start engine, stop engine, rotate it forward and backward, etc.). Everything works fine, but when I change pins going from one of the relays to Arduino, IR signal works only once to start an engine, and then just stops working or, sometimes, just does not work at all. I am a complete novice with such things and was lucky enough to avoid that unknown to me problem with that "secret" combination of pins, but I am very eager to know what really could be wrong. I am looking forward to hear from you soon. Also I provided a screenshot of the relay which I use if it may be of any help.
P.S. If there is any good book to learn how to solve similar problems or any other good source of information I would be very glad to get that kind of advice as well. Thank you.
Thank you for your reply. By this moment that "secret" combination of pins I mentioned in the previous post sometimes fails to work too (so, there is no any secret combination of pins anymore). First I upload the code, then enable power for the motor (one of the four ones which I test now). The problem is that first everything is fine and I can control some engine modes (forward, backward and stop) with remote control, but after several presses it starts to send wrong key codes and then gets disabled completely until the next run. I don't know what exactly it may be. Here is the sketch which I use for this project.
#include <IRremote.h>
class Car {
private:
unsigned long keyCode;
int in1 = 11; int in2 = 12; //Motor 1
//int in3 = 9; int in4 = 10; //Motor 2
//int in5 = 2; int in6 = 3; //Motor 3
//int in7 = 6; int in8 = 5; //Motor 4
public:
//Setters
void setKeyCode(unsigned long keyCode) {
this->keyCode = keyCode;
}
//Setters
//Getters
unsigned long getKeyCode() {
return this->keyCode;
}
//Getters
//Move forward, backward and stop
void run() {
if (this->getKeyCode() == 3091726080) {
digitalWrite(this->in1, 1); digitalWrite(this->in2, 0);
} else if (this->getKeyCode() == 3125149440) {
digitalWrite(this->in1, 0); digitalWrite(this->in2, 1);
} else if (this->getKeyCode() == 3108437760) {
digitalWrite(this->in1, 0); digitalWrite(this->in2, 0);
}
}
//Move forward, backward and stop
//Default constructor
Car() {
this->keyCode = 1;
pinMode(this->in1, OUTPUT);
pinMode(this->in2, OUTPUT);
pinMode(A0, INPUT);
IrReceiver.begin(A0, ENABLE_LED_FEEDBACK);
}
//Default constructor
};
Car* engine;
void setup() {
engine = new Car();
//pinMode(in3, OUTPUT); pinMode(in4, OUTPUT ); //Motor 2
//pinMode(in5, OUTPUT); pinMode(in6, OUTPUT ); //Motor 3
//pinMode(in7, OUTPUT); pinMode(in8, OUTPUT ); //Motor 4
Serial.begin(9600);
}
void loop() {
engine->run();
if (IrReceiver.decode()) {
unsigned long a = IrReceiver.decodedIRData.decodedRawData; //Receive a value from remote control
IrReceiver.resume();
engine->setKeyCode(a);
Serial.println(engine->getKeyCode());
}
}
If anybody knows what may be any possible reason for such a weird behaviour, I would be very glad to follow your advice. Thank you for your attention and support.
Unfortunately, the relay which I use (you can see its screeshot above) can't be found in both TinkerCad or Wokwi simulators, so, for now I can't create any simulation for you, guys. Please, let me know what schematic should usually be done in such a case as I am a complete novice with this kind unknown to me representation. I made and tested all that in Arduino IDE straightaway with no any previous preparation except for the real scheme. I attached a top view of it to this post if it may be of any help. If you like, I can get rid of all odd components (I test only one motor with one relay for now) to simplify your efforts around this issue, though this view is not really acceptable I suppose, but at least you can see something to evaluate. Please, let me know if you have any further quesions in relation to this post.
Thank you for your reply. Here is the sceenshot which I made with a pencil. It describes only one motor and only one relay. In reality where problem occurs all four motors are connected with two relays. Now I will detach those to fit it to the scheme I provided to check if the problem still persists. I will let you know as soon as I get any results on that matter. Looking forward to hear from you too.
van_der_decken, I disjoined other parts according to the pencil scheme above and the problem disappeared. At least I could control only one engine with only one relay for a long time after several runs. I will inform you on the next advancement as soon as I detect the mentioned problem again. I will be adding other motors and second relay step by step to see when it could happen. Thank you all for your help and kind attention.