Programing the LEDs on the mbot Ranger


#1

I am learning how to program the Aruiga on the mbot Ranger. I used mBlock 5 to generate an Arduino C++ program to set the 12th LED to blink. That worked. But I have some questions about some of the lines of code. I tried to look at all the info I can find about the code below but I still have some questions. Here are my questions with some comments on what I think the code is doing.

MeRGBLed rgbled_0(0, 12); This line of code creates an object named rgbled_0 of type MeRGBLed. The parameters refers to port 0 and led # 12. So my question here is port 0 the port the LED ring is on?

rgbled_0.setpin(44); This code resets the LED available data pin at Arduino D44. What is the purpose of resetting the LED available pin?

rgbled_0.fillPixelsBak(0, 2, 1); The description for the function “fillPixelsBak” says that it “fill the LED color data to pixels_bak.” The parameters are the RGB values. What does this code do?
.
rgbled_0.setColor(1,255,0,0); I understand that this code sets the color of the LED, but I don’t understand the first parameter. The first parameter is the index of the LED that will be lit. My question is why is this value not 12 but instead is 1?

Thank you for you time.


#2

Hi.

Please see my documents over here: https://docs.google.com/document/d/1EpMWJo9pP2J_pstzXA-XHK8t00Z70SCZYwZ_Kl7VLuw/edit?usp=sharing They may be of help to you in your coding journey.

MeRGBLed rgbled_0(0, 12); This line of code creates an object named rgbled_0 of type MeRGBLed. The parameters refers to port 0 and led # 12. So my question here is port 0 the port the LED ring is on?

and…

rgbled_0.setpin(44); This code resets the LED available data pin at Arduino D44. What is the purpose of resetting the LED available pin?

The led ring is not on port 0, it is on pin 44. The Port 0 setting in the constructor is irrelevant because on the Auriga the first parameter is essentially ignored. The second parameter is telling the library how many LEDs are available on the board.

The setpin call is then simply telling the library which pin the led ring interface is attached to for use by later functions.

rgbled_0.fillPixelsBak(0, 2, 1); The description for the function “fillPixelsBak” says that it “fill the LED color data to pixels_bak.” The parameters are the RGB values. What does this code do?

fillPixelsBak simply sets the values in the pixels_bak array in the library - it’s a standard part of Makebot reset code. When you call the constructor, it creates two arrays - pixels[] and pixels_bak[]. When writing to the LED interface (in the show() function), their code compares pixels_bak[] and pixels[] and only writes if they are different. Not sure why they do this, but that’s what happens. This code I’m guessing, is simply there to set pixels_bak[] to something different to pixels[] so the first write works.

rgbled_0.setColor(1,255,0,0); I understand that this code sets the color of the LED, but I don’t understand the first parameter. The first parameter is the index of the LED that will be lit. My question is why is this value not 12 but instead is 1?

setColor() first parameter is the LED index from 1 to 12 (or zero for ALL leds) - it simply calls the function setColourAt() which takes a LED index from 0 to 11 and which writes the values to the pixels[] array for subsequent use by the show() function.

When using the setColor position, the LED indexes are:
LED 12 = 9 o’clock position
LED 3 = 12 o’clock position
LED 6 = 3 o’clock position
LED 9 = 6 o’clock position
When the power connector is at the bottom

When I used Mbot 5, if I use the ‘Turn on x light with color y’, the code I get is rgbled_0.setColor(x,yr,yg,yb); so I’m not sure why your experience is different, unless you’re mistaking the constructor call (MeRGBLed rgbled_0(0, 12);) as using LED number 12, which as I explain above is not correct.

There’s no documentation, so you have to work all this out by reading the sourcecode, sadly.


#3

Here’s my mBot code. You can see the numbers match .LedsCode


#4

Thank you Murray. Anther question is the line of code

randomseed… what does this do? I saw the same thing in my mblock code.


#5

Hi @georgebush. It simply sets the random number generator to an arbitrary place.

https://www.arduino.cc/reference/en/language/functions/random-numbers/randomseed/

I don’t think it’s actually used by the library though, I suspect it’s just some default initialisation code they stick into everything. (There is a 'Pick Random from x to y in the mBlock operators, so it’s probably to ensure that function is as random as possible)