automated cat feeder

Ocean

Member
hi everyone, im building an automated cat feeder for one of my classes i was wondering if anyone could help me out setting the alarm feature and menu navigation. im using an oled display and 3 push buttons for the user interface. what im trying to so is push one button to enter auto serving mode, when in auto serving mode push one button to increase the hours tenths place, push the second button to increase the hours ones place, the third button when pressed will jump to another section of code identical to the last lines except it will increment the mins tens and ones place instead of the hrs. pushing the third button again will set the alarm and jump back to the start. my issue is that i cant get the numbers on the screen to increase with a button press and if i do get it to increase, it dosent follow the 12hr format. any ideas on what i can change or do? im not the most experienced programmer so anything helps. sorry if some stuff doesnt make sense

code:

'variables for RTC
serout c.7, n2400, (254,1)
pause 30

symbol secs = b0 'stores secs on rtc
symbol mins = b1 'stores mins on rtc
symbol hours = b2 'stores hrs on rtc
symbol secs_tens = b3 'for displaying sec on OLED
symbol secs_ones = b4
symbol mins_tens = b5 'for displaying mins on OLED
symbol mins_ones = b6
symbol hrs_tens = b7 'for displaying hrs on OLED
symbol hrs_ones = b8
symbol AM_PM = b9

symbol secs2 = b10 '
symbol mins2 = b11 '
symbol hours2 = b12 '
symbol secs_tens2 = b13 '
symbol secs_ones2 = b14
symbol mins_tens2 = b15 '
symbol mins_ones2 = b16
symbol hrs_tens2 = b17 '
symbol hrs_ones2 = b18
symbol AM_PM2 = b19

'RTC section and menu selection
readrtc:
i2cslave %11010000, i2cslow, i2cbyte 'reads time from in 24 hr from rtc

hi2cin 0,(SECS, MINS, HOURS)

debug
'converts time to 12hr
let AM_PM = "A"
if hours > $11 then : let AM_PM = "P" : endif
if hours = $20 or hours = $21 then : let hours = hours - $6 : endif
if hours > $12 then : let hours = hours - $12 : endif
if hours = $0 then : let hours = $12 : endif

'converts time from bcd to ASCII
bcdtoascii hours, hrs_tens, hrs_ones
if hrs_tens = "0" then : let hrs_tens = " " : endif
bcdtoascii mins, mins_tens, mins_ones
bcdtoascii secs, secs_tens, secs_ones


'display time on OLED
serout c.7, n2400, (254,128)
serout c.7, n2400, (hrs_tens, hrs_ones, ":", mins_tens, mins_ones, ":", secs_tens, secs_ones," ", AM_PM, "m") 'Displays Current Time

'mode selection menu

if pinc.6 = 1 then : goto manualserving :endif 'press button 1 to go to manual
if pinc.5 = 1 then : goto autoserving : endif 'press button 2 to go to auto

goto readrtc




manualserving:
serout c.7, n2400, (254,128)
serout c.7, n2400, ("Manual Serving ")
wait 1
gosub servo_on



servo_on:
serout c.7, n2400, (254,128)
do loop until pinC.6 = 1 'loops until button is pressed
serout c.7, n2400, ("Dispensing... ")
servo b.2, 255 'Starts Servo


do loop until pinC.6 = 0 'loops until button is released
servopos b.2, off 'Shuts off Servo
serout c.7, n2400, (254,128)
serout c.7, n2400, ("Done! ")

pause 500

serout c.7, n2400, (254, 1)
pause 30

goto readrtc


autoserving:
serout c.7, n2400, (254,128)
serout c.7, n2400, ("Auto Serving ")
wait 1
serout c.7, n2400, (254,1)
pause 30

'converts to 12hr
let AM_PM2 = "A"
if hours2 > $11 then : let AM_PM2 = "P" : endif
if hours2 = $20 or hours2 = $21 then : let hours2 = hours2 - $6 : endif
if hours2 > $12 then : let hours2 = hours2 - $12 : endif
if hours2 = $0 then : let hours2 = $12 : endif

'converts to ASCII
bcdtoascii hours2, hrs_tens2, hrs_ones2
if hrs_tens2 = "0" then : let hrs_tens2 = " " : endif
bcdtoascii mins2, mins_tens2, mins_ones2
bcdtoascii secs2, secs_tens2, secs_ones2
serout c.7 ,n2400, (254,128)
serout c.7 ,n2400, (hrs_tens2, hrs_ones2, ":", mins_tens2, mins_ones2, AM_PM2, "M")

goto time_selcthrs

