I am having trouble with adding my HX711 load cell to my project. it seems to conflict with my RTC code. RTC works, but HX711 doesn't

#picaxe 18M2
#no_data

symbol baud = N2400
symbol oled = C.3
symbol seconds = b0
symbol mins = b1
symbol hour = b2
symbol day = b3
symbol date = b4
symbol month = b5
symbol year = b6
symbol control = b7 ; Control register for RTC
symbol debounce = b8 ; Button debounce variable

; Set up I2C for RTC DS3231
init:
hi2csetup i2cmaster, %11010000, i2cslow, i2cbyte ; Set RTC slave address
;writei2c 0, ($00, $43, $16, $06, $23, $11, $24, $10)

pause 800
serout c.3, n2400, (254, 1) ; Clear OLED screen
pause 100
; Display initial OLED message
;serout oled, baud, (254, 128, " Cat Feeder ")
;serout oled, baud, (254, 192, " Press to Feed ")

; Uncomment to set the clock
; goto set_clock

main:
do
; Read RTC values
hi2cin 0, (seconds, mins, hour, day, date, month, year)
pause 100

; Convert BCD values to ASCII for display
bcdtoascii seconds, b10, b11
bcdtoascii mins, b12, b13
bcdtoascii hour, b14, b15
bcdtoascii date, b16, b17
bcdtoascii month, b18, b19
bcdtoascii year, b20, b21

; Display day of the week on OLED
; select case day
; case 1: serout oled, baud, (254, 128, " Mo ")
; case 2: serout oled, baud, (254, 128, " Tu ")
; case 3: serout oled, baud, (254, 128, " We ")
; case 4: serout oled, baud, (254, 128, " Th ")
; case 5: serout oled, baud, (254, 128, " Fr ")
; case 6: serout oled, baud, (254, 128, " Sa ")
; case 7: serout oled, baud, (254, 128, " Su ")
; endselect

; Display date on OLED
;serout oled, baud, (254, 133, b16, b17, ".", b18, b19, ".20", b20, b21)

; Display time on OLED


serout oled, baud, (254, 193, b14, b15, ":", b12, b13, ":", b10, b11)
SerOut c.3, n2400, (254, 128, "Weight:", #w0, " g ")
; Check button for manual feeding
button C.5, 1, 255, 0, debounce, 1, manual_feed

; Automatic feeding at 7:00 AM
if hour = $16 and mins = $44 and seconds < $02 then
gosub automatic_feed
endif

pause 500
loop

manual_feed:
; Button is pressed - rotate servo for manual feeding
serout oled, baud, (254, 128, " Feeding... ")
servo b.3, 148 ; Initialize servo
servopos b.3, 224 ; Rotate servo clockwise
pause 2000 ; Rotate for 1 second
servopos b.3, 148 ; Stop servo
pause 3000 ; Small delay to prevent accidental re-triggering
debounce = 0 ; Reset debounce variable
return


automatic_feed:
; Automatic feeding at scheduled time
serout oled, baud, (254, 128, " Feeding... ")
servo b.3, 148 ; Initialize servo
servopos b.3, 224 ; Rotate servo clockwise
pause 2000 ; Rotate for 2 seconds
servopos b.3, 148 ; Stop servo
pause 1000
return

set_clock:

; Write current time and date (e.g., 8:30 AM on Saturday, 23rd November 2024)
let seconds = $00 ; 00 seconds
let mins = $37 ; 30 minutes (BCD format)
let hour = $10 ; 8:00 AM (24-hour format)
let day = $06 ; Saturday (Day 1 = Monday, Day 7 = Sunday)
let date = $23 ; 23rd (BCD format)
let month = $11 ; November (BCD format)
let year = $24 ; Year 2024 (BCD format)
let control = %00010000 ; Enable 1Hz square wave output (optional)

hi2cout 0, ($00, $08, $16, $06, $23, $11, $24, $10)

serout oled, baud, (254, 1, " Clock Set! ") ; Notify clock is set

pause 2000

goto main
return
Symbol CLK = C.2
Symbol DIN = C.0 : Symbol DIN_PIN = pinC.0
Symbol weightRaw = w0 ' Store raw weight reading
Symbol tareValue = w1 ' Store tare value (for zeroing)
Symbol calibrationFactor = w2 ' Store calibration factor (used for conversion)
;Define bitCount for loop
Symbol bitCount = w3 ' Define the bitCount variable



PowerOnReset:
Input DIN
High CLK
Pause 5000
Low CLK

' Set a calibration factor (to be determined through experimentation)
calibrationFactor = 500 ' Example value (adjust after calibration)

' Set the tare value (offset with no weight on load cell)
tareValue = 0 ' Set the tare value to 55000 (your measured offset)


MainLoop:
Do
' Read HX711 and process data
Gosub Read_HX711

' Subtract the tare value to get the actual weight
weightRaw = w0 - tareValue ' Subtract tare value to get net weight

' Apply calibration factor to convert raw data to readable weight
weightRaw = weightRaw / calibrationFactor ' Apply calibration factor

' Display the weight on the OLED
SerOut oled, 9600, (254, 128) ' Move cursor to top-left of OLED
SerOut oled, 9600, ("Weight: ", #weightRaw, " g ") ' Display weight in grams

' Send the processed weight data over serial for debugging
SerTxd("Weight (Processed): ", #weightRaw, " g", CR, LF)

Pause 1000 ' Wait 1 second before next reading
Loop

' Subroutine to read HX711 and get raw data
Read_HX711:
Gosub Read_Channel_A_Gain_128
Return

' Subroutine to read data from HX711 (Gain = 128 for Channel A)
Read_Channel_A_Gain_128:
Gosub Read_24bits
PulsOut CLK, 1
Return

' Subroutine to read 24 bits of data from HX711
Read_24bits:
Do : Loop Until DIN_PIN = 0 ' Wait for data to be ready
w0 = 0 ' Initialize w0 for raw data storage
For bitCount = 1 To 24
PulsOut CLK, 1 ' Clock the HX711
w0 = w0 + w0 + DIN_PIN ' Shift in the data bit-by-bit
Next
Return
 
You have overlapping variables. E.g. TareValue (w1) is also used as Hour & Day ( b2 & b3 )

You cannot normally use bytes and words that overlap, unless you are very careful with your coding.
 
There seems to be a whole section of code, to which there is no path (from 'PowerOnReset' onwards).
It looks like two seperate programmes have been concatenated together. There is an overall 'do / loop' in the first section, and another in the second, but the first loop never exits to 'drop through' to the second.

Try restructuring the code like this ...

do

gosub clock_stuff

gosub weight_stuff

loop
 
My understanding of the button function is that it uses an internal goto, rather than an internal gosub for branching. This means your manual_feed routine needs to terminate with a goto rather than a return, otherwise your code is likely to misbehave and possibly crash.
button C.5, 1, 255, 0, debounce, 1, manual_feed
 
My understanding of the button function is that it uses an internal goto, rather than an internal gosub for branching. This means your manual_feed routine needs to terminate with a goto rather than a return, otherwise your code is likely to misbehave and possibly crash.
Funny you mention that a soon as ran the program my servo would keep pulsing on an off. Thanks for the input
 
Back
Top