This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Xixili's profile picture

RFID redstone

Started by Xixili, 07 February 2014 - 02:49 PM
Xixili #1
Posted 07 February 2014 - 03:49 PM
Hello everyone,

Im making a code for my metrosystem.

When you buy a ticket with a destination the cart should bring you there while carrying the RFID chip with you.

When you arrive near a station then the RFID reader should throw the switch so you arrive at the correct station.

I wrote down some code and it reacts on all rfid cards

this piece of code should only throw the switch if he sees a RFID card named with "central" and it should ignore all the other ones.
But it doesnt


reader = peripheral.wrap("left")
detected = false
central = false

while true do
if detected then
  if central then
  rs.setOutput("bottom",true)
  end
	sleep(3)
	rs.setOutput("bottom",false)
  
  end

  term.clear()
  term.setCursorPos(1,1)

  reader.scan() -- scans for 1 second

  detected = false
  while true do

	event, message = os.pullEvent()
  
	if event == "rfid_detected"then
	  detected = true
	
	  print("An RFID Card was detected")
	
	  print("Card Data: "..message)
	
	  if message == "central" then
		central = true
	  end
	elseif event == "rfid_scan_done" then
	  break
	end
end
end
Edited on 07 February 2014 - 02:50 PM
LBPHacker #2
Posted 07 February 2014 - 04:43 PM
-snip- Hold on. The indentation confused me.

EDIT: You might want to assign central to false as well after assigning detected to false (around line 20).
Edited on 07 February 2014 - 03:49 PM
Xixili #3
Posted 07 February 2014 - 05:03 PM
-snip- Hold on. The indentation confused me.

EDIT: You might want to assign central to false as well after assigning detected to false (around line 20).
That doesnt solve the problem
6677 #4
Posted 07 February 2014 - 05:56 PM
Your indentation is very poor. Fixed below:

reader = peripheral.wrap("left")
detected = false
central = false
while true do
	if detected then
		if central then
			rs.setOutput("bottom",true)
		end
		sleep(3)
		rs.setOutput("bottom",false)
			if message == "central" then
				central = true
			end
	end
	term.clear()term.setCursorPos(1,1)
	reader.scan() -- scans for 1 second
	detected = false
	while true do
		event, message = os.pullEvent()
		if event == "rfid_detected"then
			detected = true
			print("An RFID Card Was Detected")
			print("Card Data: "..message)
		elseif event == "rfid_scan_done" then
			break
		end
	end
end

When you indent your code properly the exact flow becomes more apparent.

Central is never set to false except for when you created the variable. Once you scan the central card the central variable will always be true. That is probably your issue.

You also use an unnecessary number of variables, loops and if statements for a fairly simple task.
Something that really helps some people plan their code is to draw a flowchart for what the program does or write out the psuedo code which is simply the steps you will take in plain english. You can often use either method to work out the exact logic you need and sometimes simplify it too.

Normally we dont post code here but this is what I came up with which would achieve the exact same task:

reader = peripheral.wrap("left")
while true do
	event, message = os.pullEvent()
	if event == "rfid_detected" then
		print("An RFID Card Was Detected")
		print("Card Data: "..message)
		if message == "central" then
			print("Correct Card")
			rs.setOutput("bottom", true)
			sleep(3)
			rs.setOutput("bottom", false)
		end
	end
end

Forum syntax highlighter sucks.
Edited on 08 February 2014 - 08:23 AM
Bomb Bloke #5
Posted 07 February 2014 - 06:26 PM
Erm, that could use another edit - your "fixed" indentation isn't exactly perfect, either.
Xixili #6
Posted 07 February 2014 - 06:54 PM
Your indentation is very poor. Fixed below:

reader = peripheral.wrap("left")
detected = false
central = false
while true do
	if detected then
		if central then
			rs.setOutput("bottom",true)
		end
		sleep(3)
		rs.setOutput("bottom",false)
			if message == "central" then
				central = true
			endend
	term.clear()term.setCursorPos(1,1)
	reader.scan() -- scans for 1 second
	detected = false
	while true do
		event, message = os.pullEvent()
		if event == "rfid_detected"then
			detected = true
			print("An RFID Card Was Detected")
			print("Card Data: "..message)
		elseif event == "rfid_scan_done" then
			break
		end
	end
end

When you indent your code properly the exact flow becomes more apparent.

Central is never set to false except for when you created the variable. Once you scan the central card the central variable will always be true. That is probably your issue.

You also use an unnecessary number of variables, loops and if statements for a fairly simple task.
Something that really helps some people plan their code is to draw a flowchart for what the program does or write out the psuedo code which is simply the steps you will take in plain english. You can often use either method to work out the exact logic you need and sometimes simplify it too.

Normally we dont post code here but this is what I came up with which would achieve the exact same task:

reader = peripheral.wrap("left")
while true do
	event, message = os.pullEvent()
	if event == "rfid_detected" then
		print("An RFID Card Was Detected")
		print("Card Data: "..message)
		if message == "central" then
			print("Correct Card")
			rs.setOutput("bottom", true)
			sleep(3)
			rs.setOutput("bottom", false)
		end
	end
end

Thank you for looking into it and writing a code but none of them work.

The first code sees the card but doesnt activate any redstone sadly.
And my knowledge is sadly not good enough to fix this.
Xixili #7
Posted 07 February 2014 - 07:00 PM
I changed my current code a little but it doesnt make it easier :(/>
At least it works for now


reader = peripheral.wrap("left")
detected = false
central = false
chapel = false
market = false
while true do
if detected then
  if central and not chapel then
  rs.setOutput("bottom",true)
 
  elseif central and not market then
  rs.setOutput("bottom",true)
  end
	    sleep(3)
	    rs.setOutput("bottom",false)
 
  central = false
  chapel = false
  market = false
 
  end
  term.clear()
  term.setCursorPos(1,1)
  reader.scan() -- scans for 1 second
  detected = false
  while true do
	    event, message = os.pullEvent()
 
	    if event == "rfid_detected"then
		  detected = true
	   
		  print("An RFID Card was detected")
	   
		  print("Card Data: "..message)
	   
	    if message == "central" then
			    central = true
  elseif message == "chapel" then
			    chapel = false
  elseif message == "market" then
			    market = false
		  end
	    elseif event == "rfid_scan_done" then
		  break
	    end
end
end
6677 #8
Posted 08 February 2014 - 09:23 AM
Erm, that could use another edit - your "fixed" indentation isn't exactly perfect, either.
That will be the forum syntax highlighter, looks just fine in Scite.

Proof:

Original source file before copying into the forum. I used spaces instead of tabs because that is my preference and then the forum choked and produced the crap above. There is no error in my indentation.



I tested the second code. It works. Nor does it have indentation errors as posted here beyond the forum syntax highlighter sucking and doubling my indents (which is not an indentation error either as indentation levels are entirely user preference with my preference being 4 spaces)