RC Car DC motor code

Good morning you all :grin:. I had success on my dc motor on my Mega, after I fried my Uno from bad wiring :grimacing:. I have now successfully wired it to my newly purchased Nano. After a few hiccups, I wa as able to browse the forums on here, and was able to resolve some related problems. However, even when I did the ccw on Mega, it was a little unresponsive on a low setting with the potentiometer. It occured at 15. With the Nano, it's anything below 129. Also, I wanted to use buttons to turn left and right, without having to purchase a joystick. I have a code for that as well, but I'm trying to make sure it doesn't interfere with my cw and ccw buttons. I have an idea to rearrange everything, but I'm trying to take this one step at a time :grin:. I think the problem with my trim pot lies within the mapping?

#define mL 8 // PWM motor pin

#define mR 9 // PWM motor pin

#define sL 6 // button for clockwise

#define sR 7 // button for counter clock wise

void setup() {

Serial.begin(9600);

pinMode(mL, OUTPUT);

pinMode(mR, OUTPUT);

pinMode(sL, INPUT);

pinMode(sR, INPUT);

}

void loop() {

int Pot = analogRead(A0);

Pot = map(Pot, 0, 1023, 0, 255);

Serial.println(Pot);

int left = digitalRead(sL);

int right = digitalRead(sR);

if(left == LOW && right == HIGH)

{

analogWrite(mL, Pot);

digitalWrite(mR,LOW);

}

else if(left == HIGH && right == LOW)

{

digitalWrite(mL, LOW);

analogWrite(mR, Pot);

}

else{

digitalWrite(mL, HIGH);

digitalWrite(mR,HIGH);

}

}

Since you are not using the internal pull-up resistor (INPUT_PULLUP) then you must be using an external pull-up or pull-down resistor. Which is it?

Um, I'm not sure sorry. I added two resistors, one for each button. A video I watched said, this is a floating resistor. I hope this schematic isn't fuzzy. Some members said they couldn't see it, on my last post. I tried to add a video, but couldn't. Had a feeling I wouldn't be able to. The dc motor driver has pins to a soldered resistor though, I never connected it.

Okay, sorry my response wasn't helpful. If it helps, my two resistors that are connected to my buttons, going to the digital pins, are connected to ground. So this would be a pull down, correct?

Here's a better schematic of the wiring.

Correct.

Your code doesn't match your schematic. In the code, you are using pins 8 and 9 for motors and pins 6 and 7 for buttons. In the schematic, you are using 5 and 6 for the motor and 2 and 3 for the buttons. The 'mL' and 'mR' pins have to be analogWrite() pins (3, 5, 6, 9, 10, or 11) so the schematic will work but the original code (8 & 9) won't.

Yeah I just noticed that. My bad :grin:, I thought everything on the right was digital I/O, no matter the number. I've been finding some videos, and it seems only certain digital pins can be used, for I/O. Not sure if I'm saying that right :grin:.

So mL and mR can only use these pin numbers? Why is that, and why does the schematic define one motor as mL and mR? Is this the polarity for the motor, Left side Right side? Oh, is the pull down resistor slowing the response, or pulling the ccw response down too far? Sorry, I'm not too good with the lingo :grin:.

Just to let you know, I did change my code according to the pin changes :grin:, relating to the schematic.

Yes. You can only use analogWrite() on pins that support PWM. They are usually marked on the board with a ~ symbol.

It's just whatever the author thought would be meaningful names for the two pins controlling the motor driver. Maybe the author spoke a language where mL and mR made sense.

I don't know what you mean by "slowing the response" or "pulling the CCW response too low". pull-up or pull-down resistor is required so your input pin is not left floating when the switch is open.

Correct, but I have to turn my trim pot (potentiometer) up to halfway in order to get it going ccw.

Half-way is where a non-PWM pin switches from LOW to HIGH when you use analogWrite() on it. Are you now using pins 5 and 6 for your motor outputs?

It doesn't do that on my cw, just ccw. The cw starts much earlier than ccw. One last quick question, until I finish wiring everything on my larger bread board. Can I put my pull down resistors in parallel, for both buttons?

Okay, I kept mL at 8, and mR at 9. I switched cw to 7 and ccw to 3.

That still won't work as I pointed out in replies 8, 11, and 13.

If you use analogWrite() on a non-PWM pin, like Pin 8, you get LOW/0 for values up to 128 and HIGH/255 for values above 128.

Okay. I almost had a scare staying up late this morning. I can't get power on both sides, so I did what I wasn't suppose to do, I hooked a 9V battery to my motor driver. Luckily after I took everything off of the board, mainly the wires, my COM port came back up. I'm going to go out today to get a breadboard power supply. I have one, but I am using it for the motor driver. It's the only thing that works to power the motor. So mL and mR, Left and Right is just IN1 and IN2 right?

What symbol are you talking about sir, a~ symbol? Do you mean "a" for analog, because the analog pins are on the left side.

In the slightly blurry picture of the Arduino Nano V3.0 in your circuit diagram, it looks like they are using the '*' character. On the Arduino UNO V2 they use the '~' character.

Sorry about the blurriness, I thought I sent a better schematic. I put everything back the way it was, corresponding to the schematic. I just switched everything to make it neater. It's just something about crooked lines that irks me :grin:. I'm about to get these breadboard power supplies in a few minutes.

Okay, got everything back working :grin:. Now I just have to find pins to make my car go left and right. Okay, I see what you're talking about now with analog write. On the schematic, kind of blurry, it has a mark on it. I'm assuming it's a tilded mark, as you indicated. So for my left and right turning, I guess I can use 7 and 8. Quick question, will the left and right turn be like 10, or 15°, or a full 90°?