//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 //By: Pedro Chana :https://fabacademy.org/2022/labs/uemadrid/students/pedro-chana/assignment12.html //Pin changes. //Added digitalWriteFast. //Added Oled screen output. //Modified by Terrence Carew:http://fabacademy.org/2021/labs/vancouver/students/terrence-carew/assignments/final_project.html //Added level variable. //Added New Mapping. //Added Intro display //Added "millis()" function timing. //Added more Pin associated devices. //Added"level" response conditions. #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 = A4; // PA2 of the ATtiny3216 int tx_pin = A5; // PA1 of the ATtiny3216 int led1 = 2; int led2 = 3; int led3 = 4; int midbtn = 15; int relay1 = 10; int relay2 = 11; int relay3 = 12; int relay4 = 13; void setup() { pinMode(tx_pin,OUTPUT); //Pin 2 provides the voltage step pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(15, INPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); 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() { long level; // declares for water level reading if (millis()< 3000){ // if microcontroller has been running less than 3 seconds. display.clearDisplay(); // Name intro display display.setTextSize(2); display.setCursor(0,0); display.print("Terrepon-X"); display.print(""); display.display(); } delay(3000); level = map(tx_rx(), 6500,12000, 0, 100); // maps the level for readings Serial.print("level:") Serial.println(level); delay(100); delay(100); if (level < 1700){ //condition for if water is below a certain level //clear display display.clearDisplay(); // display display.setTextSize(1); display.setCursor(0,0); display.print("level:"); display.setTextSize(2); display.setCursor(0,10); display.print("Too Low"); display.print(""); display.display(); digitalWrite(led1, HIGH); digitalWrite(relay1, HIGH); digitalWrite(led2, HIGH); digitalWrite(relay2, HIGH); digitalWrite(led3, LOW); digitalWrite(relay4, LOW); } else if (level >=1700){ // condition if water is above or at acceptable level //clear display display.clearDisplay(); // display display.setTextSize(1); display.setCursor(0,0); display.print("level:"); display.setTextSize(2); display.setCursor(0,10); display.print("Full"); display.print(""); display.display(); digitalWrite(led1, LOW); digitalWrite(relay1, LOW); digitalWrite(led2, LOW); digitalWrite(relay2, LOW); digitalWrite(led3, HIGH); digitalWrite(relay4, HIGH); } }