Monitor not showing the program

Started by DarkLordALex, Mar 30, 2019, 06:47 PM

Previous topic - Next topic
Hi,
So i ran across a new program to have a realtime watch on my computer.
He did give a example-program which shows the time on a computer.

Problems are, that I am a beginner at Lua and don't know what i do wrong,
because in my opinion i don't have any mistakes in the activation part.

Original: https://pastebin.com/pshcjtwq

local localsavedrefresh = 180
 
local localsavedlast = localsavedrefresh

local saved = 0
 
function getTimestamp()
  http.request('http://neogriever.atwebpages.com/time.php')
  local requesting = true
  while requesting do
    local event, url, sourceText = os.pullEvent()
    if event == 'http_success' then
      local responseText = sourceText.readAll()
      sourceText.close()
      requesting = false
      return(responseText)
    elseif event == 'http_failure' then
      requesting = false
      return(saved) -- Wenn URL-Abfrage fehlschlägt, gespeicherten Zeitstempel nutzen
    end
  end
end
 
function convertTime ()
  if localsavedlast > localsavedrefresh then
    saved = getTimestamp()
    localsavedlast = 0
  end
  local zone = 1
  local d, h, m, s = 0
  s = (saved + (3600 * zone)) % 86400
  m = math.floor(s / 60)
  s = s - (m * 60)
  h = math.floor(m / 60)
  m = m - (h * 60)
  d = math.floor(h / 24)
  h = h - (d * 24)
  if string.len(tostring(h)) < 2 then
    h = '0' .. tostring(h)
  end
  if string.len(tostring(m)) < 2 then
    m = '0' .. tostring(m)
  end
  if string.len(tostring(s)) < 2 then
    s = '0' .. tostring(s)
  end
  return(h .. ":" .. m .. ":" .. s)
end
 
function secondUp ()
  while true do
    if saved ~= nil then
      saved = saved + 1
    end
    localsavedlast = localsavedlast + 1
    sleep(1)
  end
end
 
-- Beispiel-Nutzung
function showtime()
  while true do
    local term = peripheral.wrap("monitor_169")
    term.clear()
    term.setCursorPos(1,1)
    term.setTextSize(5)
    term.setTextColor(colors.blue)
    print(convertTime())
    sleep(1)
  end
end


I hope someone can help me with this.
Thynk you beforehand :)


Dog

First of all, what is the problem?  What does the program actually do?  Does it produce an error?  If so, what is that error?

FWIW, I see two things at a quick glance.

1. The script is trying to wrap "monitor_169" - is that the monitor you're actually trying to use?
2. The script is using setTextSize(5) on a monitor, which won't work - the correct command is setTextScale(5)

Quote from: Dog on Mar 31, 2019, 02:10 AMFirst of all, what is the problem?  What does the program actually do?  Does it produce an error?  If so, what is that error?

FWIW, I see two things at a quick glance.

1. The script is trying to wrap "monitor_169" - is that the monitor you're actually trying to use?
2. The script is using setTextSize(5) on a monitor, which won't work - the correct command is setTextScale(5)

It downs't give any errors.
When I run the program, there comes the shell:
   Press any key to continue.

The monitor stays black.

The monitor I use is called monitor_169 on the network.hell there is just a press a key to contnue

Dog

Is this the exact code you're running?  I ask because the code you posted won't run at all, it'll just drop back the to the shell when executed without doing anything.  The fact that you're seeing 'Press any key to continue' tells me that the code is actually running, which means the code you posted is not complete.

Before we can continue, please make sure the code posted is exactly the same as the code you're working with.

-- Alle 3 minuten den Timestamp vom Server holen. Hier diesen "Intervall" in Sekunden angeben.
local localsavedrefresh = 180
 
-- Start-refresh-festlegung, um einen Zeitstempel vom Server direkt beim Start zu holen.
local localsavedlast = localsavedrefresh
-- Speichervariable für den Zeitstempel
local saved = 0
 