time_selcthrs:
'converts to 12hr
let AM_PM2 = "A"
if hours2 > $11 then : let AM_PM2 = "P" : endif
if hours2 = $20 or hours2 = $21 then : let hours2 = hours2 - $6 : endif
if hours2 > $12 then : let hours2 = hours2 - $12 : endif
if hours2 = $0 then : let hours2 = $12 : endif

'converts to ASCII
bcdtoascii hours2,hrs_tens2, hrs_ones2
if hrs_tens2 = "0" then : let hrs_tens2 = " " : endif
bcdtoascii mins2, mins_tens2, mins_ones2
serout c.7 ,n2400, (254,128)
serout c.7 ,n2400, (hrs_tens2, hrs_ones2, ":", mins_tens2, mins_ones2, AM_PM2, "M")


button C.6, 1,255,0,b27,0,time_selcthrs
pause 50
inc hrs_tens2

button C.5, 1,255,0,b27,0,time_selcthrs
pause 50
inc hrs_ones2

if pinc.4 = 1 then goto time_selctmins
pause 50

time_selctmins:
'converts to 12hr
let AM_PM2 = "A"
if hours2 > $11 then : let AM_PM2 = "P" : endif
if hours2 = $20 or hours2 = $21 then : let hours2 = hours2 - $6 : endif
if hours2 > $12 then : let hours2 = hours2 - $12 : endif
if hours2 = $0 then : let hours2 = $12 : endif

'converts to ASCII
bcdtoascii hours2, hrs_tens2, hrs_ones2
if hrs_tens2 = "0" then : let hrs_tens2 = " " : endif
bcdtoascii mins2, mins_tens2, mins_ones2
bcdtoascii secs2, secs_tens2, secs_ones2
serout c.7 ,n2400, (254,128)
serout c.7 ,n2400, (hrs_tens2, hrs_ones2, ":", mins_tens2, mins_ones2, AM_PM2, "M")

button C.6, 1,255,0,b27,0,time_selctmins
pause 50
inc mins_tens2

button C.5, 1,255,0,b27,0,time_selctmins
pause 50
inc mins_ones2

if pinc.4 = 1 then goto timeset
pause 50

timeset:
serout c.7, n2400, (254,1)
pause 30
serout c.7, n2400, (254,128)
serout c.7, n2400, ("Feeder Set! ")
goto readrtc


alarm:
if hrs_tens2 = hrs_tens and hrs_ones2 = hrs_ones and mins_tens = mins_tens2 and mins_ones = mins_ones2 then
tune B.6, 6,%00000001,($65,$65,$65,$EA,$C5,$43,$42,$40,$CA,$05,$43,$42,$40,$CA,$05,$43,$42,$43,$C0,$2C,$65,$65,$65,$EA,$C5,$43,$42,$40,$CA,$05,$43,$42,$40,$CA,$05,$43,$42,$43,$C0)
servo b.2, 255
wait 10
servopos b.2, off
endif

goto readrtc
 

The bear

Senior Member
Hi,
What Picaxe are you using?
I hope your cat understands how to work the controls.
This bear isn't much help.
It's an interesting project, I will follow the progress and learn.
Good luck.........
 

Ocean

Member
Hi,
What Picaxe are you using?
I hope your cat understands how to work the controls.
This bear isn't much help.
It's an interesting project, I will follow the progress and learn.
Good luck.........
im using a 20m2 and i hope he dosent or else im in big trouble lol
 

Ocean

Member
ended up changing a lot of code yesterday. now im stuck on how to properly use the select case commands to make certain variables jump back to a number after reaching a target number so i can properly display double digits on an oled display

code:

'variables for RTC
serout c.7, n2400, (254,1)
pause 30

symbol secs = b0 'stores secs on rtc
symbol mins = b1 'stores mins on rtc
symbol hours = b2 'stores hrs on rtc
symbol secs_tens = b3 'for displaying sec on OLED
symbol secs_ones = b4
symbol mins_tens = b5 'for displaying mins on OLED
symbol mins_ones = b6
symbol hrs_tens = b7 'for displaying hrs on OLED
symbol hrs_ones = b8
symbol AM_PM = b9

'RTC section and menu selection
readrtc:
i2cslave %11010000, i2cslow, i2cbyte 'reads time from in 24 hr from rtc

hi2cin 0,(SECS, MINS, HOURS)

debug
'converts time to 12hr
let AM_PM = "A"
if hours > $11 then : let AM_PM = "P" : endif
if hours = $20 or hours = $21 then : let hours = hours - $6 : endif
if hours > $12 then : let hours = hours - $12 : endif
if hours = $0 then : let hours = $12 : endif

'converts time from bcd to ASCII
bcdtoascii hours, hrs_tens, hrs_ones
if hrs_tens = "0" then : let hrs_tens = " " : endif
bcdtoascii mins, mins_tens, mins_ones
bcdtoascii secs, secs_tens, secs_ones


