Recommended Posts

I'm missing a place where Powered Up code can be gathered and explained so other guys can reuse it or find inspiration. I had a discussion with @Jim how that could work out in this forum. I was thinking about having something like the hall of fame for code which underwent optimizations in a dedicated thread and which was proven to work. But this might lead to several thread which are prone to be inactive. So we decided to start with a single thread to publish PU code and discuss it. If it gets to chaotic We could have two separate thread. One for discussion and help request and the other one for working and optimized code. To keep things orderly, a general structure might be helpful.

So here we start with my proposal.

 

TL;DR:

Automatic brake lights based on speed reduction

 

Description:

The following code switches brakes lights on in case the speed is reduced. That works for reducing speed while driving forward and backward. When the speed reaches 0, the light are turned off. In this code I'm using PF lights controlled by the Boost color and distance sensor. But that can be replaced with PU lights.

Code:

800x400.png

800x400.png

Video

Declaration of variables:

a - Used to control the speed, is set by the slider 0

b - Speed (a) at the first point in time

c - Speed (a) at the second point in time

d - Speed difference between b and c

Detailed walkthrough:

The code block consists of three rows. The first row consists of a loop (left) and the code block to control the speed of the motor (right). Most of the code blocks in the loop are not needed. You actually just need the first block which sets constantly a value for the variable a to control the speed. The rest are widgets for testing timings.

The second row is the logic to detect speed differences. To do that constantly, a loop is used again. It simply reads a, the speed at different times. You can adjust the time between the measurments according to your needs. In my case 0,08s worked well. At the end of the loop the difference is set to the variable d. The value can be something between 100 and -100.

The third loop at the bottom contains the logic to turn the brake lights on or off. You will also notice the purple blocks. They are used to control the PF lights. I'll come to that in a bit. First i want to explain the logic to turn the lights off an on. There are three scenarios:

  1. Driving forwards
  2. Driving backwards
  3. Parking

When you are driving forward the max value of a is 100. Lets say the speed is suddenly reduced to 50. The difference is 50. When you are driving backwards the max value for a is -100. The speed is again reduced by 50. The difference is -50. When you are parking the difference is of course 0, because it doesn't change.

So i have two condition i want to check for:

  1. Am I driving backwards or forwards?
  2. Is there a speed difference?

I can exclude the third check, parking, by checking values for a that are greater or smaller than 0.

So, in one sentence: IF there is a speed difference AND the speed is GREATER THAN 0 OR IF there is a speed difference AND the speed is SMALLER THAN 0 then turn on the light (upper purple block at the end).

But how to control PF lights with Powered UP??? That is covered for example by @kbalage here.

The first purple code block on the left sets the port and the correct mode for the boost sensor. The two purple code blocks on the right side control the brightness of the PF lights. D is the port where the sensor is connected to, 3 is the channel 4 on the PF IR receiver, 5 is the red or blue port on the IR receiver, 7 switches the light to max brightness, 8 turns it off.

 

Edited by Andman
added video, fixed links

Share this post


Link to post
Share on other sites

Nice explanation of the code! Actually there was a similar topic created some time ago, although not exclusively for code sharing but for other Powered Up-related tips & tricks as well. Not sure if there's a need for a separate "PU code collection" topic, but if @Jim was ok with it then it should be fine :) It'll be a challenge to keep such topic clean since people will naturally react on the publications, just like I do right now. 

Share this post


Link to post
Share on other sites
10 hours ago, kbalage said:

Nice explanation of the code! Actually there was a similar topic created some time ago, although not exclusively for code sharing but for other Powered Up-related tips & tricks as well. Not sure if there's a need for a separate "PU code collection" topic, but if @Jim was ok with it then it should be fine :) It'll be a challenge to keep such topic clean since people will naturally react on the publications, just like I do right now. 

I discussed it with @Andman and we figured "let's see how this develops". Can you post a link to the topic you mentioned? Maybe we can merge both topics.

Share this post


Link to post
Share on other sites
1 hour ago, Jim said:

I discussed it with @Andman and we figured "let's see how this develops". Can you post a link to the topic you mentioned? Maybe we can merge both topics.

It is linked in my post above :) 

Share this post


Link to post
Share on other sites
4 minutes ago, kbalage said:

It is linked in my post above :) 

:laugh: Oops

Share this post


Link to post
Share on other sites
On 5/26/2021 at 8:37 AM, kbalage said:

Nice explanation of the code! Actually there was a similar topic created some time ago, although not exclusively for code sharing but for other Powered Up-related tips & tricks as well. Not sure if there's a need for a separate "PU code collection" topic, but if @Jim was ok with it then it should be fine :) It'll be a challenge to keep such topic clean since people will naturally react on the publications, just like I do right now. 

Oh right, and i was the last one who posted. Totally forgot about this thread. But then we already have a place to discuss. This thread then can be used to only publish working code and possibly optimized code. But back to topic. Here is another code which I'm going to use for my Steinwinter 2040.

TL;DR

Automatic turn lights

Description:

This code turns PF LEDs into automatic turn lights based on in which direction the wheel are steered.

Code:

800x400.png

800x400.png

Video

Declaration of variables:

a - controls steering angle, is set by the slider 0

b - sets the port used on the PF receiver (right and left turn light)

Detailed walk-through:

In the upper row are two Blocks. The right one controls the steering via the variable a and the slider on the left block. Note that the green block contains a multiplier. @allanp pointed it out in this post. Without the multiplier, the steering motor is constantly under load because it tries to steer further then possible. In my case 0.64 worked best. Tweak that value so it fits your needs. The turquoise block constantly updates steering position in the widget.

The lower row contains the logic. In one (or maybe more) sentence:

  1. IF the steering position IS GREATER THAN 0 OR IF the steering position IS LOWER THAN 0, THEN continue the logic.
  2. IF a IS GREATER THAN 0 THEN set b to channel left (4), ELSE set b to channel right(5).

The next purple block is setting the send mode (7) for the Boost Color and distance sensor. The sensor is connected to port D on the hub. After that next purple blocks will turn the light on and off with some delay. In my case half a second.

Edited by Andman
Typos, text format

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.