Assignment
-
Individual assignment:
- Adesign, build, and connect wired or wireless node(s) with network or bus addresses
-
Group assignment:
- Send a message between two projects
Individual assignment
Plan of Communication
The input message would activate the main board to send data through serial to secondary boards. In my case, I would split one byte into two parts, and the first 4 bits as an address and the later 4 bits as a command.
Connector on PCBs
I used RCPT connectors 4POS and 6POS to communicate in between three PCB boards. The idea is to press the button and turn on or off a pump and a valve simultaneously. Therefore, I will use one byte to send the address and command to the microcontroller of the pump and valve. At the receiver end, it needs to encode the byte and filter out the commands they need.
Bitwise Operators
In this section, I studied through the tutorial of Programing and Arduino Reference. There are Bitwise AND, OR, XOR, and Shift Left and Right to operate the bits.
Once I know it I could use TX and RX channel of them to communicate. I used functions, Serial.read() and Serial.write() to send data through the cables and Serial.print to debug.
Important code on Hello Board
byte address = B0000;
byte motoraddress = B0000; // 0000 is for motor
byte valveaddress = B0001; // 1000 is for valve
Initialize the address and assign different address to motor and valve.
void loop() {
byte command_motor = B0000; // 0000 is off, 1000 is on for motor
byte command_valve = B0000; // 0000 is off, 1000 is on for valve
int OnorOff = digitalRead(BUTTON);
if(OnorOff == 0)
{
Serial.print("Motor on ");
address = motoraddress;
command_motor = command_motor | B0001; // Motor on
sendMessage(address, command_motor);
Serial.print("Valve off ");
address = valveaddress;
command_valve = command_valve | B0000; // Valve off
sendMessage(address, command_valve);
}
...
}
Define the command, and read the button on PCB. Set the condition for press or release of the button. Send the message with sendMessage fuction.
void sendMessage(byte address, byte command) {
byte message = B00000000;
message = (address << 4) | command;
Serial.println(message,BIN);
Serial.write(message);
}
Combine the address and command bits into one byte and send it with Serial.write().
Important code on DCMotor Driver
void loop() {
int incomingMessage = Serial.read(); // for incoming serial data
byte address = incomingMessage >> 4; // 0000 is for motor, 0001 is for valve
byte command = incomingMessage & B00001111; // get last 4 bits from the message
if (address == B0000) { // address to motor
if (command == B0001) MotorOn();
else if (command == B0000) MotorOff();
}
}
Read incoming message and split it into address and command. Moreover read only the right command to Motor Driver by checking the address.
void MotorOn(){
digitalWrite(motorIN1, 1);
digitalWrite(motorIN2, 0);
}
void MotorOff(){
digitalWrite(motorIN1, 0);
digitalWrite(motorIN2, 0);
}
Simplify the turn on or off the motor into functions.
Important code on Valve Controller
void loop() {
int incomingMessage = Serial.read(); // for incoming serial data
byte address = incomingMessage >> 4; // 0000 is for motor, 0001 is for valve
byte command = incomingMessage & B00001111; // get last 4 bits from the message
if (address == B0001){ // address to Valve
if (command == B0001) ValveOn();
else if (command == B0000) ValveOff();
}
}
Samething that I did on Motor Driver, but this time I only read the address to valve.
void ValveOn(){ //valve ON if would be heated up gradually
digitalWrite(valve, HIGH);
}
void ValveOff(){
digitalWrite(valve, LOW);
}
Simplify the turn on or off the valve into functions.
Schematic and Trace for Valve Controller
I used N-Mosfet to control valves, I referred to Arduino Project Hub, Seed Studio, bildr, and Stockfeel to understand how could I apply a N-Mosfet to my boards.
Valve Controller testing
Combine three of them
The motor and valve can work simultaneously.
Test for the implication in Final Project
Challenges
-
The function, Serial.Write(), wouldn’t show in the Serial.monitors, which is hard for debugging.
!! Solution: Use Serial.print() or Serial.println() to debug, but remember to clean it after debugging, otherwise it would impact the communication data.
Group Assignments
Please find the group assignment from Kitijia’s Week14 NETWORKING AND COMMUNICATIONS.
Download
The Week 15 zip file includes: