/* James Khan Fab Academy networking and communication week week. https://fabacademy.org/2021/labs/vancouver/students/james-khan/assignments/week13/ Used the code I developed for my input week as a starting point and modified for communication. https://fabacademy.org/2021/labs/vancouver/students/james-khan/assignments/week11/ This program is for my selfmade Attiny14 board and has an ultrasonic sensor attached to it and communicates with another board (an RP2040, with a different program). This program constantly measures the distance from the ultrasonic sensor. When it gets sent a message on its RX data pin, it displays that message on the attached OLED screen as well. It then sends back confirmation on the TX pin along with the measured distance. */ #include // for OLED #include // for OLED #include // for OLED const int SCREEN_WIDTH = 128; // OLED display width, in pixels const int 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); // for OLED // DEfine variables Ultrasonic #define ECHO_PIN 9 #define TRIG_PIN 8 String incomingString ; //to hold incomming serial data. void setup() { Serial.begin(115200); // if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 Serial.println(F("SSD1306 allocation failed")); // genetare arror msg if oled can't initialize while(true); } // for ultrasonic setup pinMode(LED_BUILTIN, OUTPUT); pinMode(TRIG_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); } //read and calculate the distance from ultrasonic sensor. long readDistanceCM() { // changed it from the regular float because of memory issues with serial.print digitalWrite(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); unsigned long duration = pulseIn(ECHO_PIN, HIGH); //corrects possible negative interperetation if measurement too big return duration * 0.034 / 2; } void loop() { long distance = readDistanceCM(); // changed it from the regular float because of memory issues using serial.print delay(200); if (Serial.available()) { incomingString=Serial.readString(); // If anything comes in Serial save it to variable Serial.print("got it: "); // send confirmation back to sender with string sent Serial.println(incomingString); Serial.print("dist: "); Serial.println(distance); // send the distance measured from attiny's ultrasonic sensor to serial delay(100); } display.clearDisplay(); // oled clear display.setTextSize(2); // oled setup // text size of 2 only gives us grid size of 10 wide by 4 tall characters. // but a text size of 1 is pretty tiny to show up well on a video, but gives allot more characters. // the below prompts are minimal to keep the bigger size font. display.setTextColor(WHITE); display.setCursor(0, 0); display.print("d:"); // static display display.println(distance); // display distance display.println("incoming:"); // static display display.println(incomingString); // display string from serial input display.display(); display.println("\n"); delay(200); }