The below script is an example of how to implement the limit switch action by using an analog sensor. This script can replace the build in analog input function of the firmware and can be used in cases that the analog sensor is noisy. By having the analog signal as an input to the script, the noise can be filtered out and the clean signal can be used instead of the original that the firmware uses.
In the below example, the analog position sensor that is connected to the gearbox of the motor gives a value from 0 to 3.3 V, depending on the motor position. 0.4 V have been configured as reverse limit switch and 3 V have been configured as forward limit switch. So when the motor goes at low position it stops and is allowed only to go at positive direction. If the motor is at high position it will stop and it will be allowed to go only at reverse direction.
The script can implement the same function by configuring the UP_LIMIT and DOWN_LIMIT parameters.
Additionally, it will have a 100 ms filter to smoothen the analog input readings. Because the limit switch function cannot be fully implemented by the script, the scrtipt will act as follows:
- When the filtered value is at the low limit, it will configure the reverse limit switch at a high voltage so it will be costantly enabled
- When the filtered value is at the high limit, it will configure the forward limit switch to a low voltage so it will be constantly enabled
- In the between, the limit switch operation will be disabled
The script follows. Keep in mind that this is an example and is not fully tested and validated by our side.