Linear scale on 20 x 4 oled

bfgstew

Senior Member
At last some success, I have it working now, coding is a bit of a mess as I had to do a set up with the code for the bargraph on a timed loop, let that go to the keypad routine to enter the triggerthreashold value, then go to a 'copy' of the first bargraph routine so it will run the bargraph so you can see what's happening, sounds confusing but it works!

Code:
#slot2
#freqm4
;#PICAXE 28X2 photogate
 
symbol SOUND_ON = C.0
symbol CAMERA = C.1
symbol FLASH = C.2
symbol SOUND_IN = A.3
symbol LIGHT_ON = C.4
symbol PG_ON = C.5
symbol TIMER_LED = C.6
symbol READY_LED = C.7
symbol LIGHT_IN = A.0
symbol PG_IN = A.1
symbol SOLENOID = A.2
symbol COL2 = pinB.0
symbol ROW1 = B.1
symbol COL1 = pinB.2
symbol ROW4 = B.3
symbol COL3 = pinB.4 
symbol ROW3 = B.5
symbol ROW2 = B.6
symbol DROPS_LED = B.7

symbol key_pos = b0        ; VARIABLES CONFIGURATION
symbol key_value = b1
symbol readvar = b4
symbol length = b5
symbol loopcounter = b6
symbol charactercounter = b7
symbol readvar1 = b8
symbol triggerthreashold = b10
symbol lcdsize = 20
setfreq em16


INIT:

let b10 = 0
let b11 = 0
let b0 = 0
let b1 = 0
let b4 = 0
let b5 = 0
let b6 = 0
let b7 = 0
		serout C.3,N2400_16,(254,1)
	pause 30
	serout C.3,N2400_16,(254,128,"TURN POT TO DESIRED")
	serout C.3,N2400_16,(254,192,"POSITION, THEN ENTER")
	serout C.3,N2400_16,(254,148,"THE 'COPY' VALUE INTO")
	serout C.3,N2400_16,(254,212,"THREASHOLD THEN HIT #")
	pause 3000
	serout C.3,N2400_16,(254,1)
	pause 30
	serout C.3,N2400_16,(254,128,"THE LOWER THE NUMBER")
	serout C.3,N2400_16,(254,192,"THE HIGHER THE SENS")
	serout C.3,N2400_16,(254,148,"THE HIGHER THE NUMBER")
	serout C.3,N2400_16,(254,212,"THE LOWER THE SENS")
	do
	gosub GKP
	loop until key_value = 12

	
