#include <xc.h>
#define _XTAL_FREQ 20e6
void main() {
TRISA0 = 1; // RA0 = FSR input (analog)
TRISB0 = 1; // RB0 = Touch input (digital)
TRISD0 = 0; // RD0 = TRIAC control (output)
ADCON1 = 0x80; // AN0 analog, others digital
ADCON0 = 0x01; // ADC on, AN0, Fosc/2
while (1) {
ADCON0bits.GO = 1; // Start ADC conversion
while (ADCON0bits.GO); // Wait for conversion
int fsrValue = ADRESH; // Read FSR (8-bit)
int touchValue = PORTBbits.RB0; // Read touch sensor
// Cut power if grip is loose (FSR low or touch released)
if (fsrValue < 50 || touchValue == 0) RD0 = 0; // TRIAC OFF
else RD0 = 1; // TRIAC ON
__delay_ms(10);
}
}
I2luY2x1ZGUgPHhjLmg+CiNkZWZpbmUgX1hUQUxfRlJFUSAyMGU2Cgp2b2lkIG1haW4oKSB7CiAgICBUUklTQTAgPSAxOyAgICAvLyBSQTAgPSBGU1IgaW5wdXQgKGFuYWxvZykKICAgIFRSSVNCMCA9IDE7ICAgIC8vIFJCMCA9IFRvdWNoIGlucHV0IChkaWdpdGFsKQogICAgVFJJU0QwID0gMDsgICAgLy8gUkQwID0gVFJJQUMgY29udHJvbCAob3V0cHV0KQogICAgQURDT04xID0gMHg4MDsgLy8gQU4wIGFuYWxvZywgb3RoZXJzIGRpZ2l0YWwKICAgIEFEQ09OMCA9IDB4MDE7IC8vIEFEQyBvbiwgQU4wLCBGb3NjLzIKCiAgICB3aGlsZSAoMSkgewogICAgICAgIEFEQ09OMGJpdHMuR08gPSAxOyAgICAgLy8gU3RhcnQgQURDIGNvbnZlcnNpb24KICAgICAgICB3aGlsZSAoQURDT04wYml0cy5HTyk7ICAvLyBXYWl0IGZvciBjb252ZXJzaW9uCiAgICAgICAgaW50IGZzclZhbHVlID0gQURSRVNIOyAgLy8gUmVhZCBGU1IgKDgtYml0KQoKICAgICAgICBpbnQgdG91Y2hWYWx1ZSA9IFBPUlRCYml0cy5SQjA7IC8vIFJlYWQgdG91Y2ggc2Vuc29yCgogICAgICAgIC8vIEN1dCBwb3dlciBpZiBncmlwIGlzIGxvb3NlIChGU1IgbG93IG9yIHRvdWNoIHJlbGVhc2VkKQogICAgICAgIGlmIChmc3JWYWx1ZSA8IDUwIHx8IHRvdWNoVmFsdWUgPT0gMCkgUkQwID0gMDsgLy8gVFJJQUMgT0ZGCiAgICAgICAgZWxzZSBSRDAgPSAxOyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vIFRSSUFDIE9OCiAgICAgICAgX19kZWxheV9tcygxMCk7CiAgICB9Cn0=