--codeExampleStart 1 ----------------------------------------------------------- -- Hello World! for the console print( "Hello World!" ) ="Hello World!" s = "Hello World!" print( s ) --codeExampleEnd 1 --codeExampleStart 4 ----------------------------------------------------------- -- Loops - how fast are they? t=nxt.TimerRead() for i=1,10000 do end =nxt.TimerRead()-t t=nxt.TimerRead() for i=1,10000 do j=i*4 end =nxt.TimerRead()-t =j t=nxt.TimerRead() for i=1,10000 do k=i*7 j=i*4+k end =nxt.TimerRead()-t =k,j --codeExampleEnd 4 --codeExampleStart 5 ----------------------------------------------------------- -- Lua arrays fruits = { "apples", "oranges", "bananas", "plums", "grapefruit" } for n,v in ipairs(fruits) do print( n, v ) end table.sort(fruits) for n,v in ipairs(fruits) do print( n, v ) end --codeExampleEnd 5 --codeExampleStart 6 ----------------------------------------------------------- -- Lua associative tables inventory = { apples=5, oranges=2, bananas=0, plums=4, grapefruit=1 } for k,v in pairs(inventory) do print( k, v ) end inventory.apples = inventory.apples + 3 inventory.plums = inventory.plums - 2 for k,v in pairs(inventory) do print( k, v ) end --codeExampleEnd 6 --codeExampleStart 1 ----------------------------------------------------------- -- Hello World! for the LCD nxt.DisplayClear() nxt.DisplayText( "Hello World!" ) --codeExampleEnd 1 --codeExampleStart 2 ----------------------------------------------------------- -- Demonstration of setting random display pixels function RandomDisplayPixel() local x,y nxt.DisplayClear() for i=1,10000 do x = nxt.random(100) y = nxt.random(64) nxt.DisplayPixel(x,y) end end -- Another way of writing this without local variables is function RandomDisplayPixel() nxt.DisplayClear() for i=1,10000 do nxt.DisplayPixel(nxt.random(100),nxt.random(64)) end end --codeExampleEnd 2 --codeExampleStart 3 ----------------------------------------------------------- -- Reading the Display and Writing it to the Console function DumpDisplay() for x=0,99 do s = "" for y=0,63 do if 0 ~= nxt.DisplayGetPixel(x,63-y) then s = s .. "*" else s = s .. " " end end print( s ) end end --codeExampleEnd 3 --codeExampleStart 3 ----------------------------------------------------------- -- Read sound sensor until the orange button is pressed function SoundRead(port) nxt.InputSetType(port,0) nxt.InputSetState(port,1,0) nxt.InputSetDir(port,1,1) repeat print( nxt.TimerRead(), nxt.InputGetStatus(port) ) until( 8 == nxt.ButtonRead() ) end -- And using the function - press the orange button on the NXT to stop it SoundRead(2) --codeExampleEnd 3 --codeExampleStart 4 ----------------------------------------------------------- -- Read sound sensor and scale the results function SoundScale(port,sensitive) nxt.InputSetType(port,0) sensitive = sensitive or 0 if 0 == sensitive then nxt.InputSetState(port,0,1) else nxt.InputSetState(port,1,0) end nxt.InputSetDir(port,1,1) repeat local raw = nxt.InputGetStatus(port) -- Check if raw value is less than minimum, adjust if needed if raw < 162 then raw = 0 else raw = raw - 162 end -- Scale the result raw = (raw*100)/83 -- Check if raw value is greater than maximum, adjust if needed if raw > 1023 then raw = 1023 end -- Invert the raw value raw = 1023 - raw -- Make a string that represents the volume... print( nxt.TimerRead(), string.rep("*", raw/16 ) ) until( 8 == nxt.ButtonRead() ) end -- And using the function - press the orange button on the NXT to stop it SoundScale(2,0) --codeExampleEnd 4