SETTING:
		serout C.3,N2400_16,(254,1)
	pause 50
	high C.5
	pause 50
	serout C.3, N2400_16, (254, 64, 32, 32, 32, 32, 32, 32, 32, 32) ; BLANK CHARACTER (0)	 
	serout C.3, N2400_16, (254, 72, 48, 48, 48, 48, 48, 48, 48, 48) ; 1st segment = (1)
	serout C.3, N2400_16, (254, 80, 56, 56, 56, 56, 56, 56, 56, 56) ; 1st and 2nd segment = (2)
	serout C.3, N2400_16, (254, 88, 60, 60, 60, 60, 60, 60, 60, 60) ; 1st, 2nd and 3rd segment = (3)
	serout C.3, N2400_16, (254, 96, 62, 62, 62, 62, 62, 62, 62, 62) ; 1st, 2nd, 3rd and 4th segment = (4)
	serout C.3, N2400_16, (254, 104, 63, 63, 63, 63, 63, 63, 63, 63); whole block = (5)
	do
		readadc A.1,readvar
	
		length = lcdsize * 6 - 1 * readvar / 255 + 1
		readvar1 = readvar * 75 / 100
			serout C.3,N2400_16,(254,128,"    SENSITIVITY")
		serout C.3,N2400_16,(254,192,#triggerthreashold," "," THREASHOLD",254,148,#readvar1," ","COPY",254,158,#readvar," ","ACTUAL",254,212)
		charactercounter = lcdsize
		do
			if length => 6 then
				serout C.3,N2400_16,(13)
				length = length - 6
			elseif length = 5 then
				serout C.3,N2400_16,(13)
				length = length - 5
			else
				exit
			end if
			dec charactercounter 
		loop 
		readvar = length + 8
		serout C.3,N2400_16,(readvar)
		for loopcounter = 0 to charactercounter
			serout C.3,N2400_16,(32)
		
		next
	inc b11
	loop until b11 = 50 gosub TT7 
		

main6:
			do 
			readadc A.1,readvar
				length = lcdsize * 6 - 1 * readvar / 255 + 1
		readvar1 = readvar * 75 / 100
			serout C.3,N2400_16,(254,128,"    SENSITIVITY")
		serout C.3,N2400_16,(254,192,#triggerthreashold," "," THREASHOLD",254,148,#readvar1," ","COPY",254,158,#readvar," ","ACTUAL",254,212)
		charactercounter = lcdsize
		do
			if length => 6 then
				serout C.3,N2400_16,(13)
				length = length - 6
			elseif length = 5 then
				serout C.3,N2400_16,(13)
				length = length - 5
			else
				exit
			end if
			dec charactercounter 
		loop 
		if readvar =< triggerthreashold then goto main66
		readvar = length + 8
		serout C.3,N2400_16,(readvar)
		for loopcounter = 0 to charactercounter
			serout C.3,N2400_16,(32)
		
		next
	loop
main66:
				 
	                   high camera : high flash
	                    pause 100
	                    

	                     low camera : low flash
	                     low READY_LED
	                     low PG_ON
	    	        goto Init
	    	        
GKP:
 	 Do
    		Gosub GTKP			' Wait until no key pressed
  			Loop Until key_value = 0
 	 Do
   		 Gosub GTKP			' Wait until key pressed
  			Loop Until key_value <> 0
  			If key_value = 11 Then
    			key_value = 0
  		End If
  	Return		
GTKP:	;Keypad scan coding
		key_pos = 0
		key_value = 0
	 	High ROW1 : gosub ScanCol : low ROW1
	 	High ROW2 : gosub ScanCol : low ROW2
	 	High ROW3 : gosub ScanCol : low ROW3 
	 	High ROW4 : gosub ScanCol : low ROW4
	 	Return	 	
	 	ScanCol:
	 		if COL1 = 1 then : key_value = key_pos + 1 : endif
	 		if COL2 = 1 then : key_value = key_pos + 2 : endif
	 		if COL3 = 1 then : key_value = key_pos + 3 : endif
	 		key_pos = key_pos + 3   
   		return	
   		

			
TT7: 
		high READY_LED
		let b11 = 0
		For b33 = 1 to 4				
  			Gosub GKP
  			if key_value = 12 then goto main6
  			pause 30	
  			b10 = b10 * 10 + b1
  			serout C.3,N2400_16,(254,192,#b10)
  			pause 30
  			next b33
 		goto main6
Now I have this working, I can have a go at trying the modulated version and the laser version, should be fun............................:p
 

bfgstew

Senior Member
Tidied up the code and added a routine for selecting sensitivity, all working fine now, thanks for all your help guys, appreciated.

Stewart

Code:
#slot2
#freqm4
;#PICAXE 28X2 photogate
 
symbol SOUND_ON = C.0
symbol CAMERA = C.1
symbol FLASH = C.2
symbol SOUND_IN = A.3
symbol LIGHT_ON = C.4
symbol PG_ON = C.5
symbol TIMER_LED = C.6
symbol READY_LED = C.7
symbol LIGHT_IN = A.0
symbol PG_IN = A.1
symbol SOLENOID = A.2
symbol COL2 = pinB.0
symbol ROW1 = B.1
symbol COL1 = pinB.2
symbol ROW4 = B.3
symbol COL3 = pinB.4 
symbol ROW3 = B.5
symbol ROW2 = B.6
symbol DROPS_LED = B.7

symbol key_pos = b0        ; VARIABLES CONFIGURATION
symbol key_value = b1
symbol readvar = b4
symbol length = b5
symbol loopcounter = b6
symbol charactercounter = b7
symbol readvar1 = b8
symbol sensitivity = b9
symbol trigger = b10
symbol flashdelay = w8
symbol lcdsize = 20
setfreq em16


INIT:

let b10 = 0
let b11 = 0
let b12 = 60
let b0 = 0
let b1 = 0
let b4 = 0
let b5 = 0
let b6 = 0
let b7 = 0
let w8 = 0

	serout C.3,N2400_16,(254,1)
	pause 30
	serout C.3,N2400_16,(254,192,"PRESS # TO START")
	pause 30
	do
	gosub GKP
	loop until key_value = 12
	
SETMODE:
		serout C.3,N2400_16,(254,1)
	pause 30
		serout C.3,N2400_16,(254,128,"FOR BULB MODE DON'T")
		serout C.3,N2400_16,(254,192,"CONNECT CAMERA AND ")
		serout C.3,N2400_16,(254,148,"PRESS * TO SET FLASH")
		serout C.3,N2400_16,(254,212,"DELAY. ELSE PRESS #")
		pause 30
		do
		gosub GKP
			 if key_value = 12 then let w8 = 0 endif	
			 if key_value = 10 then goto FD
		     	 loop until key_value > 0
			 
SETSENS:
	serout C.3,N2400_16,(254,1)
	pause 30
		serout C.3,N2400_16,(254,128,"  SET SENSITIVITY")
		serout C.3,N2400_16,(254,192," RANGE BY SELECTING")
		serout C.3,N2400_16,(254,148,"  A VALUE BETWEEN")
		serout C.3,N2400_16,(254,212,"      1 & 10")
		pause 30
		do
		gosub GKP
			 if key_value = 1 then let b9 = 10 endif	
			 if key_value = 2 then let b9 = 20 endif
			 if key_value = 3 then let b9 = 30 endif
			 if key_value = 4 then let b9 = 40 endif
			 if key_value = 5 then let b9 = 50 endif
			 if key_value = 6 then let b9 = 60 endif
			 if key_value = 7 then let b9 = 70 endif
			 if key_value = 8 then let b9 = 80 endif
			 if key_value = 9 then let b9 = 90 endif
		     	 loop until key_value > 0

	
SETTING:

	serout C.3,N2400_16,(254,1)
	pause 50
	high C.5
	pause 50
	serout C.3, N2400_16, (254, 64, 32, 32, 32, 32, 32, 32, 32, 32) ; BLANK CHARACTER (0)	 
	serout C.3, N2400_16, (254, 72, 48, 48, 48, 48, 48, 48, 48, 48) ; 1st segment = (1)
	serout C.3, N2400_16, (254, 80, 56, 56, 56, 56, 56, 56, 56, 56) ; 1st and 2nd segment = (2)
	serout C.3, N2400_16, (254, 88, 60, 60, 60, 60, 60, 60, 60, 60) ; 1st, 2nd and 3rd segment = (3)
	serout C.3, N2400_16, (254, 96, 62, 62, 62, 62, 62, 62, 62, 62) ; 1st, 2nd, 3rd and 4th segment = (4)
	serout C.3, N2400_16, (254, 104, 63, 63, 63, 63, 63, 63, 63, 63); whole block = (5)
	do
		readadc A.1,readvar
		length = lcdsize * 6 - 1 * readvar / 255 + 1
		readvar1 = readvar * sensitivity / 100
		serout C.3,N2400_16,(254,128,"SENSITIVITY = %",254,143,#sensitivity)
			serout C.3,N2400_16,(254,192,"CALIBRATING - WAIT!")
			serout C.3,N2400_16,(254,148,"COUNTDOWN -",254,160," ",#b12," ",254,212)
		charactercounter = lcdsize
		do
			if length => 6 then
				serout C.3,N2400_16,(13)
				length = length - 6
			elseif length = 5 then
				serout C.3,N2400_16,(13)
				length = length - 5
			else
				exit
			end if
			dec charactercounter 
		loop 
		readvar = length + 8
		serout C.3,N2400_16,(readvar)
		for loopcounter = 0 to charactercounter
			serout C.3,N2400_16,(32)
		next
		
	inc b11 dec b12
	loop until b11 = 60 gosub TT7 
		

SETTING2:
	high READY_LED
	serout C.3,N2400_16,(254,1)
	pause 50
		do 
		readadc A.1,readvar
				length = lcdsize * 6 - 1 * readvar / 255 + 1
		readvar1 = readvar * sensitivity / 100
			serout C.3,N2400_16,(254,128,"  ARMED AND READY")
		serout C.3,N2400_16,(254,192,#trigger," ","= TRIGGER POINT",254,148,#readvar," ","= ACTUAL READING",254,212)
		charactercounter = lcdsize
		do
			if length => 6 then
				serout C.3,N2400_16,(13)
				length = length - 6
			elseif length = 5 then
				serout C.3,N2400_16,(13)
				length = length - 5
			else
				exit
			end if
			dec charactercounter 
		loop 
		if readvar =< trigger then goto TRIGGER1
		readvar = length + 8
		serout C.3,N2400_16,(readvar)
		for loopcounter = 0 to charactercounter
			serout C.3,N2400_16,(32)
		next
	loop
	
TRIGGER1:
	pause flashdelay
	high camera : high flash
	     pause 100
	     low camera : low flash
	      low READY_LED
	       low PG_ON
	     	serout C.3,N2400_16,(254,1)
	      pause 30
       	serout C.3,N2400_16,(254,192,"      GOTCHA!")
       	pause 4000
       	do
	      gosub GKP
	      loop until key_value = 12
	    	        goto Init
	    	        
GKP:
 	 Do
    		Gosub GTKP			' Wait until no key pressed
  			Loop Until key_value = 0
 	 Do
   		 Gosub GTKP			' Wait until key pressed
  			Loop Until key_value <> 0
  			If key_value = 11 Then
    			key_value = 0
  		End If
  	Return		
GTKP:	;Keypad scan coding
		key_pos = 0
		key_value = 0
	 	High ROW1 : gosub ScanCol : low ROW1
	 	High ROW2 : gosub ScanCol : low ROW2
	 	High ROW3 : gosub ScanCol : low ROW3 
	 	High ROW4 : gosub ScanCol : low ROW4
	 	Return	 	
	 	ScanCol:
	 		if COL1 = 1 then : key_value = key_pos + 1 : endif
	 		if COL2 = 1 then : key_value = key_pos + 2 : endif
	 		if COL3 = 1 then : key_value = key_pos + 3 : endif
	 		key_pos = key_pos + 3   
   		return
   		
FD:
	serout C.3,N2400_16,(254,1)
	pause 500
	serout C.3,N2400_16,(254,128," NOW SET THE FLASH  ")
	serout C.3,N2400_16,(254,192,"       DELAY        ")
	serout C.3,N2400_16,(254,154,"FD -",254,159,#w8,"ms")
 	pause 100
	    	gosub FD6
	    return	
	    
FD6: ;Coding to change the pause time via keypad

		For b33 = 1 to 4				
  			Gosub GKP
  				if key_value = 12 then goto SETSENS
  			w8 = w8 * 10 + b1
  			serout C.3,N2400_16,(254,128," NOW SET THE FLASH  ")
			serout C.3,N2400_16,(254,192,"       DELAY        ") 
  			serout C.3,N2400_16,(254,154,"FD -",254,159,#w8,"ms")
  			pause 30
  			next b33
 		Return		
   		

			
TT7: 
			serout C.3,N2400_16,(254,1)
	pause 50
		serout C.3,N2400_16,(254,128,"TRIGGER POINT VALUE")
		serout C.3,N2400_16,(254,199,"=",254,201,#readvar1)
		serout C.3,N2400_16,(254,148," KEY THAT VALUE IN ")
		serout C.3,N2400_16,(254,216,"HERE -",254,223,#trigger)
		For b33 = 1 to 4				
  			Gosub GKP
  			if key_value = 12 then goto SETTING2
  			pause 30	
  			b10 = b10 * 10 + b1
  			serout C.3,N2400_16,(254,223,#trigger)
  			pause 30
  			next b33
 		goto SETTING2
 

bfgstew

Senior Member
Code:
SETTING:

	serout C.3,N2400_16,(254,1)
	pause 50
	high C.5
	pause 50
	serout C.3, N2400_16, (254, 64, 32, 32, 32, 32, 32, 32, 32, 32) ; BLANK CHARACTER (0)	 
	serout C.3, N2400_16, (254, 72, 48, 48, 48, 48, 48, 48, 48, 48) ; 1st segment = (1)
	serout C.3, N2400_16, (254, 80, 56, 56, 56, 56, 56, 56, 56, 56) ; 1st and 2nd segment = (2)
	serout C.3, N2400_16, (254, 88, 60, 60, 60, 60, 60, 60, 60, 60) ; 1st, 2nd and 3rd segment = (3)
	serout C.3, N2400_16, (254, 96, 62, 62, 62, 62, 62, 62, 62, 62) ; 1st, 2nd, 3rd and 4th segment = (4)
	serout C.3, N2400_16, (254, 104, 63, 63, 63, 63, 63, 63, 63, 63); whole block = (5)
	do
		readadc A.1,readvar
		length = lcdsize * 6 - 1 * readvar / 255 + 1
		readvar1 = readvar * sensitivity / 100
		serout C.3,N2400_16,(254,128,"SENSITIVITY = %",254,143,#sensitivity)
			serout C.3,N2400_16,(254,192,"CALIBRATING - WAIT!")
			serout C.3,N2400_16,(254,148,"COUNTDOWN -",254,160," ",#b12," ",254,212)
		charactercounter = lcdsize
		do
			if length => 6 then
				serout C.3,N2400_16,(13)
				length = length - 6
			elseif length = 5 then
				serout C.3,N2400_16,(13)
				length = length - 5
			else
				exit
			end if
			dec charactercounter 
		loop 
		readvar = length + 8
		serout C.3,N2400_16,(readvar)
		for loopcounter = 0 to charactercounter
			serout C.3,N2400_16,(32)
		next
		
	inc b11 dec b12
	loop until b11 = 30 gosub TT7
A bit more help needed please.

What I am after is adding a loop into the above code snippet (Thanks Nick) to read the readadc reading (readvar) 30 times using b11 as the count, so I can have a result at the end that can be averaged out and shown via serout. I have tried using the read command and just end up with 0. I have read the read command but cannot seem to impement it into this code.
Any ideas?
 

nick12ab

Senior Member
A bit more help needed please.

What I am after is adding a loop into the above code snippet (Thanks Nick) to read the readadc reading (readvar) 30 times using b11 as the count, so I can have a result at the end that can be averaged out and shown via serout. I have tried using the read command and just end up with 0. I have read the read command but cannot seem to impement it into this code.
Any ideas?
Use some word variable variable and each time the ADC value is read add it to this word variable. Once you have 30 readings, divide the word variable by 30 to get the average.

The word variable must be set to 0 before starting the loop again.
 

westaust55

Moderator
By way of an example almost there:
Code:
Symbol readaverage = w13 ; you must and unused word variable that does not conflict with your other byte and word variables
symbol pointer = b11

Readaverage = 0
For pointer = 1 to 30
READADC A.1,  readvar
Readaverage = readaverage + readvar
Next pointer
Readaverage - readaverage / 30
 

bfgstew

Senior Member
Managed to get it sorted all by myself...................blimey, better have a lie down.....................:p

Added w21 = w21 + b5 in the correct place and now working as I want it.

Many thanks chaps.

Stewart
 
Top