//tx_rx03 Robert Hart Mar 2019. //https://roberthart56.github.io/SCFAB/SC_lab/Sensors/tx_rx_sensors/index.html //Modified from Adrián´s Adrianino´s step response: https://fabacademy.org/2020/labs/leon/students/adrian-torres/adrianino.html#step //Pin changes. //Added digitalWriteFast. //Added Oled screen output. #include #include #include #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); long result; //variable for the result of the tx_rx measurement. int analog_pin = A2; // PA2 of the ATtiny1614 int tx_pin = A1; // PA1 of the ATtiny1614 void setup() { pinMode(tx_pin,OUTPUT); //Pin 2 provides the voltage step Serial.begin(115200); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); for(;;); } delay(2000); display.clearDisplay(); display.setTextColor(WHITE); } long tx_rx(){ //Function to execute rx_tx algorithm and return a value //that depends on coupling of two electrodes. //Value returned is a long integer. int read_high; int read_low; int diff; long int sum; int N_samples = 100; //Number of samples to take. Larger number slows it down, but reduces scatter. sum = 0; for (int i = 0; i < N_samples; i++){ digitalWriteFast(tx_pin,HIGH); //Step the voltage high on conductor 1. read_high = analogRead(analog_pin); //Measure response of conductor 2. delayMicroseconds(100); //Delay to reach steady state. digitalWriteFast(tx_pin,LOW); //Step the voltage to zero on conductor 1. read_low = analogRead(analog_pin); //Measure response of conductor 2. diff = read_high - read_low; //desired answer is the difference between high and low. sum += diff; //Sums up N_samples of these measurements. } return sum; } //End of tx_rx function. void loop() { result = tx_rx(); result = map(result, 19000, 50000, 0, 1024); //I recommend mapping the values of the two copper plates, it will depend on their size Serial.println(result); delay(100); delay(100); //clear display display.clearDisplay(); if (result > 0) {// display display.setTextSize(1); display.setCursor(0,0); display.print("OOPS TILTED!"); display.setTextSize(2);} else { // display display.setTextSize(1); display.setCursor(0,0); display.print("Value:"); display.setTextSize(2); display.setCursor(0,10); display.print(result); display.print("");} display.display(); }