Analog data logger

picaxester

Senior Member
Heres an analog data logger program that I'm working on. It's not finished and still needs a lot of work.
It uses FreeBASIC and a PICAXE 08M to read three analog voltages and then sends them to my pc through the serial port.

Video



FB code:
Code:
dim as integer x, y, buttons, i, ii, ra1, ra2, rb1, rb2, rc1, rc2, graphpos
dim as double logtime
dim as string s, text, filename, temp, device_name, ra1s, ra2s, rb1s, rb2s, rc1s, rc2s

declare sub bttn(x as integer, y as integer, s as string)
declare sub buttonup(x as integer, y as integer, s as integer)
declare sub buttondown(x as integer, y as integer, s as integer)
declare sub inbox(x as integer, y as integer, w as integer, h as integer)
declare sub clearbox(x as integer, y as integer)
declare sub label(x as integer, y as integer, s as string)

screenres 800,335
color 0, 15
cls

bttn (555, 14, "START")	'Button #1
bttn (619, 14, "STOP")	'Button #2
bttn (675, 14, "CLEAR")	'Button #3
bttn (739, 14, "EXIT")	'Button #4
inbox (51, 14, 163, 26)	'In/Out box #1
inbox (275, 14, 51, 26)	'In/Out box #2
locate 4, 37: color 0,15: print "mins"
inbox (403, 14, 131, 26)	'In/Out box #3
locate 4, 52: color 0,15
#IFDEF __FB_LINUX__
	print "/dev/ttyS0"
#ENDIF
#IFDEF __FB_WIN32__
	print "COM1"
#ENDIF
#IFDEF __FB_DOS__
	print "COM1"
#ENDIF
inbox (11, 54, 771, 269)	'In/Out box #4 314
label (11, 16, "FILE")	'Label #1
label (11, 32, "NAME")	'Label #2
label (235, 16, "LOG")	'Label #3
label (235, 32, "TIME")	'Label #4
label (347, 16, "DEVICE")	'Label #5
label (347, 32, "NAME")	'Label #6

for y = 61 to 330 step 15
	line (14, y)-(783, y), 7
next y

logtime = 1
graphpos = 14
device_name = "/dev/ttyS0"

do
	do while buttons < 1
		getmouse (x, y, , buttons)
		s = inkey$
		if s = chr(255) & "k" or s = chr(27) then end
		sleep 10
	loop

'BUTTONS========================================================
'START
	if x > 555 and x < 609 and y > 14 and y < 44 then
		buttondown (558, 43, 50)
		open filename & ".txt" for binary as #1
		#IFDEF __FB_LINUX__
			shell "stty -F " & device_name & " speed 4800"
			Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
		#ENDIF

		#IFDEF __FB_WIN32__
			Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
		#ENDIF
		 
		#IFDEF __FB_DOS__
			Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
		#ENDIF
		ra2 = 316
		rb2 = 316
		rc2 = 316
		'print #2, "B"
		sleep 100, 1
		for i = graphpos to 784 'step 5
			print #2, chr(66)
			sleep 40, 1
			ra1 = val(Input(LOC(1), 2))
			print #2, chr(82)
			sleep 40, 1
			rb1 = val(Input(LOC(1), 2))
			print #2, chr(71)
			sleep 40, 1
			rc1 = val(Input(LOC(1), 2))
			if ra1 > 255 or ra1 < 0 then ra1 = 0
			if rb1 > 255 or rb1 < 0 then rb1 = 0
			if rc1 > 255 or rc1 < 0 then rc1 = 0
			print #1, ra1 & "	Blue"
			print #1, rb1 & "	Red"
			print #1, rc1 & "	Green"
			ra1 = 316 - ra1
			rb1 = 316 - rb1
			rc1 = 316 - rc1
			if ra2 > 316 or ra2 < 61 then ra2 = 316
			if rb2 > 316 or rb2 < 61 then rb2 = 316
			if rc2 > 316 or rc2 < 61 then rc2 = 316
			line (i, ra1)-(i - 1, ra2), 1
			line (i, rb1)-(i - 1, rb2), 4
			line (i, rc1)-(i - 1, rc2), 2
			ra2 = ra1
			rb2 = rb1
			rc2 = rc1
			for ii = 1 to logtime
				getmouse (x, y, , buttons)
				if x > 619 and x < 665 and y > 14 and y < 44 and buttons = 1 then
					graphpos = i
					exit for, for
				end if
				if x > 675 and x < 729 and y > 14 and y < 44 and buttons = 1 then goto cleargraph
				if x > 739 and x < 785 and y > 14 and y < 44 and buttons = 1 then end
				sleep 20, 1
			next ii
		next i
		close #1
		close #2
		
'STOP
	elseif x > 619 and x < 665 and y > 14 and y < 44 then
		buttondown (622, 43, 42)
		
'CLEAR
	elseif x > 675 and x < 729 and y > 14 and y < 44 then
	cleargraph:
		buttondown (678, 43, 50)
		clearbox (14, 57)
		graphpos = 14
		for y = 61 to 330 step 15
			line (14, y)-(783, y), 7
		next y
		close #1
		close #2
		kill filename & ".txt"
		
'EXIT
	elseif x > 739 and x < 785 and y > 14 and y < 44 then
		buttondown (742, 43, 42)
		close #1
		close #2
		end
		
'IN/OUT BOXES===================================================
'Box #1
	elseif x > 51 and x < 218 and y > 14 and y < 43 then
		clearbox (54, 17)
		clearbox (14, 57)
		graphpos = 14
		for y = 61 to 330 step 15
			line (14, y)-(783, y), 7
		next y
		
		
		locate 4, 8: color 0,15: print "_"
		filename = ""
		do
			getmouse (x, y, , buttons)
			if buttons > 0 and x < 51 or buttons > 0 and x > 218 or buttons > 0 and y < 14 or buttons > 0 and y > 43 then exit do
			s = inkey$
			if s = chr(255) & "k" or s = chr(27) then end
			if s = chr(13) then exit do
			if s = chr(8) and len(filename) > 1 then
				clearbox (54, 17)
				filename = left(filename, len(filename))
				locate 4, 8: color 0,15: print filename & "_"
			end if
			if s > "" and len(filename) < 19 then
				clearbox (54, 17)
				filename = filename + s
				locate 4, 8: color 0,15: print filename & "_"
			end if
			sleep 20
		loop
		clearbox (54, 17)
		locate 4, 8: color 0,15: print filename
		
		close #1
		open filename & ".txt" for binary as #1
		if lof(1) > 0 Then
			
			for i = 14 to 784 'step 5
			line input #1, ra1s
			line input #1, rb1s
			line input #1, rc1s
			ra1 = val(ra1s)
			rb1 = val(rb1s)
			rc1 = val(rc1s)
			if ra1 > 255 or ra1 < 0 then ra1 = 0
			if rb1 > 255 or rb1 < 0 then rb1 = 0
			if rc1 > 255 or rc1 < 0 then rc1 = 0
			ra1 = 316 - ra1
			rb1 = 316 - rb1
			rc1 = 316 - rc1
			if ra2 > 316 or ra2 < 61 then ra2 = 316
			if rb2 > 316 or rb2 < 61 then rb2 = 316
			if rc2 > 316 or rc2 < 61 then rc2 = 316
			line (i, ra1)-(i - 1, ra2), 1
			line (i, rb1)-(i - 1, rb2), 4
			line (i, rc1)-(i - 1, rc2), 2
			ra2 = ra1
			rb2 = rb1
			rc2 = rc1
		next i
		else
			clearbox (14, 57)
			for y = 61 to 330 step 15
				line (14, y)-(783, y), 7
			next y
		end if
		
'Box #2
	elseif x > 275 and x < 330 and y > 14 and y < 43 then
		clearbox (278, 17)
		locate 4, 36: color 0,15: input "", logtime
		logtime = logtime * 60
		logtime = int(logtime * .18)
		
