For the group assignment we are communicating 3 boards, Gerhard's board has a LDR photoresistor that senses the amount of light in the environment and turns on/off an LED connected to his board, the sensed value is sent via Serial Communication to Jefferson's board which also turns on an LED but also communicates via I2C with my board to activate/deactivate the motor based on the value sent by Gerhard's board.
Gerhard's code is:
// ==========================
// SENDER
// ==========================
#include
SoftwareSerial softwareSerial(0, 1); // RX, TX
const int sendPin = 10; // LED pin
const int ldrPin = 9; // LDR pin
void setup() {
Serial.begin(9600);
softwareSerial.begin(9600);
pinMode(sendPin, OUTPUT);
pinMode(ldrPin, INPUT);
Serial.println("Sender ready");
}
void loop() {
// Read ldr value
int ldrStatus = analogRead(ldrPin);
/******* Serial Monitor for debugging *******/
Serial.print("[SENDING] Data: ");
Serial.println(ldrStatus); // Displays the value of "ldrStatus" on the serial monitor
/******* Send data to Reciver *******/
softwareSerial.write(ldrStatus);
if (ldrStatus <= 500) {
digitalWrite(sendPin, HIGH);
// for debugging
//Serial.println("[STATUS] Its DARK Turn ON the LED");
} else {
digitalWrite(sendPin, LOW);
// for debugging
//Serial.println("[STATUS] Its BRIGHT Turn off the LED");
}
delay(200);
}
Jefferson's code is:
// ==========================
// RECIVER
// ==========================
#include
#include
SoftwareSerial softwareSerial(0, 1); // RX, TX
#define LED 10 // LED pin
int data; // data from the sender
// Creates a object
FabMotorController MotorControl;
void setup() {
Serial.begin(9600);
softwareSerial.begin(9600);
pinMode(LED, OUTPUT);
Serial.println("Reciver ready");
// Initializes I2C communication with the secondary at address 0x04
MotorControl.begin(0x04);
}
void loop() {
/* Read data from Sender */
while (softwareSerial.available()){
data = softwareSerial.read();
}
Serial.print("[RECIVER] data: ");
Serial.println(data);
if (data <= 125){
digitalWrite(LED, HIGH);
MotorControl.run(OUTPUT1, 100);
}
else{
digitalWrite(LED, LOW);
MotorControl.run(OUTPUT1, 0);
}
delay(200);
}
Sends the sensed values through serial connection to Board B.
Board B:
1) is the Serial connection to Board A, receives sensed light intensity values;the board analyzes the values and according to them sends a message through ->
2) I2C connection to Board C, to turn the motor On or Off.
3) is FTDI for Serial monitor, and
4) is UPDI for programming.
Board C:
Receives the messages from Board B to turn the DC motor On and Off.
// ==========================// SENDER// ==========================#include<SoftwareSerial.h>SoftwareSerialsoftwareSerial(0,1);// RX, TXconstintsendPin=10;// LED pinconstintldrPin=9;// LDR pinvoidsetup(){Serial.begin(9600);softwareSerial.begin(9600);pinMode(sendPin,OUTPUT);pinMode(ldrPin,INPUT);Serial.println("Sender ready");}voidloop(){// Read ldr valueintldrStatus=analogRead(ldrPin);/** Serial Monitor for debugging **/Serial.print("[SENDING] Data: ");Serial.println(ldrStatus);// Displays the value of "ldrStatus" on the serial monitor/** Send data to Reciver **/softwareSerial.write(ldrStatus);if(ldrStatus<=500){digitalWrite(sendPin,HIGH);// for debugging//Serial.println("[STATUS] Its DARK Turn ON the LED");}else{digitalWrite(sendPin,LOW);// for debugging//Serial.println("[STATUS] Its BRIGHT Turn off the LED");}delay(200);}
// ===========================// Receiver// ==========================/* Made by Jefferson Sandoval in collaboration with Harley Lara * for the Embedded Networking and Communications group assignment * during the FabAcademy2021 * * This code was uploaded to a board with an Attiny1614 microcontroller * board. Board documentation: * http://fabacademy.org/2021/labs/kamplintfort/students/jefferson-sandoval/assignments/week07/ * * My documentation for this assignment: * http://fabacademy.org/2021/labs/kamplintfort/students/jefferson-sandoval/assignments/week14/ */#include<SoftwareSerial.h> //Include library to create an extra serial communication#include<FabMotorController.h> //Include library to control Motor controller boardSoftwareSerialsoftwareSerial(0,1);//Assign softwareSerial(RX, TX) pinsFabMotorControllerMotorControl;//Create an objectconstintled=10;//Declare constant for the built-in LEDintdata;//Declare ingeter variable to read data from serial connectionvoidsetup(){Serial.begin(9600);//Begin serial communication (used for monitor)softwareSerial.begin(9600);//Begin the extra serial communicationpinMode(LED,OUTPUT);//Set LED as outputSerial.println("Reciever ready");//Starting serial monitor messageMotorControl.begin(0x04);// Initialize I2C communication at address 0x04}voidloop(){while(softwareSerial.available()){data=softwareSerial.read();// Read data from Sender (Board A)}//Print the received values from Board ASerial.print("[RECEIVER] data: ");Serial.println(data);if(data<=125){//If the received value is less than 125 (darkness)...digitalWrite(LED,HIGH);//Turn on the built-in LEDMotorControl.run(OUTPUT1,100);//Send message to Board C to turn on the motor}else{//else...digitalWrite(LED,LOW);//Turn off the built-in LEDMotorControl.run(OUTPUT1,0);//Send message to Board C to turn off the motor}delay(100);}