14. Networking and Communications

For this week’s individual assignment, I had to design, build, and connect wired or wireless node(s) with network or bus addresses, according to the schedule. For the group assignment, we had to send a message between two projects.

Individual Assignment

I’m completely new to networking, but I wanted to try to communicate between my boards from input week (capacitive sensor) and output week (lcd display). I wanted to test if I could communicate between the two boards using only TX and RX pins. I referenced last year fab student Kai Vincent’s code for his lcd display code, and his use of the software serial. I wrote up these codes:

A code for my sensor board:

#include <util/delay.h>

#define CHARGEPIN 0
#define READPIN 1
#define settleDelay 100
#define chargeDelay 1

int reading;

void setup() {
  Serial.begin(115200);
  pinMode(CHARGEPIN, OUTPUT);
  digitalWrite(CHARGEPIN, LOW);
  pinMode(READPIN, INPUT);  
}

void loop() {
  //settle, charge, and wait
  delay(settleDelay);        
  digitalWrite(CHARGEPIN, HIGH);
  delay(chargeDelay);
  reading = analogRead(READPIN);
  //send result
  //Serial.println(reading);
  if (reading < 1015){
    Serial.println("Touch detected");
  }
  else Serial.println("No touch detected");
  delay(1000);
}

and a code for my display board:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int count;
const int RX = 4, TX = 5;
SoftwareSerial mySerial (RX, TX);

void setup()
{
  // initialize the LCD
  lcd.begin();
  // Turn on the blacklight and print a message.
  lcd.backlight();
  mySerial.begin(115200);
}

void loop()
{
  if (mySerial.available()) {
    // Serial.write(mySerial.read());
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (mySerial.available() > 0) {
      // display each character to the LCD
      lcd.write(mySerial.read());
    }
  }
}

This is what my setup looked like. After programming each board, I used a breadboard for easier connections. I placed my two boards next to each other. I used jumper wires to connect the ftdi chip to the sensor board, then more jumper wires to connect the grounds, powers, TX to RX, and RX to TX between the two boards.

I tested them out, and it didn’t work. I realized then that I had plugged my sensor plate into the wrong pin header on my input board. After changing the pins, it was still weird. Many of the characters didn’t come through. I thought it was a problem with my code syntax, so I changed the code so that the lcd display would only print “Touch detected” if it detected a touch, and nothing otherwise. This ended up being messy as well– the display would just update whenever I touched the sensor. But this was good news! I could communicate between the two boards.

As I messed around with the code, making more small changes in if statements, while loops, print statements, and delay times, I realized that I had a timing issue. The delay of 1000 in the sensor board code that waited after the serial monitor was updated and the delay of 100 milliseconds for the entire message to arrive did not combine well. I wanted to then test how I could fix the timing issue, so I changed the delay in the sensor board code to 2000 and made the delay in the display board 500. I had also ended up making the display simple N’s and Y’s, to see if there was No touch, or if, Yes. I found that there were 4, or 2000/500 N’s or Y’s printed each time.

So, I just had to make the two delays the same in order to get one character out. I changed them both to 500, and it worked well, aside from the weird characters (that I assumed meant a new line).

Now that it was sort of working, I wanted to go back to Touch or No touch. I changed the “Y“‘s and “N“‘s to “Touch” and “No touch,” and it sort of worked. You can tell when the display says what, but the characters are still a little odd. I think this is because of the time it takes to update. The general pattern of the display prints is that the characters print from left to right, so I think there may still be either a timing issue, or an issue with the display needing to wait longer to read the serial monitor output. But this is the working version, along with its code!

#include <util/delay.h>

#define CHARGEPIN 0
#define READPIN 1
#define settleDelay 100
#define chargeDelay 1

int reading;

void setup() {
  Serial.begin(115200);
  pinMode(CHARGEPIN, OUTPUT);
  digitalWrite(CHARGEPIN, LOW);
  pinMode(READPIN, INPUT);  
}

void loop() {
  //settle, charge, and wait
  delay(settleDelay);        
  digitalWrite(CHARGEPIN, HIGH);
  delay(chargeDelay);
  reading = analogRead(READPIN);
  //send result
  //Serial.println(reading);
  if (reading < 1015){
    Serial.println("Touch");
  }
  else Serial.println("No touch");
  delay(500);
}
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int count;
const int RX = 4, TX = 5;
SoftwareSerial mySerial (RX, TX);

void setup()
{
  // initialize the LCD
  lcd.begin();
  // Turn on the blacklight and print a message.
  lcd.backlight();
  mySerial.begin(115200);
}

void loop()
{
  if (mySerial.available()) {
    // Serial.write(mySerial.read());
    // wait a bit for the entire message to arrive
    delay(500);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (mySerial.available() > 0) {
      // display each character to the LCD
      lcd.write(mySerial.read());
    }
  }
}

After meeting with Dr. Fagan, he recommended that I try a non-blocking delay. I looked at this example using the method millis() on Arduino. I replaced the delays I had with if statements using variables previousMillis and currentMillis. I also changed the delay interval on the sensor code to 1000, and the delay value on the display to 100. To my surprise, it worked much better!

Here are my files for this week: Week 14 files

Group Assignment

The group assignment can be found here.