-- Die Grundfunktion
function getTimestamp()
  http.request('http://neogriever.atwebpages.com/time.php')
  local requesting = true
  while requesting do
    local event, url, sourceText = os.pullEvent()
    if event == 'http_success' then
      local responseText = sourceText.readAll()
      sourceText.close()
      requesting = false
      return(responseText)
    elseif event == 'http_failure' then
      requesting = false
      return(saved) -- Wenn URL-Abfrage fehlschlägt, gespeicherten Zeitstempel nutzen
    end
  end
end
 
-- Die Umrechnungs-Funktion
function convertTime ()
  if localsavedlast > localsavedrefresh then
    saved = getTimestamp()
    localsavedlast = 0
  end
  local zone = 1
  local d, h, m, s = 0
  s = (saved + (3600 * zone)) % 86400
  m = math.floor(s / 60)
  s = s - (m * 60)
  h = math.floor(m / 60)
  m = m - (h * 60)
  d = math.floor(h / 24)
  h = h - (d * 24)
  if string.len(tostring(h)) < 2 then
    h = '0' .. tostring(h)
  end
  if string.len(tostring(m)) < 2 then
    m = '0' .. tostring(m)
  end
  if string.len(tostring(s)) < 2 then
    s = '0' .. tostring(s)
  end
  return(h .. ":" .. m .. ":" .. s)
end
 
-- Funktion zur Sekunden-Hochzählung des gespeicherten Zeitstempels
function secondUp ()
  while true do
    if saved ~= nil then
      saved = saved + 1
    end
    localsavedlast = localsavedlast + 1
    sleep(1)
  end
end
 
-- Beispiel-Nutzung
function showtime()
  while true do
    -- Im Computer testweise ausgeben
    term.clear()
    term.setCursorPos(1,1)
    -- convertTime() liefert stets den TimeZone+1 berechneten Timestamp in Form HH:MM:SS aus.
    print(convertTime())
    sleep(1)
  end
end
 
-- Als Parallel-Prozess!
parallel.waitForAll(secondUp, showtime)

With this code, it atleast runs inside the computer without any output.
So There is just the time and I can't write anymore.

Dog

OK, the only difference I see between the code you posted and the original code is a single change...

In the function getTimeStamp(), the first line (the http request) has a semi-colon added near the end of the line in your version - remove that semi-colon and the code should work.  Based on that, I would guess that your code with the monitor would also start working if you removed the semi-colon and changed setTextSize() to setTextScale().


Quote from: Dog on Mar 31, 2019, 04:00 PMOK, the only difference I see between the code you posted and the original code is a single change...

In the function getTimeStamp(), the first line (the http request) has a semi-colon added near the end of the line in your version - remove that semi-colon and the code should work.  Based on that, I would guess that your code with the monitor would also start working if you removed the semi-colon and changed setTextSize() to setTextScale().


I looked at the syntax and removed the semicolon, chanegd the syntax to term.setTextScale(5).
But still nothing.

It still just does nothing, but it may have something to do with the disk it starts from. I don't know.

Dog

#7
Please repost your code with the edits - I'd like to compare what you're running now with the original.  Also, what size is monitor_169?  Finally, what version of MC and CC are you using?

I'm using minecraft 1.7.10 and CC 1.75


The monitor is 7x5.



local localsavedrefresh = 180
 
local localsavedlast = localsavedrefresh

local saved = 0
 
function getTimestamp()
  http.request('http://neogriever.atwebpages.com/time.php')
  local requesting = true
  while requesting do
    local event, url, sourceText = os.pullEvent()
    if event == 'http_success' then
      local responseText = sourceText.readAll()
      sourceText.close()
      requesting = false
      return(responseText)
    elseif event == 'http_failure' then
      requesting = false
      return(saved) -- Wenn URL-Abfrage fehlschlägt, gespeicherten Zeitstempel nutzen
    end
  end
end
 
