General Discussion

official

#21

I don’t think PWM was really covered in the modules. Basically it’s how to simulate an analog signal from digital:

https://www.arduino.cc/en/Tutorial/PWM

Near the top of the sketch, pwms is declared:

// Array to track current PWM values for each channel (A and B)
int pwms[] = {APWM, BPWM};

And above that the APWM/BPWM pins are set:

#define APWM 5
#define BPWM 11

Those are part of the 7 pins the motor controller uses. Part 2 covers this some in the schematic:

So looking at goForward() for example:

// Move the robot forward
void goForward ( ) {
  digitalWrite(STBY, HIGH);
  digitalWrite(AIN1, LOW);
  digitalWrite(AIN2, HIGH);
  digitalWrite(BIN1, LOW);
  digitalWrite(BIN2, HIGH);
  writePwms (STRAIGHTSPEED - leftOffset, STRAIGHTSPEED - rightOffset);
}

We’re toggling (i.e. digital) STBY, AIN1, AIN2, BIN1, and BIN2 pins, and analog controlling the speed with APWM and BPWM pins. Because in Part 1 we’re just moving the robot forward/backward/etc. by STRAIGHTSPEED and TURNSPEED, it seems unsatisfying. Later in Part 2 when you hook up the Blynk.cc app to use a joystick, the analog sliders for finer speed control will make more sense with the PWM functionality.


#22

Hey there! Thanks for the quick reply :slight_smile:

I fully understand all of that…the part I don’t get is:

analogWrite (pwms[0], left);
analogWrite (pwms[1], right);

Looking up the analogWrite function on the Arduino site, it would seem that it is passing…oh wait…

CLICK

It’s because of this:
int pwms[] = {APWM, BPWM};
This is declaring an integer array. In the array is APWM and BPWM, but they were declared earlier so calling them by name is the same as passing the pin numbers into this array?

So, when you have this:
void writePwms (int left, int right {
analogWrite (pwms[0], left);
analogWrite (pwms[1], right);

While considering the following syntax:
analogWrite(pin, value)

it would go a little like this for the goForward function (assuming there was no offset values set)

\the value 200 gets passed into the to parameters and is assigned to both left and right variables
void writePwms (200 left, 200 right {
\these two lines send an analog value to the APWM and BPWM pins (which was declared as pin 5 and pin 11) and that value is set as left which equals 200
analogWrite (pwms[APWM=5], left=200);
analogWrite (pwms[BPWM=11], right=200);

Sorry for my messy syntax. I’m trying my best to articulate what’s in my brain…I have no programming experience.

Does what I’m saying make sense? Am I on the correct track?

Cheers,
Taolin


#23

Yep! In Part 1 with fixed speeds (and assuming no offsets for mismatched motors), goForward() would be calling writePwms(200,200) which would call analogWrite(5,200) and analog(11,200). Because of the #define at the top, you can use the name APWM instead of the value 5 anywhere in your code. The compiler swaps them out for you. It typically makes for nicer to read code, instead of having hard-coded magic numbers all over the place.

I think something that’s a little off is viewing it as pins 0 and 1. Those are just the array references. When pwms is declared, it’s an array of length two (just indices 0 and 1). So pwms[0] is APWM and pwms[1] is BPWM. Array locations start numbering at 0, just like the pins.

But overall yeah, it seems like you’re on the right track here!


#24

Yes exactly! That was the part that clicked for me. I didn’t initially understand that the calls for [0] and [1] were actual addresses in an array as opposed to actual pins. It all makes perfect sense to me now. I appreciate the help!

Cheers,
Taolin


#25

Definitely think that for future projects the coding part should be explained much better, I have been struggling too.

For a complete beginner like me it’s pretty tough I thought


#26

I agree that it was definitely tough. I’m also a beginner at coding, but I’d encourage you to go back through some of the basic codes (the one making it go back and forth) and just figure it out by looking for patterns, repeated commands… I’ve already learned a lot just by doing that. Also, I’d recommend two other things:

  1. Look for basic Arduino tutorials, either books or videos. There are a lot of people on YouTube that have playlists from basic codes all the way to small projects

  2. Try using drag-and-drop programs like Scratch to learn the fundamentals of coding. Like I said, I still consider myself a beginner, but from my understanding, drag-and-drops programs are pretty much the same as coding, just without the actual code, or in other words, in English rather than a confusing programming language


#27

Thanks for letting us know, Alex! We’re working on creating better instructions/tutorials so that the coding portion isn’t as tough. Anything in particular that you’d like to see in the coding explanations? Perhaps covering more content? Thanks for the feedback in advance! Oscar


#28

Hi Alex,
yeah we have jumped in fairly deep straight off with building this bot first up.

Perhaps you should try looking at the examples in the Arduino IDE. They guide you thru this all at https://www.arduino.cc/en/Tutorial/HomePage
If you can buy the basic electronic parts that are needed for some of them, that would help. Things like LED’s a 10k ohm Potentiometer, they should only cost a couple of dollars.
This will give you a good introduction to the Arduino the IDE platform and programing as well as putting together the basic electronic components.

Good luck
Coyote


#29

Thanks I’m glad, to be honest if you started from basics to help users fully understand the syntax while writing it and how each line effects the board and what exactly each statement is doing, that would be useful

Even examples of smaller programs to help, it’s quite overwhelming to have a wall of code straight away - my 2 cents anyway, not sure if other beginners feel the same

Will practice more myself and learn what I can, but I’m having to learn from other resources to understand further

Thanks
Alex


#30

Hello I am new to thimble and my wife got these as a gift for my kids and I. I am making the wifi bot now and my kit doesn’t have an arduino board. Was it supposed to come with one because I don’t have one in my kit. Can I order one somewhere?


#31

You should email support@thimble.io directly about this.


#32

To solve this problem, I turned the lower motor mount bolt around with the head on the inside (by the battery holder) and nut on the outside.