'display time on OLED
serout c.7, n2400, (254,128)
serout c.7, n2400, (hrs_tens, hrs_ones, ":", mins_tens, mins_ones, ":", secs_tens, secs_ones," ", AM_PM, "M") 'Displays Current Time

'mode selection menu

if pinc.6 = 1 then : goto manualserving :endif 'press button 1 to go to manual
if pinc.5 = 1 then : goto autoserving : endif 'press button 2 to go to auto

goto alarm




manualserving:
serout c.7, n2400, (254,128)
serout c.7, n2400, ("Manual Serving ")
wait 1
gosub servo_on



servo_on:
serout c.7, n2400, (254,128)
do loop until pinC.6 = 1 'loops until button is pressed
serout c.7, n2400, ("Dispensing... ")
servo b.2, 255 'Starts Servo


do loop until pinC.6 = 0 'loops until button is released
servopos b.2, off 'Shuts off Servo
pause 50
serout c.7, n2400, (254,128)
serout c.7, n2400, ("Done! ")

pause 500

serout c.7, n2400, (254, 1)
pause 30

goto readrtc


autoserving:
serout c.7, n2400, (254,128)
serout c.7, n2400, ("Auto Serving ")
wait 1
serout c.7, n2400, (254,1)
pause 30

symbol secs2 = b10 'Alarm variables
symbol mins2 = b11 'Alarm Variables
symbol hours2 = b12 'Alarm Variables
symbol secs_tens2 = b13
symbol secs_ones2 = b14
symbol mins_tens2 = b15 'variables for display
symbol mins_ones2 = b16
symbol hrs_tens2 = b17
symbol hrs_ones2 = b18
symbol AM_PM2 = b19

b10 = $30
b11 = $30
b12 = $30
b13 = $30
b14 = $30
b15 = $30
b16 = $30
b17 = $30
b18 = $30
b19 = $30

b27 = 0

time_selcthrs:
'converts to 12hr
debug
let AM_PM2 = "A"
if hours2 > $11 then : let AM_PM2 = "P" : endif
if hours2 = $20 or hours2 = $21 then : let hours2 = hours2 - $6 : endif
if hours2 > $12 then : let hours2 = hours2 - $12 : endif
if hours2 = $0 then : let hours2 = $12 : endif

bcdtoascii hours2, hrs_tens2, hrs_ones2
if hrs_tens2 = "0" then : let hrs_tens2 = " " : endif
bcdtoascii mins2, mins_tens2, mins_ones2
bcdtoascii secs2, secs_tens2, secs_ones2

serout c.7 ,n2400, (254,128)
serout c.7 ,n2400, (hrs_tens2, hrs_ones2, ":", mins_tens2, mins_ones2, AM_PM2, "M")

select case b11
case $3C
b11 = $30
endselect

select case b12
case $13
b12 = $30
endselect

if pinc.6 = 1 then : inc b11
pause 100
endif

if pinc.5 = 1 then : inc b12
pause 100
endif

button c.4,1,255,0,b27,0,time_selcthrs
goto timeset

timeset:
serout c.7, n2400, (254,1)
pause 30
serout c.7, n2400, (254,128)
serout c.7, n2400, ("Feeder Set! ")
goto readrtc


alarm:
if hrs_tens2 = hrs_tens and hrs_ones2 = hrs_ones and mins_tens = mins_tens2 and mins_ones = mins_ones2 then
tune B.6, 6,%00000001,($65,$65,$65,$EA,$C5,$43,$42,$40,$CA,$05,$43,$42,$40,$CA,$05,$43,$42,$43,$C0,$2C,$65,$65,$65,$EA,$C5,$43,$42,$40,$CA,$05,$43,$42,$40,$CA,$05,$43,$42,$43,$C0)
serout c.7, n2400, (254,128)
serout c.7, n2400, ("Dispensing... ")
servo b.2, 255
wait 10
servopos b.2, off
serout c.7, n2400, ("Done! ")
else
goto readrtc
endif
 

lbenson

Senior Member
Note that if your code has indentation to help make its structure easier to follow (which arguably every program should have--especially one which reaches the length yours has), you can preserve it in the forum by enclosing it in the forum tags "[ code]" and "[ /code]" (without the spaces following "[").
 

PieM

Senior Member
IMHO, too many goto and a gosub without return.
Try a main program with several gosub.
 

Ocean

Member
Note that if your code has indentation to help make its structure easier to follow (which arguably every program should have--especially one which reaches the length yours has), you can preserve it in the forum by enclosing it in the forum tags "[ code]" and "[ /code]" (without the spaces following "[").
Thank you for the tip! First time using the forums:)
 
Top