'Box #3
	elseif x > 403 and x < 538 and y > 14 and y < 43 then
		clearbox (406, 17)
		close #2
		locate 4, 52: color 0,15: input "", device_name
		
'Box #4
	elseif x > 11 and x < 786 and y > 54 and y < 371 then
		'clearbox (14, 57)
		'locate 9, 3: color 0,15: print "???"
		
	end if

	sleep 10
	getmouse (x, y, , buttons)
	do while x > 1 and buttons > 0: getmouse (x, y, , buttons): sleep 10: loop
	buttonup (556,39,50)
	buttonup (620,39,42)
	buttonup (676,39,50)
	buttonup (740,39,42)
loop

sub bttn(x as integer, y as integer, s as string)
	dim as integer w
	w = len(s) * 8 + 10
	draw string (x + 8, y + 10), s
	draw "C0 BM" & x & "," & y & "D26 R D R D R" & w & "U R U R U26 L U L U L" & w & "D L D L C7 BM" & x + 1 & "," & y + 1 & "BD26 BR BD BR BD R" & w & "U R U R U26 C0"
end sub

sub buttonup(x as integer, y as integer, w as integer)
	draw "C15 BM" & x & "," & y & "U24 R U R U R" & w - 2 & "C7 BM" & x + 2 & "," & y + 4 & "R" & w & "U R U R U26"
end sub

sub buttondown(x as integer, y as integer, w as integer)
	draw "C15 BM" & x & "," & y & " R " & w & " U R U R U26 C7 BM" & x - 2 & "," & y - 4 & " U24 R U R U R" & w - 2
end sub

sub inbox(x as integer, y as integer, w as integer, h as integer)
	draw "C0 BM" & x & "," & y & " D" & h & " R D R D R" & w & " U R U R U" & h & " L U L U L" & w & " D L D L C7 BM" & x + 1 & "," & y + 1 & " D" & h - 2 & " R D R D R" & w - 2 & " U R U R U" & h - 2 & " L U L U L" & w - 2 & "D L D"
end sub

sub clearbox(x as integer, y as integer)
	draw "BM" & x & "," & y & "P15,7 C0"
end sub

sub label(x as integer, y as integer, s as string)
	draw string (x, y), s, 0
end sub
PICAXE code:
Code:
setfreq m8

