// // hello.txrx.t1614.ino // // ATtiny1614 step response // // // This work may be reproduced, modified, distributed, // performed, and displayed for any purpose, but must // acknowledge this project. Copyright is retained and // must be preserved. The work is provided as is; no // warranty is provided, and users accept all liability. // #define rxpin1 PIN_PA5 // receive pin (PA5 on ATtiny1614) #define rxpin2 PIN_PA6 // receive pin (PA6 on ATtiny1614) #define txpin PIN_PA7 // transmit pin (PA7 on ATtiny1614) #define settle 100 // settle time #define samples 100 // number of samples to accumulate void setup() { Serial.begin(115200); // start serial pinMode(txpin, OUTPUT); // set transmit pin to output pinMode(rxpin1, INPUT); // set receive pin1 to input pinMode(rxpin2, INPUT); // set receive pin2 to input } void loop() { int32_t up, down; up = down = 0; noInterrupts(); // disable interrupts while measuring for (int i = 0; i < samples; ++i) { digitalWrite(txpin, HIGH); // charge up up += analogRead(rxpin1); // read from rxpin1 delayMicroseconds(settle); // settle digitalWrite(txpin, LOW); // charge down down += analogRead(rxpin1); // read from rxpin1 delayMicroseconds(settle); // settle } interrupts(); // enable interrupts after measuring int32_t difference = up - down; // Display messages based on the measured values if (difference >= -20000 && difference <= -18000) { Serial.print("Bottom pad touched: "); Serial.println(difference); } else if (difference >= -9000 && difference <= -5000) { Serial.print("Left pad touched: "); Serial.println(difference); } else if (difference < -20000) { Serial.print("Right pad touched: "); Serial.println(difference); } else if (difference >= -12000 && difference <= -8500) { Serial.print("Untouched: "); Serial.println(difference); } Serial.flush(); // finish communicating before measuring }