function convertTime ()
  if localsavedlast > localsavedrefresh then
    saved = getTimestamp()
    localsavedlast = 0
  end
  local zone = 1
  local d, h, m, s = 0
  s = (saved + (3600 * zone)) % 86400
  m = math.floor(s / 60)
  s = s - (m * 60)
  h = math.floor(m / 60)
  m = m - (h * 60)
  d = math.floor(h / 24)
  h = h - (d * 24)
  if string.len(tostring(h)) < 2 then
    h = '0' .. tostring(h)
  end
  if string.len(tostring(m)) < 2 then
    m = '0' .. tostring(m)
  end
  if string.len(tostring(s)) < 2 then
    s = '0' .. tostring(s)
  end
  return(h .. ":" .. m .. ":" .. s)
end
 
function secondUp ()
  while true do
    if saved ~= nil then
      saved = saved + 1
    end
    localsavedlast = localsavedlast + 1
    sleep(1)
  end
end
 
-- Beispiel-Nutzung
function showtime()
  while true do
    local term = peripheral.wrap("monitor_169")
    term.clear()
    term.setCursorPos(1,1)
    term.setTextScale(5)
    term.setTextColor(colors.blue)
    print(convertTime())
    sleep(1)
  end
end

Dog

If what you've posted is the full script, the reason it's not running is because you're never calling any of the functions.  You've forgotten the last line of the program...

parallel.waitForAny(secondUp, showtime)

Also - don't use the variable term to wrap the monitor - you're overwriting the built in terminal functions by overwriting term.  If that's not already causing problems, it might when you get the script running.  I recommend using mon, monitor, or some other variable name that reflects what you're actually wrapping.

Now it launches and I can't type on the computer anymore.

When I did misstype something the error message showed on the monitor, which i conclude as good news.

Bad though I can't seem to get the time out of this even after I added the
   parallel.waitForAll(secondUp, showtime)
but it required me to add () behind both functions.

so now i have this code at the end

function showtime()
  while true do
    local mon = peripheral.wrap("monitor_169")
    term.clear()
    term.setCursorPos(1,1)
    term.setTextSize(5)
    term.setTextColor(colors.blue)
    print(convertTime())
    sleep(1)
  end
end

parallel.waitForAll(secondUp(), showtime())

which doesn't give any output at all.

So I tried
function showtime()
  while true do
    local mon = peripheral.wrap("monitor_169")
    term.clear()
    term.setCursorPos(1,1)
    term.setTextSize(5)
    term.setTextColor(colors.blue)
    mon.write(convertTime())
    sleep(1)
  end
end

parallel.waitForAll(secondUp(), showtime())

Dog

#11
Part of the reason it's not working is because you've added () to the end of the function names in the parallel call.  You're passing the results of the functions to parallel when you add the parenthesis instead of passing the actual functions.  You need to remove the parenthesis after the function names in the parallel call.  It should look exactly as I typed it in my previous reply.

When I asked you to post the code with the edits, you still posted code with the semi-colon in place - that needs to be removed.  I realize you said you removed it, but the code you posted still has it.

Also in your recent examples, you're again using setTextSize when there is no such call (it's setTextScale).  You're also doing most of your work on the terminal, not the monitor - you're still using term calls for cursorPos, textColor, clear, etc.

I just tested the script, and while it's slow the way it's written, it works fine for me - it just takes several seconds before it displays anything.  Part of the reason for the slowness is because the script is wrapping the monitor over and over again in the loop, when the monitor should be wrapped only once at the beginning of the program.

To sum up:

- Make sure the semi-colon is removed.
- replace your term calls with mon calls - e.g. mon.setTextScale(5), mon.setCursorPos(1, 1)
- move the line that wraps the monitor out of the loop and to the beginning of the program

Once you've made those changes, test it out, and if it still isn't working, please post the code with the edits and let me know what it's doing and what it's not doing.  We're almost there - it's working for me, so now we just need to get it working for you.

Tank you for your help.

The code is nworking now.

Have a good day.