do

	serin 3, N2400, b0

	if b0 = "B" then

		readadc 1, b1

		serout 0, N2400, (#b1)

		

	elseif b0 = "R" then

		readadc 2, b2

		serout 0, N2400, (#b2)

		

	elseif b0 = "G" then

		readadc 4, b3

		serout 0, N2400, (#b3)

		

	endif

loop
 

hippy

Senior Member
I note the /dev/ttyS0 in picaxester's application so presume other existing data loggers either did not do what was required in this particular case or would not run on the OS of choice.
 

lbenson

Senior Member
Nice animation. It would certainly be exciting to see that appear on your screen compared to staring at a list of varying ADC values. For curiosity's sake, what were you using to generate your ADC signals? I note that RED is almost the mirror image of GREEN. Time tics and time labels would be useful--also for voltage, tho the lines help.

In my own code, in order to keep very long lines visible, I'm inclined to do something like the following.
Code:
A =" C0 BM" & x & "," & y & " D" & h & " R D R D R" & w & " U R U R U" & h & " L U L U L" 
B = w & " D L D L C7 BM" & x + 1 & "," & y + 1 & " D" & h - 2 & " R D R D R" 
C = w - 2 & " U R U R U" & h - 2 & " L U L U L" & w - 2 & "D L D"
D = A & B & C
draw D
This would also help on the display here.


Now, with an X2 at 40mHz ...
 

picaxester

Senior Member
I'm using two photo resistors (opposite of each other) and a photo transistor.
I'll be adding more to it yet, first I got to get it to work under windows.

Most of the code is from a gui code maker that I made.
 

alexthefox

Senior Member
hi, nice work, i try to run your job, but when i run the sw, i get this error:

Compiler output:
C:\Temp\test.bas(54) : error 21: Type mismatch, found: 'then'
if s = chr(255) & "k" or s = chr(27) then end



what do you think?


PROBLEM SOLVED!!!!

Hi,i install FBIDE last version and it work.... sorry
and thk
 
Last edited:

picaxester

Senior Member
It doesn't seam to work in windows but I'm working on it.

The problem lines are: (receives the data from serial port)
ra1 = val(Input(LOC(1), 2))
rb1 = val(Input(LOC(1), 2))
rc1 = val(Input(LOC(1), 2))

It didn't work in Linux either until this was added:
shell "stty -F " & device_name & " speed 4800"

Is there a way to manually set the baud rate in Windows?
 

GreenLeader

Senior Member
Hi
That looks very good - I want something like that too! Its on my to do list, but I've got no Basic experience yet...Hope you keep us updated!

I've played with StampPlot a bit. Its not very intuitive, there's no overview help - dives straight into detail. Also seems a bit quirky - sometimes it seems to work fine, other times just won't work. I am sure with more playing around I might work out what's wrong...
 

picaxester

Senior Member
It works in Windows now :D

Remember, after downloading the PICAXE code, switch the serial in wire from the serial input pin (lag 2) to pin3 (lag 4) and use a diode from 5V+ to pin3 like it says in the manual (manual #2 page 156).


Code:
Dim As Integer x, y, buttons, i, ii, ra1, ra2, rb1, rb2, rc1, rc2, graphpos 
dim as string s, text, filename, logtime, temp, device_name, ra1s, ra2s, rb1s, rb2s, rc1s, rc2s

declare sub bttn(x as integer, y as integer, s as string)
declare sub buttonup(x as integer, y as integer, s as integer)
declare sub buttondown(x as integer, y as integer, s as integer)
declare sub inbox(x as integer, y as integer, w as integer, h as integer)
declare sub clearbox(x as integer, y as integer)
declare sub label(x as integer, y as integer, s as string)

screenres 800,335
color 0, 15
cls

bttn (555, 14, "START")
bttn (619, 14, "STOP")
bttn (675, 14, "CLEAR")
bttn (739, 14, "EXIT")
inbox (51, 14, 163, 26)
inbox (275, 14, 51, 26)
locate 4, 37: color 0,15: print "mins"
inbox (403, 14, 131, 26)
locate 4, 52: color 0,15
#IFDEF __FB_LINUX__
	print "/dev/ttyS0"
	device_name = "/dev/ttyS0"
#ENDIF
#IFDEF __FB_WIN32__
	print "COM1"
	device_name = "COM1"
#ENDIF
#IFDEF __FB_DOS__
	print "COM1"
	device_name = "COM1"
#ENDIF
inbox (11, 54, 771, 269)
label (11, 16, "FILE")
label (11, 32, "NAME")
label (235, 16, "LOG")
label (235, 32, "TIME")
label (347, 16, "DEVICE")
label (347, 32, "NAME")

for y = 61 to 330 step 15
	line (14, y)-(783, y), 7
next y

logtime = "1"
graphpos = 14

do
	do while buttons < 1
		getmouse (x, y, , buttons)
		s = inkey$
		if s = chr(255) & "k" or s = chr(27) then end
		sleep 10
	loop

'BUTTONS========================================================
'START
	if x > 555 and x < 609 and y > 14 and y < 44 then
		buttondown (558, 43, 50)
		open filename & ".txt" for binary as #1
		#IFDEF __FB_LINUX__
			shell "stty -F " & device_name & " speed 4800"
			Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
		#ENDIF

		#IFDEF __FB_WIN32__
			open com "COM1:4800,n,8,1,cs0,cd0,ds0,rs" as #2
		#ENDIF
		 
		#IFDEF __FB_DOS__
			Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
		#ENDIF
		ra2 = 316
		rb2 = 316
		rc2 = 316
		sleep 100, 1
		For i = graphpos To 784
			print #2, chr(66)
			sleep 40, 1
			ra1 = Val(Input(Loc(2), 2))
			Print #2, Chr(82)
			Sleep 40, 1
			rb1 = Val(Input(Loc(2), 2))
			Print #2, Chr(71)
			Sleep 40, 1
			rc1 = Val(Input(Loc(2), 2))
			if ra1 > 255 or ra1 < 0 then ra1 = 0
			if rb1 > 255 or rb1 < 0 then rb1 = 0
			if rc1 > 255 or rc1 < 0 then rc1 = 0
			print #1, ra1 & "	Blue"
			print #1, rb1 & "	Red"
			print #1, rc1 & "	Green"
			ra1 = 316 - ra1
			rb1 = 316 - rb1
			rc1 = 316 - rc1
			if ra2 > 316 or ra2 < 61 then ra2 = 316
			if rb2 > 316 or rb2 < 61 then rb2 = 316
			if rc2 > 316 or rc2 < 61 then rc2 = 316
			line (i, ra1)-(i - 1, ra2), 1
			line (i, rb1)-(i - 1, rb2), 4
			line (i, rc1)-(i - 1, rc2), 2
			ra2 = ra1
			rb2 = rb1
			rc2 = rc1
			for ii = 1 to val(logtime)
				getmouse (x, y, , buttons)
				if x > 619 and x < 665 and y > 14 and y < 44 and buttons = 1 then
					graphpos = i
					exit for, for
				end if
				if x > 675 and x < 729 and y > 14 and y < 44 and buttons = 1 then goto cleargraph
				if x > 739 and x < 785 and y > 14 and y < 44 and buttons = 1 then end
				sleep 20, 1
			next ii
		next i
		close #1
		close #2
		
'STOP
	elseif x > 619 and x < 665 and y > 14 and y < 44 then
		buttondown (622, 43, 42)
		
'CLEAR
	elseif x > 675 and x < 729 and y > 14 and y < 44 then
	cleargraph:
		buttondown (678, 43, 50)
		clearbox (14, 57)
		graphpos = 14
		for y = 61 to 330 step 15
			line (14, y)-(783, y), 7
		next y
		close #1
		close #2
		kill filename & ".txt"
		
'EXIT
	elseif x > 739 and x < 785 and y > 14 and y < 44 then
		buttondown (742, 43, 42)
		close #1
		close #2
		end
		
'IN/OUT BOXES===================================================
'Box #1
	elseif x > 51 and x < 218 and y > 14 and y < 43 then
		clearbox (54, 17)
		clearbox (14, 57)
		graphpos = 14
		for y = 61 to 330 step 15
			line (14, y)-(783, y), 7
		next y
		
		Locate 4, 8: Color 0,15: Print "_"
		filename = ""
		Do
			getmouse (x, y, , buttons)
			if buttons > 0 and x < 51 or buttons > 0 and x > 218 or buttons > 0 and y < 14 or buttons > 0 and y > 43 then exit do
				s = Inkey$
				If s = Chr(255) & "k" Or s = Chr(27) Then End
				If s = Chr(13) Then Exit Do
				if s = chr(8) and len(filename) > 0 then
					clearbox (54, 17)
					filename = left(filename, len(filename) - 1)
					locate 4, 8: color 0,15: print filename & "_"
				end if
				If s > "" And Len(filename) < 19 And s <> Chr(8) Then
					clearbox (54, 17)
					filename = filename + s
					locate 4, 8: color 0,15: print filename & "_"
				End If
				Sleep 20
		Loop
		
		clearbox (54, 17)
		locate 4, 8: color 0,15: print filename
		
		close #1
		open filename & ".txt" for binary as #1
		if lof(1) > 0 Then
			
			For i = 14 To 784
			line input #1, ra1s
			line input #1, rb1s
			line input #1, rc1s
			ra1 = val(ra1s)
			rb1 = val(rb1s)
			rc1 = val(rc1s)
			if ra1 > 255 or ra1 < 0 then ra1 = 0
			if rb1 > 255 or rb1 < 0 then rb1 = 0
			if rc1 > 255 or rc1 < 0 then rc1 = 0
			ra1 = 316 - ra1
			rb1 = 316 - rb1
			rc1 = 316 - rc1
			if ra2 > 316 or ra2 < 61 then ra2 = 316
			if rb2 > 316 or rb2 < 61 then rb2 = 316
			if rc2 > 316 or rc2 < 61 then rc2 = 316
			line (i, ra1)-(i - 1, ra2), 1
			line (i, rb1)-(i - 1, rb2), 4
			line (i, rc1)-(i - 1, rc2), 2
			ra2 = ra1
			rb2 = rb1
			rc2 = rc1
		next i
		else
			clearbox (14, 57)
			for y = 61 to 330 step 15
				line (14, y)-(783, y), 7
			next y
		end if
		
'Box #2
	elseif x > 275 and x < 330 and y > 14 and y < 43 then
		clearbox (278, 17)
		Locate 4, 36: Color 0,15: Print "_"
		logtime = ""
		Do
			getmouse (x, y, , buttons)
			if buttons > 0 and x < 275 or buttons > 0 and x > 330 or buttons > 0 and y < 14 or buttons > 0 and y > 43 then exit do
				s = Inkey$
				If s = Chr(255) & "k" Or s = Chr(27) Then End
				If s = Chr(13) Then Exit Do
				if s = chr(8) and len(logtime) > 0 then
					clearbox (278, 17)
					logtime = left(logtime, len(logtime) - 1)
					locate 4, 36: color 0,15: print logtime & "_"
				end if
				If s > "" And Len(logtime) < 5 And s <> Chr(8) Then
					clearbox (278, 17)
					logtime = logtime + s
					locate 4, 36: color 0,15: print logtime & "_"
				End If
				Sleep 20
		Loop
		clearbox (278, 17)
		locate 4, 36: color 0,15: print logtime
		
'Box #3
	elseif x > 403 and x < 538 and y > 14 and y < 43 then
		clearbox (406, 17)
		close #2
		Locate 4, 52: Color 0,15: Print "_"
		device_name = ""
		Do
			getmouse (x, y, , buttons)
			if buttons > 0 and x < 403 or buttons > 0 and x > 538 or buttons > 0 and y < 14 or buttons > 0 and y > 43 then exit do
				s = Inkey$ 
				If s = Chr(255) & "k" Or s = Chr(27) Then End
				If s = Chr(13) Then Exit Do
				if s = chr(8) and len(device_name) > 0 then
					clearbox (406, 17)
					device_name = left(device_name, len(device_name) - 1)
					locate 4, 52: color 0,15: print device_name & "_"
				end if
				If s > "" And Len(device_name) < 15 And s <> Chr(8) Then
					clearbox (406, 17)
					device_name = device_name + s
					locate 4, 52: color 0,15: print device_name & "_"
				End If
				Sleep 20
		Loop
		clearbox (406, 17)
		locate 4, 52: color 0,15: print device_name
		
'Box #4
	elseif x > 11 and x < 786 and y > 54 and y < 371 then
		
	end if

	sleep 10
	getmouse (x, y, , buttons)
	do while x > 1 and buttons > 0: getmouse (x, y, , buttons): sleep 10: loop
	buttonup (556,39,50)
	buttonup (620,39,42)
	buttonup (676,39,50)
	buttonup (740,39,42)
loop

sub bttn(x as integer, y as integer, s as string)
	dim as integer w
	w = len(s) * 8 + 10
	draw string (x + 8, y + 10), s
	draw "C0 BM" & x & "," & y & "D26 R D R D R" & w & "U R U R U26 L U L U L" & w & "D L D L C7 BM" & x + 1 & "," & y + 1 & "BD26 BR BD BR BD R" & w & "U R U R U26 C0"
end sub

sub buttonup(x as integer, y as integer, w as integer)
	draw "C15 BM" & x & "," & y & "U24 R U R U R" & w - 2 & "C7 BM" & x + 2 & "," & y + 4 & "R" & w & "U R U R U26"
end sub

sub buttondown(x as integer, y as integer, w as integer)
	draw "C15 BM" & x & "," & y & " R " & w & " U R U R U26 C7 BM" & x - 2 & "," & y - 4 & " U24 R U R U R" & w - 2
end sub

sub inbox(x as integer, y as integer, w as integer, h as integer)
	draw "C0 BM" & x & "," & y & " D" & h & " R D R D R" & w & " U R U R U" & h & " L U L U L" & w & " D L D L C7 BM" & x + 1 & "," & y + 1 & " D" & h - 2 & " R D R D R" & w - 2 & " U R U R U" & h - 2 & " L U L U L" & w - 2 & "D L D"
end sub

sub clearbox(x as integer, y as integer)
	draw "BM" & x & "," & y & "P15,7 C0"
end sub

sub label(x as integer, y as integer, s as string)
	draw string (x, y), s, 0
end sub
ps. the "Log Time" doesn't work properly yet.
 

GreenLeader

Senior Member
I downloaded FreeBasic, compiled your code and made an executable in a matter of minutes! So now I have my first 5 minutes of Basic experience under my belt:cool: . I have not connected a Picaxe yet, but the .exe works.

As far as I can tell, your basic program sends a command to ask the Picaxe to read an ADC and sends the value back to the Basic program, one ADC at a time.

Another variation (which would suit me nicely, hint, hint :)) is as follows:
The picaxe is continuously streaming readings from several ADC's to the serial port like this:
0,1,33,44,66.5,6,66
3,5,7,8,9,9,112

Now if you could plot those as they come in and also write them to a text file for permanent storage, then my to-do list would be 1 item shorter :D

The data stream would probably be a lot faster than you are getting now and the timing of sampling would be under Picaxe control. Also you only have to worry about communications in one direction then.

With my 40X1 logger running at 8MHz and the hardware serial port set at 115200 baud, I think I am getting something like 10Hz (that is 7 ADC channels at 10Hz each)
 
Last edited:

womai

Senior Member
GreenLeader,

if speed is a concern, maybe you want to post your Picaxe code? I am sure it can be sped up quite a bit. Quite a while ago I made some experiments in that direction (reading the ADC sending the result over the serial port) and achieved well over 1000 readings per second on one ADC channel. So with 7 channels you should be able to get to around 100 readings per second.

One trick is to take advantage of the background hserout. But keep in mind it holds only a single byte, so if you send e.g. two bytes at once, the second byte gets held up while the first one is being sent.

So instead of

Code:
readadc 0, b0
readadc 1, b1
readadc 2, b2
readadc 3, b3
hserout 0, (b0, b1, b2, b3)
it should be quite a bit faster to do

Code:
readadc 0, b0
  hserout 0, (b0)
 readadc 1, b0
  hserout 0, (b0)
 readadc 2, b0
  hserout 0, (b0)
 readadc 3, b0
  hserout 0, (b0)
In the second case the time to send one byte over the serial port is used to read the next value from the ADC, effectively almost doubling the execution speed.

Also, instead of readadc10 and sending 2 bytes, it helps to use readadc and only sending 1 byte (if you can live with the reduced resolution, that is).

Wolfgang
 

picaxester

Senior Member
It does save it to a .txt already. You can even look at previously save files. Just fill in the "File Name" box (btw it adds ".txt" to the file name for you) :)

At this time I'm limited to the 08M and the 14M.
I think it might get a little messy if there's more the 3 lines on the graph.

I'll be working on speed right now though :)
 

picaxester

Senior Member
With help from the FB forum it's now a bit quicker:

Code:
dim as integer x, y, buttons, i, ii, Blue1, Blue2, Red1, Red2, Green1, Green2, graphpos
'dim as double 
dim as string s, text, filename, logtime, temp, device_name, Blues, Reds, Greens

declare Sub checkport(filenumber As Integer, delay As Double)
declare sub bttn(x as integer, y as integer, s as string)
declare sub buttonup(x as integer, y as integer, s as integer)
declare sub buttondown(x as integer, y as integer, s as integer)
declare sub inbox(x as integer, y as integer, w as integer, h as integer)
declare sub clearbox(x as integer, y as integer)
declare sub label(x as integer, y as integer, s as string)

screenres 800,335
color 0, 15
cls

bttn (555, 14, "START")	'Button #1
bttn (619, 14, "STOP")	'Button #2
bttn (675, 14, "CLEAR")	'Button #3
bttn (739, 14, "EXIT")	'Button #4
inbox (51, 14, 163, 26)	'In/Out box #1
inbox (275, 14, 51, 26)	'In/Out box #2
locate 4, 37: color 0,15: print "mins"
inbox (403, 14, 131, 26)	'In/Out box #3
locate 4, 52: color 0,15
#IFDEF __FB_LINUX__
	print "/dev/ttyS0"
	device_name = "/dev/ttyS0"
#ENDIF
#IFDEF __FB_WIN32__
	print "COM1"
	device_name = "COM1"
#ENDIF
#IFDEF __FB_DOS__
	print "COM1"
	device_name = "COM1"
#ENDIF
inbox (11, 54, 771, 269)	'In/Out box #4 314
label (11, 16, "FILE")	'Label #1
label (11, 32, "NAME")	'Label #2
label (235, 16, "LOG")	'Label #3
label (235, 32, "TIME")	'Label #4
label (347, 16, "DEVICE")	'Label #5
label (347, 32, "NAME")	'Label #6

for y = 61 to 330 step 15
	line (14, y)-(783, y), 7
next y

logtime = "1"
graphpos = 14

do
	do while buttons < 1
		getmouse (x, y, , buttons)
		s = inkey$
		if s = chr(255) & "k" or s = chr(27) then end
		sleep 10
	loop

'BUTTONS========================================================
'START
	if x > 555 and x < 609 and y > 14 and y < 44 then
		buttondown (558, 43, 50)
		open filename & ".txt" for binary as #1
		#IFDEF __FB_LINUX__
			shell "stty -F " & device_name & " speed 4800"
			Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
		#ENDIF

		#IFDEF __FB_WIN32__
			'Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
			open com "COM1:4800,n,8,1,cs0,cd0,ds0,rs" as #2
		#ENDIF
		 
		#IFDEF __FB_DOS__
			Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
		#ENDIF
		Blue2 = 316
		Red2 = 316
		Green2 = 316
		for i = graphpos to 784
			Print #2, Chr(66)
			checkport(2,.1)
			Blue1 = Val(Input(Loc(2), 2))
			Print #2, Chr(82)
			checkport(2,.1)
			Red1 = Val(Input(Loc(2), 2))
			Print #2, Chr(71)
			checkport(2,.1)
			Green1 = Val(Input(Loc(2), 2))
			if Blue1 > 255 or Blue1 < 0 then Blue1 = 0
			if Red1 > 255 or Red1 < 0 then Red1 = 0
			if Green1 > 255 or Green1 < 0 then Green1 = 0
			print #1, Blue1 & "	Blue"
			print #1, Red1 & "	Red"
			print #1, Green1 & "	Green"
			Blue1 = 316 - Blue1
			Red1 = 316 - Red1
			Green1 = 316 - Green1
			if Blue2 > 316 or Blue2 < 61 then Blue2 = 316
			if Red2 > 316 or Red2 < 61 then Red2 = 316
			if Green2 > 316 or Green2 < 61 then Green2 = 316
			line (i, Blue1)-(i - 1, Blue2), 1
			line (i, Red1)-(i - 1, Red2), 4
			line (i, Green1)-(i - 1, Green2), 2
			Blue2 = Blue1
			Red2 = Red1
			Green2 = Green1
			for ii = 1 to val(logtime)
				getmouse (x, y, , buttons)
				if x > 619 and x < 665 and y > 14 and y < 44 and buttons = 1 then
					graphpos = i
					exit for, for
				end if
				if x > 675 and x < 729 and y > 14 and y < 44 and buttons = 1 then goto cleargraph
				if x > 739 and x < 785 and y > 14 and y < 44 and buttons = 1 then end
				sleep 20, 1
			next ii
		next i
		close #1
		close #2
		
'STOP
	elseif x > 619 and x < 665 and y > 14 and y < 44 then
		buttondown (622, 43, 42)
		
'CLEAR
	elseif x > 675 and x < 729 and y > 14 and y < 44 then
	cleargraph:
		buttondown (678, 43, 50)
		clearbox (14, 57)
		graphpos = 14
		for y = 61 to 330 step 15
			line (14, y)-(783, y), 7
		next y
		close #1
		close #2
		kill filename & ".txt"
		
'EXIT
	elseif x > 739 and x < 785 and y > 14 and y < 44 then
		buttondown (742, 43, 42)
		close #1
		close #2
		end
		
'IN/OUT BOXES===================================================
'Box #1
	elseif x > 51 and x < 218 and y > 14 and y < 43 then
		clearbox (54, 17)
		clearbox (14, 57)
		graphpos = 14
		for y = 61 to 330 step 15
			line (14, y)-(783, y), 7
		next y
		
		Locate 4, 8: Color 0,15: Print "_"
		filename = ""
		Do
			getmouse (x, y, , buttons)
			if buttons > 0 and x < 51 or buttons > 0 and x > 218 or buttons > 0 and y < 14 or buttons > 0 and y > 43 then exit do
				s = Inkey$
				If s = Chr(255) & "k" Or s = Chr(27) Then End
				If s = Chr(13) Then Exit Do
				if s = chr(8) and len(filename) > 0 then
					clearbox (54, 17)
					filename = left(filename, len(filename) - 1)
					locate 4, 8: color 0,15: print filename & "_"
				end if
				If s > "" And Len(filename) < 19 and s <> chr(8) Then
					clearbox (54, 17)
					filename = filename + s
					locate 4, 8: color 0,15: print filename & "_"
				End If
				Sleep 20
		Loop
		
		clearbox (54, 17)
		locate 4, 8: color 0,15: print filename
		
		close #1
		open filename & ".txt" for binary as #1
		if lof(1) > 0 Then
			
			for i = 14 to 784
			line input #1, Blues
			line input #1, Reds
			line input #1, Greens
			Blue1 = val(Blues)
			Red1 = val(Reds)
			Green1 = val(Greens)
			if Blue1 > 255 or Blue1 < 0 then Blue1 = 0
			if Red1 > 255 or Red1 < 0 then Red1 = 0
			if Green1 > 255 or Green1 < 0 then Green1 = 0
			Blue1 = 316 - Blue1
			Red1 = 316 - Red1
			Green1 = 316 - Green1
			if Blue2 > 316 or Blue2 < 61 then Blue2 = 316
			if Red2 > 316 or Red2 < 61 then Red2 = 316
			if Green2 > 316 or Green2 < 61 then Green2 = 316
			line (i, Blue1)-(i - 1, Blue2), 1
			line (i, Red1)-(i - 1, Red2), 4
			line (i, Green1)-(i - 1, Green2), 2
			Blue2 = Blue1
			Red2 = Red1
			Green2 = Green1
		next i
		else
			clearbox (14, 57)
			for y = 61 to 330 step 15
				line (14, y)-(783, y), 7
			next y
		end if
		
'Box #2
	elseif x > 275 and x < 330 and y > 14 and y < 43 then
		clearbox (278, 17)
		Locate 4, 36: Color 0,15: Print "_"
		logtime = ""
		Do
			getmouse (x, y, , buttons)
			if buttons > 0 and x < 275 or buttons > 0 and x > 330 or buttons > 0 and y < 14 or buttons > 0 and y > 43 then exit do
				s = Inkey$
				If s = Chr(255) & "k" Or s = Chr(27) Then End
				If s = Chr(13) Then Exit Do
				if s = chr(8) and len(logtime) > 0 then
					clearbox (278, 17)
					logtime = left(logtime, len(logtime) - 1)
					locate 4, 36: color 0,15: print logtime & "_"
				end if
				If s > "" And Len(logtime) < 5 and s <> chr(8) Then
					clearbox (278, 17)
					logtime = logtime + s
					locate 4, 36: color 0,15: print logtime & "_"
				End If
				Sleep 20
		Loop
		clearbox (278, 17)
		locate 4, 36: color 0,15: print logtime
		'locate 4, 36: color 0,15: input "", logtime
		'logtime = logtime * 60
		'logtime = int(logtime * .18)
		
'Box #3
	elseif x > 403 and x < 538 and y > 14 and y < 43 then
		clearbox (406, 17)
		close #2
		Locate 4, 52: Color 0,15: Print "_"
		device_name = ""
		Do
			getmouse (x, y, , buttons)
			if buttons > 0 and x < 403 or buttons > 0 and x > 538 or buttons > 0 and y < 14 or buttons > 0 and y > 43 then exit do
				s = Inkey$
				If s = Chr(255) & "k" Or s = Chr(27) Then End
				If s = Chr(13) Then Exit Do
				if s = chr(8) and len(device_name) > 0 then
					clearbox (406, 17)
					device_name = left(device_name, len(device_name) - 1)
					locate 4, 52: color 0,15: print device_name & "_"
				end if
				If s > "" And Len(device_name) < 15 and s <> chr(8) Then
					clearbox (406, 17)
					device_name = device_name + s
					locate 4, 52: color 0,15: print device_name & "_"
				End If
				Sleep 20
		Loop
		clearbox (406, 17)
		locate 4, 52: color 0,15: print device_name
		
'Box #4
	elseif x > 11 and x < 786 and y > 54 and y < 371 then
		'clearbox (14, 57)
		'locate 9, 3: color 0,15: print "???"
		
	end if

	sleep 10
	getmouse (x, y, , buttons)
	do while x > 1 and buttons > 0: getmouse (x, y, , buttons): sleep 10: loop
	buttonup (556,39,50)
	buttonup (620,39,42)
	buttonup (676,39,50)
	buttonup (740,39,42)
loop

Sub checkport(filenumber As Integer, delay As Double)
Dim t As Double
t = Timer
While Loc(filenumber) = 0
    If Timer - t > delay Then
        Exit While
    End If
        Sleep 1
Wend
End Sub

sub bttn(x as integer, y as integer, s as string)
	dim as integer w
	w = len(s) * 8 + 10
	draw string (x + 8, y + 10), s
	draw "C0 BM" & x & "," & y & "D26 R D R D R" & w & "U R U R U26 L U L U L" & w & "D L D L C7 BM" & x + 1 & "," & y + 1 & "BD26 BR BD BR BD R" & w & "U R U R U26 C0"
end sub

sub buttonup(x as integer, y as integer, w as integer)
	draw "C15 BM" & x & "," & y & "U24 R U R U R" & w - 2 & "C7 BM" & x + 2 & "," & y + 4 & "R" & w & "U R U R U26"
end sub

sub buttondown(x as integer, y as integer, w as integer)
	draw "C15 BM" & x & "," & y & " R " & w & " U R U R U26 C7 BM" & x - 2 & "," & y - 4 & " U24 R U R U R" & w - 2
end sub

sub inbox(x as integer, y as integer, w as integer, h as integer)
	draw "C0 BM" & x & "," & y & " D" & h & " R D R D R" & w & " U R U R U" & h & " L U L U L" & w & " D L D L C7 BM" & x + 1 & "," & y + 1 & " D" & h - 2 & " R D R D R" & w - 2 & " U R U R U" & h - 2 & " L U L U L" & w - 2 & "D L D"
end sub

sub clearbox(x as integer, y as integer)
	draw "BM" & x & "," & y & "P15,7 C0"
end sub

sub label(x as integer, y as integer, s as string)
	draw string (x, y), s, 0
end sub
 

GreenLeader

Senior Member
Hi Womai

In my application, speed is not an issue, but here is my code that achieves 10Hz per channel, for an aggregate rate of 70Hz.

I prefer to scale my ADC's into engineering units and transmit them nicely formatted so they can be used directly in Excel, Matlab etc. Time stamp is in the first column, like this:

11.73,51.48,127.86,2.10,407.67,55.36,366.02,148.46,
11.83,51.48,127.86,2.10,409.14,55.36,371.90,187.66,
11.92,51.48,127.86,2.10,418.94,78.39,427.27,512.53,
12.02,51.48,127.86,2.10,407.67,78.39,420.90,199.91,
12.12,51.48,127.86,2.10,409.14,55.36,371.41,157.77,

Doing all the conversions might slow things up a bit I suppose. Am I limited by the baud rate or just the code-processing?

Code:
' Reads 7 ADC channels. Scales into 1/100ths in Engineering Units. 
' Prints to 2 decimal places and streams to serial port
' Capable of about 10Hz per channel
' 
#PICAXE 40X1
setfreq m8

' ADC's
symbol Ch1 = w1        
symbol Ch2 = w2        
symbol Ch3 = w3        
symbol Ch4 = w4        
symbol Ch5 = w5        
symbol Ch6 = w6    
symbol Ch7 = w7    
symbol tmp = w8        ' scratch variable

' configure hardware serial port
' Uses hardware serial port on pins 25=tx and 26=rx on 40X1 to for hserout (higher baud rates available)
' But could use any pins and serout if lower baud rates are adequate
hsersetup B115200_8, %10   

settimer 65222        'set timer to increment every 10ms

Stream:  
   if timer>60000 then 
    timer = 0        'reset timer after 600s (instead of default rolling over at 655.5s) 
   endif    
   
   gosub getADC        'read ADC's
 
   tmp = timer        'timer is in 10ms steps. Printout will be in seconds to 1/100s resolution 
   gosub Bin2Asc100    'print the timer in decimal seconds   
   
   tmp = Ch1*43/10*3    'convert Ch1 to Eng Units
   gosub Bin2Asc100    'print to two decimal places

   tmp = Ch2*13/10*3    'convert Ch2 to Eng Units
   gosub Bin2Asc100    'print to two decimal places
   
   tmp = Ch3*13/10*3    'convert Ch3 to Eng Units
   gosub Bin2Asc100    'print to two decimal places
   
   tmp = Ch4*49-60000    'convert Ch4 to Eng Units
   gosub Bin2Asc100    'print to two decimal places
   
   tmp = Ch5*49-60000    'convert Ch5 to Eng Units
   gosub Bin2Asc100    'print to two decimal places
      
   tmp = Ch6*49-60000    'convert Ch6 to Eng Units
   gosub Bin2Asc100    'print to two decimal places
   
   tmp = Ch7*49-60000    'convert Ch7 to Eng Units
   gosub Bin2Asc100    'print to two decimal places
   
   hserout 0,(CR,LF)    'carriage return, linefeed
   
   goto Stream
   

Bin2Asc100:
  'Writes with 2 decimal place (assumes tmp in 1/100ths of a unit)
  bintoascii tmp, b2,b3,b4,b5,b6
                 'tt, t, h, t, u 
  if tmp >=10000 then hserout 0,(b2) endif    'tens of thousands
  if tmp >=1000  then hserout 0,(b3) endif    'thousands
  if tmp >=100   then hserout 0,(b4) endif    'hundreds
  hserout 0,(".")                        'decimal point 
  if tmp >=10    then 
     hserout 0,(b5)                    'tens
  else  
   hserout 0,("0")            
  endif
  hserout 0,(b6,",")                    'units
  
  return

GetADC:
   'read the 7 ADC's 
   readadc10 0,Ch1
   readadc10 1,Ch2
   readadc10 2,Ch3
   readadc10 3,Ch4
   readadc10 5,Ch5
   readadc10 6,Ch6
   readadc10 7,Ch7
   return
 

picaxester

Senior Member
I got it down to 34 seconds for the length of the graph:

FB code:
Code:
dim as integer x, y, buttons, i, ii, Blue1, Blue2, Red1, Red2, Green1, Green2, graphpos
'dim as double 
dim as string s, text, filename, logtime, temp, device_name, Blues, Reds, Greens

declare Sub checkport(filenumber As Integer, delay As Double)
declare sub bttn(x as integer, y as integer, s as string)
declare sub buttonup(x as integer, y as integer, s as integer)
declare sub buttondown(x as integer, y as integer, s as integer)
declare sub inbox(x as integer, y as integer, w as integer, h as integer)
declare sub clearbox(x as integer, y as integer)
declare sub label(x as integer, y as integer, s as string)

screenres 800,335
color 0, 15
cls

bttn (555, 14, "START")	'Button #1
bttn (619, 14, "STOP")	'Button #2
bttn (675, 14, "CLEAR")	'Button #3
bttn (739, 14, "EXIT")	'Button #4
inbox (51, 14, 163, 26)	'In/Out box #1
inbox (275, 14, 51, 26)	'In/Out box #2
locate 4, 37: color 0,15: print "mins"
inbox (403, 14, 131, 26)	'In/Out box #3
locate 4, 52: color 0,15
#IFDEF __FB_LINUX__
	print "/dev/ttyS0"
	device_name = "/dev/ttyS0"
#ENDIF
#IFDEF __FB_WIN32__
	print "COM1"
	device_name = "COM1"
#ENDIF
#IFDEF __FB_DOS__
	print "COM1"
	device_name = "COM1"
#ENDIF
inbox (11, 54, 771, 269)	'In/Out box #4 314
label (11, 16, "FILE")	'Label #1
label (11, 32, "NAME")	'Label #2
label (235, 16, "LOG")	'Label #3
label (235, 32, "TIME")	'Label #4
label (347, 16, "DEVICE")	'Label #5
label (347, 32, "NAME")	'Label #6

for y = 61 to 330 step 15
	line (14, y)-(783, y), 7
next y

logtime = "1"
graphpos = 14

do
	do while buttons < 1
		getmouse (x, y, , buttons)
		s = inkey$
		if s = chr(255) & "k" or s = chr(27) then end
		sleep 10
	loop

'BUTTONS========================================================
'START
	if x > 555 and x < 609 and y > 14 and y < 44 then
		buttondown (558, 43, 50)
		open filename & ".txt" for binary as #1
		#IFDEF __FB_LINUX__
			shell "stty -F " & device_name & " speed 4800"
			Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
		#ENDIF

		#IFDEF __FB_WIN32__
			'Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
			open com "COM1:4800,n,8,1,cs0,cd0,ds0,rs" as #2
		#ENDIF
		 
		#IFDEF __FB_DOS__
			Open Com device_name & ":4800,n,8,1,CD,CS,DS,RS,BIN" For Binary As #2
		#ENDIF
		Blue2 = 316
		Red2 = 316
		Green2 = 316
		for i = graphpos to 784
			Print #2, Chr(65)
			checkport(2,.1)
			s = Input(Loc(2), 2)
			Blue1 = asc(mid(s, 1, 1))
			Red1 = asc(mid(s, 2, 1))
			Green1 = asc(mid(s, 3, 1))
			if Blue1 > 255 or Blue1 < 0 then Blue1 = 0
			if Red1 > 255 or Red1 < 0 then Red1 = 0
			if Green1 > 255 or Green1 < 0 then Green1 = 0
			print #1, Blue1 & "	Blue"
			print #1, Red1 & "	Red"
			print #1, Green1 & "	Green"
			Blue1 = 316 - Blue1
			Red1 = 316 - Red1
			Green1 = 316 - Green1
			if Blue2 > 316 or Blue2 < 61 then Blue2 = 316
			if Red2 > 316 or Red2 < 61 then Red2 = 316
			if Green2 > 316 or Green2 < 61 then Green2 = 316
			line (i, Blue1)-(i - 1, Blue2), 1
			line (i, Red1)-(i - 1, Red2), 4
			line (i, Green1)-(i - 1, Green2), 2
			Blue2 = Blue1
			Red2 = Red1
			Green2 = Green1
			for ii = 1 to val(logtime)
				getmouse (x, y, , buttons)
				if x > 619 and x < 665 and y > 14 and y < 44 and buttons = 1 then
					graphpos = i
					exit for, for
				end if
				if x > 675 and x < 729 and y > 14 and y < 44 and buttons = 1 then goto cleargraph
				if x > 739 and x < 785 and y > 14 and y < 44 and buttons = 1 then end
				sleep 20, 1
			next ii
		next i
		close #1
		close #2
		
'STOP
	elseif x > 619 and x < 665 and y > 14 and y < 44 then
		buttondown (622, 43, 42)
		
'CLEAR
	elseif x > 675 and x < 729 and y > 14 and y < 44 then
	cleargraph:
		buttondown (678, 43, 50)
		clearbox (14, 57)
		graphpos = 14
		for y = 61 to 330 step 15
			line (14, y)-(783, y), 7
		next y
		close #1
		close #2
		kill filename & ".txt"
		
'EXIT
	elseif x > 739 and x < 785 and y > 14 and y < 44 then
		buttondown (742, 43, 42)
		close #1
		close #2
		end
		
'IN/OUT BOXES===================================================
'Box #1
	elseif x > 51 and x < 218 and y > 14 and y < 43 then
		clearbox (54, 17)
		clearbox (14, 57)
		graphpos = 14
		for y = 61 to 330 step 15
			line (14, y)-(783, y), 7
		next y
		
		Locate 4, 8: Color 0,15: Print "_"
		filename = ""
		Do
			getmouse (x, y, , buttons)
			if buttons > 0 and x < 51 or buttons > 0 and x > 218 or buttons > 0 and y < 14 or buttons > 0 and y > 43 then exit do
				s = Inkey$
				If s = Chr(255) & "k" Or s = Chr(27) Then End
				If s = Chr(13) Then Exit Do
				if s = chr(8) and len(filename) > 0 then
					clearbox (54, 17)
					filename = left(filename, len(filename) - 1)
					locate 4, 8: color 0,15: print filename & "_"
				end if
				If s > "" And Len(filename) < 19 and s <> chr(8) Then
					clearbox (54, 17)
					filename = filename + s
					locate 4, 8: color 0,15: print filename & "_"
				End If
				Sleep 20
		Loop
		
		clearbox (54, 17)
		locate 4, 8: color 0,15: print filename
		
		close #1
		open filename & ".txt" for binary as #1
		if lof(1) > 0 Then
			
			for i = 14 to 784
			line input #1, Blues
			line input #1, Reds
			line input #1, Greens
			Blue1 = val(Blues)
			Red1 = val(Reds)
			Green1 = val(Greens)
			if Blue1 > 255 or Blue1 < 0 then Blue1 = 0
			if Red1 > 255 or Red1 < 0 then Red1 = 0
			if Green1 > 255 or Green1 < 0 then Green1 = 0
			Blue1 = 316 - Blue1
			Red1 = 316 - Red1
			Green1 = 316 - Green1
			if Blue2 > 316 or Blue2 < 61 then Blue2 = 316
			if Red2 > 316 or Red2 < 61 then Red2 = 316
			if Green2 > 316 or Green2 < 61 then Green2 = 316
			line (i, Blue1)-(i - 1, Blue2), 1
			line (i, Red1)-(i - 1, Red2), 4
			line (i, Green1)-(i - 1, Green2), 2
			Blue2 = Blue1
			Red2 = Red1
			Green2 = Green1
		next i
		else
			clearbox (14, 57)
			for y = 61 to 330 step 15
				line (14, y)-(783, y), 7
			next y
		end if
		
'Box #2
	elseif x > 275 and x < 330 and y > 14 and y < 43 then
		clearbox (278, 17)
		Locate 4, 36: Color 0,15: Print "_"
		logtime = ""
		Do
			getmouse (x, y, , buttons)
			if buttons > 0 and x < 275 or buttons > 0 and x > 330 or buttons > 0 and y < 14 or buttons > 0 and y > 43 then exit do
				s = Inkey$
				If s = Chr(255) & "k" Or s = Chr(27) Then End
				If s = Chr(13) Then Exit Do
				if s = chr(8) and len(logtime) > 0 then
					clearbox (278, 17)
					logtime = left(logtime, len(logtime) - 1)
					locate 4, 36: color 0,15: print logtime & "_"
				end if
				If s > "" And Len(logtime) < 5 and s <> chr(8) Then
					clearbox (278, 17)
					logtime = logtime + s
					locate 4, 36: color 0,15: print logtime & "_"
				End If
				Sleep 20
		Loop
		clearbox (278, 17)
		locate 4, 36: color 0,15: print logtime
		'locate 4, 36: color 0,15: input "", logtime
		'logtime = logtime * 60
		'logtime = int(logtime * .18)
		
'Box #3
	elseif x > 403 and x < 538 and y > 14 and y < 43 then
		clearbox (406, 17)
		close #2
		Locate 4, 52: Color 0,15: Print "_"
		device_name = ""
		Do
			getmouse (x, y, , buttons)
			if buttons > 0 and x < 403 or buttons > 0 and x > 538 or buttons > 0 and y < 14 or buttons > 0 and y > 43 then exit do
				s = Inkey$
				If s = Chr(255) & "k" Or s = Chr(27) Then End
				If s = Chr(13) Then Exit Do
				if s = chr(8) and len(device_name) > 0 then
					clearbox (406, 17)
					device_name = left(device_name, len(device_name) - 1)
					locate 4, 52: color 0,15: print device_name & "_"
				end if
				If s > "" And Len(device_name) < 15 and s <> chr(8) Then
					clearbox (406, 17)
					device_name = device_name + s
					locate 4, 52: color 0,15: print device_name & "_"
				End If
				Sleep 20
		Loop
		clearbox (406, 17)
		locate 4, 52: color 0,15: print device_name
		
'Box #4
	elseif x > 11 and x < 786 and y > 54 and y < 371 then
		'clearbox (14, 57)
		'locate 9, 3: color 0,15: print "???"
		
	end if

	sleep 10
	getmouse (x, y, , buttons)
	do while x > 1 and buttons > 0: getmouse (x, y, , buttons): sleep 10: loop
	buttonup (556,39,50)
	buttonup (620,39,42)
	buttonup (676,39,50)
	buttonup (740,39,42)
loop

Sub checkport(filenumber As Integer, delay As Double)
Dim t As Double
t = Timer
While Loc(filenumber) = 0
    If Timer - t > delay Then
        Exit While
    End If
        Sleep 1
Wend
End Sub

sub bttn(x as integer, y as integer, s as string)
	dim as integer w
	w = len(s) * 8 + 10
	draw string (x + 8, y + 10), s
	draw "C0 BM" & x & "," & y & "D26 R D R D R" & w & "U R U R U26 L U L U L" & w & "D L D L C7 BM" & x + 1 & "," & y + 1 & "BD26 BR BD BR BD R" & w & "U R U R U26 C0"
end sub

sub buttonup(x as integer, y as integer, w as integer)
	draw "C15 BM" & x & "," & y & "U24 R U R U R" & w - 2 & "C7 BM" & x + 2 & "," & y + 4 & "R" & w & "U R U R U26"
end sub

sub buttondown(x as integer, y as integer, w as integer)
	draw "C15 BM" & x & "," & y & " R " & w & " U R U R U26 C7 BM" & x - 2 & "," & y - 4 & " U24 R U R U R" & w - 2
end sub

sub inbox(x as integer, y as integer, w as integer, h as integer)
	draw "C0 BM" & x & "," & y & " D" & h & " R D R D R" & w & " U R U R U" & h & " L U L U L" & w & " D L D L C7 BM" & x + 1 & "," & y + 1 & " D" & h - 2 & " R D R D R" & w - 2 & " U R U R U" & h - 2 & " L U L U L" & w - 2 & "D L D"
end sub

sub clearbox(x as integer, y as integer)
	draw "BM" & x & "," & y & "P15,7 C0"
end sub

sub label(x as integer, y as integer, s as string)
	draw string (x, y), s, 0
end sub
PICAXE code:
Code:
setfreq m8

do

	serin 3, N2400, ("A"),b0

		readadc 1, b1

		readadc 2, b2

		readadc 4, b3

		serout 0, N2400, (b1, b2, b3)

loop
 

picaxester

Senior Member
It now uses .csv files :)

And shows the values and time stamp when logging and when you open a saved file.
To view values from a saved file just make a file name by clicking in the "FILE NAME" box and type in a name like "test", .csv will be added by the program.
Then click "START. It will start logging. Let it go or hit stop (it will be save either way).
Now click in the "FILE NAME" box and type in your file name ("test" or whatever) hit enter or click away from the box.
To look at values at a specific point in the graph just click in the graph.

 

Attachments

GreenLeader

Senior Member
Link to video did not work for me...
The format of your .csv text file is much better now, though normally the time stamp would be the first column.. (I noticed it does not like to be maximised..)
Nice little application.

I know it may not be on your agenda, but if you did a version to collect and graph data streaming from the picaxe serial port (instead of asking the picaxe to send data), I'd have a go at expanding it to plot more channels. My Basic skills are non-existant, so the comms port stuff is probably beyond me for now. But plotting I could probably have a go at.

I'd make a few dial-type gauges which would suit my particular application, a bit like a dashboard, for some of the signals. Or put each one on its own axis....
 

picaxester

Senior Member
The link works for me. Sorry.

Heres a small test program to get you started :)

