Cars and programming

Why are there no easy tutorials to just write programs that control the DC motors with the Arduino chip? My daughter spent hours searching and trying to figure this out and became a little bit discouraged. There are so many great tutorials, but she really just wanted to figure out how to turn one motor… how to turn the other motor. That’s it.

1 Like

Hi !
It would really be helpful if you could give more details about your project.
What I mean is, what are your input bits ( light sensor, roller switch, pressure switch, … )
What does your arduino has to process ?
Your output bits, I guess 2 dc motors ?
And all our forum members will certainly help you.
Greetings

Thanks!

My daughter built a car that was pretty simple. Two motors at the outputs for 5 and 9. She didn’t understand if she should put the little switches to PWM or the other setting.

There are two wheels in the front and the little ball dragging in the back. She could put the simple push button to turn it on, or just turn it on at the power switch on the power module, and let that trigger the program to run after a short delay. It didn’t hugely matter to her.

She is smart and if there was somewhere some simple codex for controlling the motors, or if someone could post a note here, I’m sure she could figure out what to do. She just wants to write a program to start and stop the motors, and the she can do her own figuring from there. She doesn’t need to go into reverse.

She understands scratch, but wasn’t able to upload or store a Scratch program on the Arduino, and wasn’t quite sure about the language in the code blocks for the LittleBits extension because the documentation on those is really sparse.

She is willing to figure out the Arduino IDE software, and she has been able to upload some of the test sketches and other projects. But she can’t find a guide anywhere to help define the terms to use with LittleBits motors. I have no idea what to tell her because I don’t know anything about the C coding language.

I think her functionality in the program will probably be something like:

Both wheels forward

One wheel forward
(She says she doesn’t need the other one to go backwards, but I’m not so sure to turn.)

Both wheels forward

Stop

One wheel forward

Other wheel forward

Both wheels forward

In her head, she was going to run the car and time the distances, and I guess put times in for each movement.

Hi,
Seems a nice project !
There are certainly different ways to program the arduino to do this.
If I’ll find some time today, I will post a sketch here with a explanation.
It’s Sunday morning here ( I live in Europe ), so please be patient.
Till then

Hi @cheadrk
I found some time to write a little sketch to make the car run in a square.
Pressing the button makes the car start.
After 4 turns, the car stops.
Here is the code :smile:

/*
Littlebits Arduino controlled motors.

Construct a car which has 2 motors, 1 motor for the left wheel and 1 motor for the right wheel.
Connect the left motor at bitsnap 5 of the arduin and the right motor at bitsnap 9.
This sketch allows the car to take turns by stopping 1 motor for a certain time.
By adding more lines and to change HIGH and LOW states, more car directions and car tracks are possible.
Also by changing the ride and turn values will change the time the motors need to spin.

A start stop button at pin A0 of the arduino will start or stop the motors after they performed their spin cycle.

*/

// Mot1 = left motor
// Mot2 = right motor
// startpin = button connected at A0 of arduino
// start = when button is pressed
// last = remembers last state
// counter can be used to memorize the steps
// ride = delay time for both motors to spin.
// turn = delay time for 1 motor to stop and the other motor to spin.

// Defining the variables

int Mot1 = 5;
int Mot2 = 9;
int startpin = A0;
int start = 0;
int last = 0;
int go;
int counter = 0;
int ride = 4000; // change the value to alter the time the car goes in a straight line
int turn = 2500; // change the value to alter the time the car needs to turn

void setup() {

// setup of the different inputs and outputs of the arduino

pinMode (Mot1, OUTPUT); // Connect the left motor at Mot1 ( which is pin 5 and as output)
pinMode (Mot2, OUTPUT); // Connect the right motor at Mot2 ( which is pin 9 and as output)
pinMode (A0, INPUT); // Connect the button at A0 and set the pin as an input

}

