Started by
JustAPotota54
on
Topic category: Help with MCreator software
I'm playing around with making a mod using MCLink, and I want to make a block that has a controllable light value base on analog input from Arduino. I have a procedure set up that runs every 10 ticks that maps the analog input (0-1023) to the block light value (0-1). It works, but only changes light value when placed. The light also persists even after the block has been destroyed. Here's my code:
double pot_range = 0;
double light_range = 0;
double pot_start = 0;
double pot_end = 1023;
double light_start = 0;
double light_end = 1;
double input = 0;
pot_start = ((double) 0);
pot_end = ((double) 1023);
light_start = ((double) 0);
light_end = ((double) 1);
input = ((double) (CurrentDevice.analogRead((int) 0)));
pot_range = ((double) (pot_end - pot_start));
light_range = ((double) (light_end - light_start));
block.setLightLevel((float) ((((input - pot_start) * light_range) / pot_range) + light_start));
The mapping algorithm is
output = (input - input_start)*output_range / input_range + output_start;
Did you use procedures to control light level? Because for some reason setting light level does not seem to work from procedures. We are working on fixing this issue in the upcoming updates.
It could be also that you are only setting the value on block placement. Try setting the light value on tick update. And right now the light value is shared across all blocks.
For the light value not changing, I have found the fix too. You need to call the update/notify block procedure block after you set the light value.
1. Yes, I am using procedures, I just figured it would be easier to copy and paste with the code.
2. It is a procedure running every update tick.
3. Adding the update/notify block didn't help.
Another option would be to remove the block on the tick update, set the light value and place it back after a new light value is set.