FB code:
Code:
'declare the variables
dim as integer Blue, Red, Green
dim as string s

'open the serial port
open com "COM1:4800,n,8,1,cs0,cd0,ds0,rs" as #2

do while inkey$ = ""					'loop until there's a key press
	Print #2, Chr(65)					'send an "A" to the PICAXE
	sleep 40							'stop for a bit
	s = Input(Loc(2), 2)				'recieve the data into variable "s"
	Blue = asc(mid(s, 1, 1))			'put the first bite into variable "Blue"
	Red = asc(mid(s, 2, 1))				'put the second bite into variable "Red"
	Green = asc(mid(s, 3, 1))			'put the last bite into variable "Green"
	cls									'clear the screen
	print using "###	Blue"; Blue		'print the data to the screen
	print using "###	Red"; Red
	print using "###	Green"; Green
	print time
loop

close #2								'close the serial port
PICAXE code:
Code:
setfreq m8

do

	serin 3, N2400, ("A"),b0

		readadc 1, b1

		readadc 2, b2

		readadc 4, b3

		serout 0, N2400, (b1, b2, b3)

loop
Note: there should be a 100K resistor pulling lag 2 low.
 
Last edited:

GreenLeader

Senior Member
...
Heres a small test program to get you started :)
Thank you for that - I've made a bit of progress in reading and plotting using FreeBasic. It was easier than I thought, thanks to your code - I'd have never even tried it if I hadn't been inspired by your code..