void loop() {

// First, we will read the input and see if the button has been pressed

start = digitalRead (startpin);
delay (50); // debounce the switch
go = start; // needed to remember the button press

// when done, check if the start button value differs from the last state

if (start != last) {
// if so, check if the button has been press pressed now
if (start == HIGH) {
// Now, start the motors by writing the output(s) to high

digitalWrite (Mot1, HIGH);
digitalWrite (Mot2, HIGH);
delay(ride);

digitalWrite (Mot1, LOW);
digitalWrite (Mot2, HIGH);
delay(turn);

digitalWrite (Mot1, HIGH);
digitalWrite (Mot2, HIGH);
delay(ride);

digitalWrite (Mot1, LOW);
digitalWrite (Mot2, HIGH);
delay(turn);
}
else {
// or else, stop the motors by writing the outputs to low.
digitalWrite (Mot1, LOW);
digitalWrite (Mot2, LOW);
}
}
last = go;
}

Paste the text in bold in your arduino software.
Instructions are in the sketch.
If any questions arise of how it works, please ask.
Greetings

1 Like

Wow! That is so cool!!! I’m sure that my daughter will be able to learn from this and modify it to suit her desired ideas. I’ll print this out for her so she can play with it tomorrow. I can tell it will really help and will be super empowering, because it will help her go from doing things that just involve following directions in the kit. This will definitely be going into the area of creating her own idea, coding something that moves in real life.

My daughter was so confused about the structure of the new language, trying this for the first time. There are some basic things here I can see that were missing from what she was trying. For one thing, she hadn’t defined any variables. Somehow, that just wasn’t clear how to do that for some reason. This will really open up the door to going back and looking at the other programs, to figure out how they worked.

I really, really, really, really, really can’t thank you enough for such a great primer.

My daughter is really interested in this stuff and I can tell that she will quickly go past my limited knowledge. I actually think she may already have gone past. She’s playing with Scratch and also with LittleBits at home, and learning other things with a RaspberryPi at school, and I was feeling really out of my depth here and frustrated that I couldn’t find a better codex or guide to give her as a beginning guide. I think we will also try to find a LittleBits meet up or a maker space here in our area.

1 Like

One question for coding –

If you wanted to, is there a way to code the motors to run in reverse?

I think she found a post in a forum where they said it wasn’t possible, but since the motors say they are “variable” and can run in both directions, she was wondering.

Shucks, I have one more question:

ANALOG or PWM?

Thanks! I’m sure she’ll figure that out by trial and error, but what would be best?

Hi ,
Unfortunately, the motors can’t be programmed to run in reverse.
They have a little switch to choose from forward to reverse, but this switch can’t be controlled by other bits.

Analog or pwm doesn’t matter here, since the outputs are only digital, HIGHor LOW.
PWM can be introduced in the sketch to make the car faster or slower.
This speed can even be controlled by other littlebits at the analog input of the arduino.
This is a little more complex for now, but when you are more familiar with coding, I’m sure you will be able to give this a try.
There are so many possible ways to use an arduino together with littlebits :grinning:

BTW, I have notice you need to copy an extra turn and ride code segment for the car to return to the start position.
Can you find out where to put these in the sketch ? ( would be a good exercise :wink: )
Also, experiment a bit with the delay values of ride and turn to get the car to ride in a square !
Good luck !

1 Like

Thanks! My daughter was so excited about your tutorial that she got up early to play with this and was able to load it onto her project. She could tell what the program was doing, and already has used your tutorial to write her own code and experimented, adding different turns. She is COMPLETELY THRILLED with understanding the structure of the code now. She even commented, “Now that I can really see what it’s doing, it’s really not that hard!” I was really hoping for that kind of response and excitement. Can’t believe she got up early on a Monday before school!! So again, thanks for your help.

The only problem she’s having is that the board is really tight and kind of hard to snap things into, and one of the motors stops working – she has to squish the cord into a little on the board, to make sure it’s in there firmly. But basically, she’s off to the races… so to speak.

1 Like