Jump to content




Refreshing Monitor With Changing Colours


  • You cannot reply to this topic
5 replies to this topic

#1 Kezaraux

  • Members
  • 12 posts

Posted 21 September 2013 - 10:02 AM

Hi, I'm trying to make a program that prints out to a monitor the percentage of power that I have in my MFSU that is also right next to the computer. I've managed to get the code to where it will print the first part of what I want it to print in white, the change the colour based on what the percentage is and then print the number with a percent sign. The only issue I'm running into is the refresh part. I have this doing it in a loop, but after it prints and works for the first time it just stops working. If someone could help me fix this and make it work that would be great!

Code:
Spoiler
Thanks to anyone who can help me out with this!

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 21 September 2013 - 10:26 AM

To refresh just put percent = round((power/cap)*100) inside of the loop (making sure to also put power and cap in there too) EDIT: Also you could make a function to do this too

However that being said there are some other things I've noticed you doing that you don't always need to do. Such as redirecting and restoring the terminal each loop (you can redirect once, then redirect back when a terminate event comes through), setting the text scale (only needs to be done once), and clearing the screen (just clear the line).

#3 Kezaraux

  • Members
  • 12 posts

Posted 21 September 2013 - 10:43 AM

Okay, I'll try this out, and thanks for letting me know about those others things. I just like to make sure that everything does what it's supposed to do every time and that's really the only way that I know how to do it.

#4 Kezaraux

  • Members
  • 12 posts

Posted 21 September 2013 - 10:46 AM

Well, that fixed it for me! Thanks! I also had to do the exact same thing in my Open Peripheral Terminal Glasses program... Guess I should remember to do this... XD

#5 BigTwisty

  • Members
  • 106 posts

Posted 21 September 2013 - 11:33 PM

The round function also seems a bit redundant. 10^0 is just 1. You are multiplying a number by 1, rounding it down, and the dividing it by 1. Why not just:
function round(num)
  return math.floor(num)
end

Although it would probably be more appropriate to simply use math.floor() inline.

Also, use Computercraft's constants to make your code more readable.

function chooseColor()
  if percent < 25 then
	return colors.red
  elseif percent < 50 then
	return colors.yellow
  end
  return colors.green
end


#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 September 2013 - 04:26 AM

View PostBigTwisty, on 21 September 2013 - 11:33 PM, said:

The round function also seems a bit redundant. 10^0 is just 1. You are multiplying a number by 1, rounding it down, and the dividing it by 1. Why not just:
function round(num)
  return math.floor(num)
end
Although it would probably be more appropriate to simply use math.floor() inline.
Because that's not rounding :P That's flooring.
local function round(num)
  return math.floor(num+0.5)
end
That's rounding

Although, this is a fully functional rounding, that supports preserving decimal places (as much as Lua can, it always removes trailing 0's)
local function round(num, idp)
  idp = 10^(idp or 0)
  if num >= 0 then
    return math.floor(num*idp+0.5)/idp
  end
  return math.ceil(num*idp-0.5)/idp
end






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users