Originally I had my picaxe logger streaming continuously to the serial port.
I found that either I read the port too quickly and only got partial lines of data (too hard to glue back together).

Or if I read it more slowly, the port buffer eventually ran out -I could stretch it to 10 or 15 minutes, but thats not good enough.

So, although I really did not want to, I reverted to a scheme similar to yours - I now tell the Picaxe to send a line only when I'm ready in FreeBasic. It works quite well and I still get a reasonable rate - about 8-9Hz on each of 7 channels....

So now I'm trying to reverse-engineer all the other FB tricks you've got in there to draw & redraw buttons and make a nice looking GUI etc :)

Great fun :)
 

picaxester

Senior Member
Heres a gui code maker that I made.

It first asks for a name for your program/form.
Then you pick the color by clicking on the "foreground", "background" and "shadow" and then click on the color that you want. Or just click "done" and it will use the default colors.
Then you pick the size of form you want.
Using the number keys 0 - 7 you add objects.
0) = undo.
1) = buttons.
2) = in/output boxes.
3) = progress bars.
4) = radio buttons.
5) = check boxes.
6) = labels.
7) = sliders.

All values are in characters.
eg. a progress bar with the number 20 will be 20 characters long.

On Esc or the clicking the X is will build and output the .bas file to be compiled and added to.

