Following on from this thread https://www.picaxeforum.co.uk/threads/tying-c-5-high-instead-of-low.33515/
I have asked AI to write me a program to add a calibration method for the servo settings. could someone please look at it and make any comments please.
Thank You in advance
From the AI chat
Assumptions this code uses
' Railway Point Controller - Servo Calibration with Plug-in Calibrator
' PICAXE 08M2
#picaxe 08m2
#no_data
setfreq m4
' -----------------------------
' Pin Definitions
' -----------------------------
symbol switchPin = pinC.3 ' Point lever input
symbol upBtn = pinC.1 ' Calibrator UP button
symbol downBtn = pinC.4 ' Calibrator DOWN button
symbol calibEn = pinC.5 ' Calibrator enable input
symbol servoPin = C.2 ' Servo output
symbol esp01Pin = C.0 ' Status to ESP-01
' -----------------------------
' Variables
' -----------------------------
symbol servoposition = b4
symbol normalPos = b5
symbol thrownPos = b6
symbol calibMode = b7
symbol lastCalibMode = b8
symbol tempPos = b9
' -----------------------------
' EEPROM addresses
' -----------------------------
symbol EEPROM_NORMAL = 0
symbol EEPROM_THROWN = 1
' -----------------------------
' Startup
' -----------------------------
init:
' Load saved positions
read EEPROM_NORMAL, normalPos
read EEPROM_THROWN, thrownPos
' First-run defaults
if normalPos = 0 then
normalPos = 75
write EEPROM_NORMAL, normalPos
endif
if thrownPos = 0 then
thrownPos = 175
write EEPROM_THROWN, thrownPos
endif
' Start servo
servo servoPin, 150
pause 1000
' Safety: wait until calibrator is OFF at boot
do
loop while calibEn = 1
lastCalibMode = 0
' -----------------------------
' Main loop
' -----------------------------
main:
if calibEn = 1 then
calibMode = 1
else
calibMode = 0
endif
' --------
' Normal mode
' --------
if calibMode = 0 then
if switchPin = 1 then
servoposition = thrownPos
high esp01Pin
else
servoposition = normalPos
low esp01Pin
endif
servopos servoPin, servoposition
pause 50
goto main
endif
' --------
' Calibration mode
' --------
if calibMode = 1 then
' If we just entered calibration, give servo a moment
if lastCalibMode = 0 then
pause 300
endif
' Decide which end is being calibrated from the lever position
if switchPin = 1 then
tempPos = thrownPos
else
tempPos = normalPos
endif
' Allow button trimming while calibration is enabled
do
' Re-check calibration enable continuously
if calibEn = 0 then
' Save the adjusted value before leaving
if switchPin = 1 then
thrownPos = tempPos
write EEPROM_THROWN, thrownPos
else
normalPos = tempPos
write EEPROM_NORMAL, normalPos
endif
pause 200
lastCalibMode = 0
goto main
endif
' UP button
if upBtn = 0 then
if tempPos < 255 then
tempPos = tempPos + 1
endif
servopos servoPin, tempPos
pause 100
endif
' DOWN button
if downBtn = 0 then
if tempPos > 0 then
tempPos = tempPos - 1
endif
servopos servoPin, tempPos
pause 100
endif
' Keep servo at current trim position
servopos servoPin, tempPos
pause 20
loop
endif
lastCalibMode = calibMode
goto main
I have asked AI to write me a program to add a calibration method for the servo settings. could someone please look at it and make any comments please.
Thank You in advance
From the AI chat
Assumptions this code uses
- C.2 = servo
- C.3 = point lever input
- C.1 = UP from calibrator
- C.4 = DOWN from calibrator
- C.5 = calibration enable input
- C.0 = ESP-01 status output
- C.5 is LOW normally, HIGH when calibrator ON
- calibration saves automatically when calibrator turns OFF
- lever selects which end is being adjusted:
- lever normal = adjust normalPos
- lever thrown = adjust thrownPos
How this code worksNormal mode- calibrator OFF
- lever controls the servo normally
- ESP-01 status output still works
Calibration mode- calibrator ON
- lever chooses which end you are trimming
- UP/DOWN move the servo
- when calibrator turns OFF, the current value is saved automatically
Important noteThis line in the code:
do<br>loop while calibEn = 1<br>
Picaxe
means the board waits at startup until the calibrator is OFF.
That matches the forum advice about keeping C.5 safe at power-up.
One thing I should mentionThe line:
symbol tempPos = b9<br>
Picaxe
may need checking because on an 08M2 the number of available scratch variables depends on how PICAXE maps them.
The program;
' Railway Point Controller - Servo Calibration with Plug-in Calibrator
' PICAXE 08M2
#picaxe 08m2
#no_data
setfreq m4
' -----------------------------
' Pin Definitions
' -----------------------------
symbol switchPin = pinC.3 ' Point lever input
symbol upBtn = pinC.1 ' Calibrator UP button
symbol downBtn = pinC.4 ' Calibrator DOWN button
symbol calibEn = pinC.5 ' Calibrator enable input
symbol servoPin = C.2 ' Servo output
symbol esp01Pin = C.0 ' Status to ESP-01
' -----------------------------
' Variables
' -----------------------------
symbol servoposition = b4
symbol normalPos = b5
symbol thrownPos = b6
symbol calibMode = b7
symbol lastCalibMode = b8
symbol tempPos = b9
' -----------------------------
' EEPROM addresses
' -----------------------------
symbol EEPROM_NORMAL = 0
symbol EEPROM_THROWN = 1
' -----------------------------
' Startup
' -----------------------------
init:
' Load saved positions
read EEPROM_NORMAL, normalPos
read EEPROM_THROWN, thrownPos
' First-run defaults
if normalPos = 0 then
normalPos = 75
write EEPROM_NORMAL, normalPos
endif
if thrownPos = 0 then
thrownPos = 175
write EEPROM_THROWN, thrownPos
endif
' Start servo
servo servoPin, 150
pause 1000
' Safety: wait until calibrator is OFF at boot
do
loop while calibEn = 1
lastCalibMode = 0
' -----------------------------
' Main loop
' -----------------------------
main:
if calibEn = 1 then
calibMode = 1
else
calibMode = 0
endif
' --------
' Normal mode
' --------
if calibMode = 0 then
if switchPin = 1 then
servoposition = thrownPos
high esp01Pin
else
servoposition = normalPos
low esp01Pin
endif
servopos servoPin, servoposition
pause 50
goto main
endif
' --------
' Calibration mode
' --------
if calibMode = 1 then
' If we just entered calibration, give servo a moment
if lastCalibMode = 0 then
pause 300
endif
' Decide which end is being calibrated from the lever position
if switchPin = 1 then
tempPos = thrownPos
else
tempPos = normalPos
endif
' Allow button trimming while calibration is enabled
do
' Re-check calibration enable continuously
if calibEn = 0 then
' Save the adjusted value before leaving
if switchPin = 1 then
thrownPos = tempPos
write EEPROM_THROWN, thrownPos
else
normalPos = tempPos
write EEPROM_NORMAL, normalPos
endif
pause 200
lastCalibMode = 0
goto main
endif
' UP button
if upBtn = 0 then
if tempPos < 255 then
tempPos = tempPos + 1
endif
servopos servoPin, tempPos
pause 100
endif
' DOWN button
if downBtn = 0 then
if tempPos > 0 then
tempPos = tempPos - 1
endif
servopos servoPin, tempPos
pause 100
endif
' Keep servo at current trim position
servopos servoPin, tempPos
pause 20
loop
endif
lastCalibMode = calibMode
goto main

