Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local previous_state = false -- Variable to store the previous state
- local current_counter = 0
- -- read previous counter from file
- local file = fs.open("counter.txt", "r")
- if file then
- local line = file.readLine()
- if line then
- current_counter = tonumber(line) or 0 -- Convert the line to a number, default to 0 if conversion fails
- end
- file.close()
- end
- local function setRedstone()
- -- values 0-9.
- -- back side is 0-9
- -- right side is 00-90
- -- front side is 000-900
- -- top side is 0000-9000
- local back = current_counter % 10 -- Get the last digit for the back side
- local right = math.floor(current_counter / 10) % 10 -- Get the second last digit for the right side
- local front = math.floor(current_counter / 100) % 10 -- Get the third last digit for the front side
- local top = math.floor(current_counter / 1000) % 10 -- Get the fourth last digit for the top side
- if (front == 0 and top > 0) then
- front = 10 -- Set front to 10 if it is 0
- end
- if (right == 0 and front > 0) then
- right = 10 -- Set right to 10 if it is 0
- end
- if (back == 0 and right > 0) then
- back = 10 -- Set back to 10 if it is 0
- end
- redstone.setAnalogOutput("back", back) -- Set the redstone signal on the back side
- redstone.setAnalogOutput("right", right) -- Set the redstone signal on the right side
- redstone.setAnalogOutput("front", front) -- Set the redstone signal on the front side
- redstone.setAnalogOutput("top", top) -- Set the redstone signal on the top side
- end
- setRedstone() -- Set the initial counter value
- -- function to save the counter to a file
- local function save_counter()
- local file = fs.open("counter.txt", "w")
- if file then
- file.writeLine(tostring(current_counter)) -- Write the counter to the file
- file.close()
- else
- print("Error: Unable to open counter.txt for writing.")
- end
- end
- while true do
- local current_state = redstone.getInput("left")
- if (current_state == previous_state) then
- os.sleep(1) -- Sleep for 1 second before checking the states again
- goto continue -- Skip to the next iteration of the loop
- end
- previous_state = current_state -- Update the previous state
- if (current_state) then
- current_counter = current_counter + 1 -- Increment the counter
- print("Counter: " .. current_counter) -- Log the counter to the console
- save_counter() -- Save the counter to the file
- end
- -- Emit redstone signal to the right side with the current counter value
- setRedstone()
- os.sleep(1)
- ::continue:: -- Label to continue to the next iteration of the loop
- end
Advertisement
Add Comment
Please, Sign In to add comment