It's not at all done but it does what want for now.
 
Last edited:

picaxester

Senior Member
I'd make a few dial-type gauges which would suit my particular application, a bit like a dashboard, for some of the signals. Or put each one on its own axis....
Like this? :)



Code:
dim as double i

declare sub inbox(x as integer, y as integer, w as integer, h as integer)
declare sub clearbox(x as integer, y as integer)

screenres 270,260
color 0, 15
cls

circle (137, 130), 90, 0,,,,f
circle (137, 130), 85, 41,,,,f
circle (137, 130), 3, 0,,,,f
inbox (115, 158, 43, 26)
clearbox (118, 161)

for i = 3.14 to .001 step -.002
	circle (137, 130), 70, 0, -i, -i
	circle (137, 130), 3, 0,,,,f
	locate 22, 16: color 0,15: print using "#.###"; i
	sleep 1
	circle (137, 130), 70, 41, -i, -i
next i

circle (137, 130), 70, 0, -i, -i

sleep

sub inbox(x as integer, y as integer, w as integer, h as integer)
	draw "C0 BM" & x & "," & y & " D" & h & " R D R D R" & w & " U R U R U" & h & " L U L U L" & w & " D L D L C7 BM" & x + 1 & "," & y + 1 & " D" & h - 2 & " R D R D R" & w - 2 & " U R U R U" & h - 2 & " L U L U L" & w - 2 & "D L D"
