Recommended Posts

I have been using, and having a lot of success with the EV3 micropython programming language. But, I need some help. I can display the the triplets for the rgb(). What I would like to do is be able to separate out the rgb() tuples into three separate variables so that I can use them to determine various colored pieces of paper. A sample code would be most appreciated.


Share this post

Link to post
Share on other sites
Posted (edited)

You can separate them with commands like AND and a bitshift.

AND is 1 if two input values are 1 (and not 0), otherwise the AND is 0 (0 AND 1 is 0, 1 AND 1 is 1, 0 AND 0 is 0).
bitshift shifts the values of a variable a given amount to the left or to the right (0001 lshift 1 is equal 0010 (or 0011, depending on the implementation))

A pseudo-code would be:

int rbg = 110011001100b    (4 bit R, 4 bit G, 4 bit B, the output of the sensor in binary. You have to check the actual datatype/ bitlength of the output)
int val2 = 000011110000b    (1 for the bits of G, again assuming that one r/g/b value is 4 bits long)
int val2 = rgb AND val2    (it should be a bitwise AND. Now the content of val2 is 000011000000)
int val2 = val2 rightshift 4    (Now the content of val2 is 000000001100, again assuming that one r/g/b val is 4 bits long)

I don't know if python supports binary inputs. You'd have to "convert" val2 to HEX if not.

The commands for rightshift and AND are probably not AND or rshift. You should be able to find them on your own ;)

Edited by Tcm0

Share this post

Link to post
Share on other sites

I will see if I can find enough information with the micropython EV3 Lego documentation to try this. If not, maybe I can figure out how to do this from the micropython documentation. I was hoping for something along the lines like:

Rcolor = rgb(0)

Gcolor = rgb(1)

Bcolor = rgb(2)


Share this post

Link to post
Share on other sites

The pythonic way to do this would be:

r, g, b = color_sensor.rgb()

It can also be written as

(r, g, b) = color_sensor.rgb()

This is called "tuple unpacking".

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.