print("Log voltage to file. Voltage measured on AIN1. Store value every 1 second for 10 seconds") --Requires SD Card installed inside the T7 or T7-Pro. --Requires FW 1.0150 or newer. On older firmware the file must exist already on the SD card --Older firmware uses "assert" command: file=assert(io.open(Filename, "w")) --timestamp (real-time-clock) available on T7-Pro only local hardware = MB.R(60010, 1) local passed = 1 if(bit.band(hardware, 8) ~= 8) then print("uSD card not detected") passed = 0 end if(bit.band(hardware, 4) ~= 4) then print("RTC module not detected") passed = 0 end if(passed == 0) then print("This Lua script requires an RTC module and a microSD card, but one or both are not detected. These features are only preinstalled on the T7-Pro. Script Stopping") MB.W(6000, 1, 0)--stop script end local mbRead=MB.R --local functions for faster processing local mbReadArray=MB.RA local Filename = "log2.csv" local voltage0 = 0 local voltage1 = 0 local voltage2 = 0 local voltage3 = 0 local voltage4 = 0 local voltage5 = 0 local voltage6 = 0 local voltage7 = 0 local voltage8 = 0 local count = 0 local count0 = 0 local delimiter = "," local dateStr = "" local voltageStr = "" local iLimit = 500 local bunchLimit = 10 local dataBunch = "" local localTimeEpoch = {} localTimeEpoch[1] = 0 localTimeEpoch[2] = 0 local file = io.open(Filename, "w") --create and open file for write access -- Make sure that the file was opened properly. if file then print("Opened File on uSD Card") else -- If the file was not opened properly we probably have a bad SD card. print("!! Failed to open file on uSD Card !!") end MB.W(48005, 0, 1) --ensure analog is on MB.W(43903, 0 ,6) -- write ain resolution index -- clock configuration for 10MHz ie 0.1 microseconds per count -- 44904 RollValue (max count) | 44908 clock counter (read mode) -- 44900 enable/disable clock | 44901 clock divisor MB.W(44900, 0, 0) --diable clock before configure it MB.W(44901, 0, 8) --divide by 8 ie 10MHz MB.W(44904, 1, 10000000) --max count set to 1 sec MB.W(44900, 0, 1) --enable clock LJ.IntervalConfig(0, 2) --set interval to 2 for 2ms local checkInterval=LJ.CheckInterval local timeCounterStart1 = mbRead(44908, 1) --reading start time from clock0 local timeCounterEnd1 = 0 while true do while true do localTimeEpoch, error = mbReadArray(61500, 1, 2) --reading RTC epoch core time if checkInterval(0) then --interval completed voltage0= mbRead(0, 3) --voltage on AIN0, address is 0, type is 3 voltage1= mbRead(2, 3) --voltage on AIN1, address is 2, type is 3 voltage2= mbRead(4, 3) --voltage on AIN2, address is 4, type is 3 voltage3= mbRead(6, 3) --voltage on AIN3, address is 6, type is 3 voltage4= mbRead(8, 3) --voltage on AIN4, address is 8, type is 3 voltage5= mbRead(10, 3) --voltage on AIN5, address is 10, type is 3 voltage6= mbRead(12, 3) --voltage on AIN6, address is 12, type is 3 voltage7= mbRead(14, 3) --voltage on AIN7, address is 14, type is 3 voltage8= mbRead(16, 3) --voltage on AIN8, address is 16, type is 3 timeCounterEnd1 = mbRead(44908, 1) --reading clock0 again local elapsedTime1 = 0 if timeCounterEnd1 < timeCounterStart1 then --lets calculate single AIN read elapsed time elapsedTime1 = 10000000 - timeCounterStart1 + timeCounterEnd1 --for timer count overflow else elapsedTime1 = timeCounterEnd1 - timeCounterStart1 end timeCounterStart1 = timeCounterEnd1 --save last time for next run dateStr = string.format("%u%u, %u", localTimeEpoch[1], localTimeEpoch[2],elapsedTime1) voltageStr = string.format("%.6f,%.6f,%.6f,%.6f,%.6f,%.6f,%.6f,%.6f,%.6f", voltage0, voltage1, voltage2, voltage3, voltage0, voltage4, voltage5, voltage6, voltage7, voltage8) dataBunch = dataBunch .. dateStr .. delimiter .. voltageStr .. "\n" --save data so we can write slower to file count0= count0 + 1 end if count0 >= bunchLimit then break end end file:write(dataBunch) dataBunch = "" count0 = 0; count = count + 1 if count >= iLimit then break end end file:close() print("Done acquiring data. Now read and display file contents. \n") print("Finished Script") MB.W(6000, 1, 0);