end sub

sub clearbox(x as integer, y as integer)
	draw "BM" & x & "," & y & "P15,7 C0"
end sub
 

picaxester

Senior Member
Heres a bit better of one:



It uses values 0 to 255.
For fun it takes and uses the mouse x axis to moves the needle :)

Code:
dim as double i, a, b
dim as integer x, y, buttons

declare sub inbox(x as integer, y as integer, w as integer, h as integer)
declare sub clearbox(x as integer, y as integer)

screenres 270,260
color 0, 15
cls

'draw a big black circle(filled)
circle (137, 130), 90, 0,,,,f
'draw a smaller red circle(filled)
circle (137, 130), 85, 41,,,,f

'draw the little lines
for i = 0 to 255 step 15
	'convert 0 to 255 into radiants
	a = .001 + 3.14 * (i / 255)
	'draw line from the center outword
	circle (137, 130), 80, 0, -a, -a
next i

'draw a big filled red circle overtop of most of the little lines
circle (137, 130), 72, 41,,,,f
'draw the center dot
circle (137, 130), 3, 0,,,,f

'make the text box
inbox (115, 158, 43, 26)
'clear the text box
clearbox (118, 161)

do while inkey$ = ""
	getmouse (x, y, , buttons)
	x = 255 - x
	if x > 255 then x = 255
	if x < 1 then x = 0
	'convert 0 to 255 into radiants
	a = .001 + 3.14 * (x / 255)
	'draw the needle
	circle (137, 130), 70, 0, -a, -a
	'draw the center dot
	circle (137, 130), 3, 0,,,,f
	'put the value into the text box
	locate 22, 17: color 0,15: print using "###"; 255 - x
	sleep 10
	'erace the needle
	circle (137, 130), 70, 41, -a, -a
loop

sub inbox(x as integer, y as integer, w as integer, h as integer)
	draw "C0 BM" & x & "," & y & " D" & h & " R D R D R" & w & " U R U R U" & h & " L U L U L" & w & " D L D L C7 BM" & x + 1 & "," & y + 1 & " D" & h - 2 & " R D R D R" & w - 2 & " U R U R U" & h - 2 & " L U L U L" & w - 2 & "D L D"
end sub

sub clearbox(x as integer, y as integer)
	draw "BM" & x & "," & y & "P15,7 C0"
end sub
 
Top