Assignment I was given, NEED HELP PLEASE!

Attached below is the flow chart we were given to follow on how to make the code so we can receive the transmission and a picture of the details i took down for the assignment.

Right so I was given an assignment from a lecturer where we connect multiple Arduino Uno boards together using Soft Serial to communicate with the RX and TX Pins, we have to pretend that were are one of the Slave boards which should only reply to the broadcast sent from the master which will be the Lecturers Board if it is addressed to us (we pick a address such as I’ll be number 24). The masters board will be 01. The master board will transmit a ‘' to signal the start of transmission which will be followed by the source so in this case it will be 01 for the masters board, the destination where its going (lets say 24 for my board), followed by a value up to 4 bytes from a floating Analog Pin and then a checksum (which will be 2 bytes) and a "#" to end the transmission. We have to make a reply to this if it is addressed to us which includes a SOT '’ the source (so since its coming from me it’ll be 24) the destination (master as we can only talk to the master 01) include a value for Pressure (4 bytes floating from Analog A0), a value for Temperature (3 bytes floating from Analog A1), a value for a Number (2 bytes floating from A2) and use a checksum from all this and then finish it with a ‘#’ to signal the EOT.

To be honest, we weren’t show how to code at all, we were told to use the resources tab and we have about a week to finish this, personally myself I have never coded before and I am really struggling, I only know how to get it to print out a word thats pre-written in the code into the serial monitor let alone most of whats above.

Can anyone point me in the right direction or help me at all?

Some code i have written to communicate between RX and TX Pins

 #include <AltSoftSerial.h>
AltSoftSerial mySerial;


void setup ()
{
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop ()
{
  if(mySerial.available())
  Serial.write(mySerial.read());
  
  if(Serial.available())
  mySerial.write(Serial.read());
}

"Can anyone point me in the right direction or help me at all?"

Get a copy of the written assignment and post it up to see just what is needed. Your instructor should have this.

zoomkat:
"Can anyone point me in the right direction or help me at all?"

Get a copy of the written assignment and post it up to see just what is needed. Your instructor should have this.

Currently there is nothing on the website, besides the notes which i have posted a picture of, the lecturer usually only puts a drop point and nothing else. My college closed because of Covid-19 and just before it got the announcement we had a class with him and he told us that we had 7 days to finish the code for it and he would make a place on moodle for the submissions which he hasn't done yet. So the only thing I have and all other 11 classmates have is that page i attached with details about the broadcast and receiving, which is everything he told us about it and wrote on the whiteboard. He also told us not to email him because he wouldn't reply.

"So the only thing I have and all other 11 classmates have is that page i attached with details about the broadcast and receiving, which is everything he told us about it and wrote on the whiteboard. "

Are you working independently or together with the 11 classmates? The picture is a logic diagram with what looks like boolean test for each one. Does "k" represent the packet that is sent to your board? There is a lot of information available on how to "parse" serial packets. You might start start with the forum search function in the upper right of this page.

Robin2’s serial input basics should get you started with the receive code.

You can make up a char array loaded with the right values to simulate a master transmission.

At this page there's a list of function libraries for strings, etc., you'll probably need some of those.

Do you have two arduino’s to test with?

Sounds to me like you’re supposed to use pins other than the built in RX,TX on pins 0 and 1, because you mention software serial.

Read about that here:
arduino.cc - softwareSerial

How much coding experience do you have? Usually if I’m having trouble figuring something out, I just look at the examples in the Arduino IDE. Theres quite a few in there with serial communication.

Because it’s an assignment I don’t think I should just write the code for you but I will definitely help out.

The flow chart actually makes this really easy. K is the index of an array called Data. “Data” is an array.
You can find out about how to use arrays in the examples. They’re very well documented.
Data is a an array that holds the data received from the master and acts as a buffer. Data is lost once read so you have to save it somewhere. You just need to make the size of it as large as one data transmission. (should be the number of bytes sent from master if i’m reading it correctly)

Here’s something to get you started.
I didn’t test it because it’s not complete but its a starting point.

// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 10 // Define RX pin
#define txPin 11 //  ^^    TX pin

int k = 0;

byte data[14];


// set up a new serial port
SoftwareSerial softSerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  softSerial.begin(9600);
}

void loop() {
  if (softSerial.available()>0){
    data[k] = Serial.read();
    k++;
  }
  if(k > 14){
    k = 0;
  }
}

This will save the 14 bytes read from the serial lines into the array called data. After that it’s up to you how you handle the Checksum. For that, just google “Arduino bitwise operators”.

Let me know how it goes.

The description and images don't appear to match. You describe the data packet as $, two bytes source address, two bytes destination address, 4 bytes data, two bytes checksum, terminated with #. The flowchart shows the destination address as bytes 3 & 4, with the checksum at byte 8, which leaves only bytes 5, 6, and 7 for the 4 bytes of data, clearly a contradiction. The checksum in the flowchart also appears to be a single byte, not two bytes.

We have to make a reply to this if it is addressed to us which includes a SOT '$' the source (so since its coming from me it'll be 24) the destination (master as we can only talk to the master 01) include a value for Pressure (4 bytes floating from Analog A0), a value for Temperature (3 bytes floating from Analog A1), a value for a Number (2 bytes floating from A2) and use a checksum from all this and then finish it with a '#' to signal the EOT.

How are you suppose to reduce the input data from A1 to 3 bytes, and A2 to 2 bytes? Analog inputs always return an integer from 0 through 1023, which will work fine as a 4-byte value with leading 0's, but cannot be used directly for the shorter numbers.

To me, the flowchart represents the detail of the processing of the incoming packet including the checksum calculation. The payload is 4 bytes ? (although the description is more vague saying “up to” 4 bytes), excluding address and checksum data. Interestingly, the checksum calculation uses a logical AND (&&) instead of an arithmetic AND (&).
You have to create a similar flowchart for the return packet, effectively expanding the box “compile reply and transmit”. The return packet has a different structure to the received packet.
Having done that, you have to convert it all to C++. Quite an interesting project in simple networking and handling C++ data structures.
I guess I’d create a C++ struct to represent the outgoing packet.
Also, always consider that when you are posting Arduino based school projects that teacher may be looking in here from time to time.

bears0:
Do you have two arduino’s to test with?

Sounds to me like you’re supposed to use pins other than the built in RX,TX on pins 0 and 1, because you mention software serial.

Read about that here:
arduino.cc - softwareSerial

How much coding experience do you have? Usually if I’m having trouble figuring something out, I just look at the examples in the Arduino IDE. Theres quite a few in there with serial communication.

Because it’s an assignment I don’t think I should just write the code for you but I will definitely help out.

The flow chart actually makes this really easy. K is the index of an array called Data. “Data” is an array.
You can find out about how to use arrays in the examples. They’re very well documented.
Data is a an array that holds the data received from the master and acts as a buffer. Data is lost once read so you have to save it somewhere. You just need to make the size of it as large as one data transmission. (should be the number of bytes sent from master if i’m reading it correctly)

Here’s something to get you started.
I didn’t test it because it’s not complete but its a starting point.

// include the SoftwareSerial library so you can use its functions:

#include <SoftwareSerial.h>

#define rxPin 10 // Define RX pin
#define txPin 11 //  ^^    TX pin

int k = 0;

byte data[14];

// set up a new serial port
SoftwareSerial softSerial =  SoftwareSerial(rxPin, txPin);

void setup()  {
  // define pin modes for tx, rx:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  // set the data rate for the SoftwareSerial port
  softSerial.begin(9600);
}

void loop() {
  if (softSerial.available()>0){
    data[k] = Serial.read();
    k++;
  }
  if(k > 14){
    k = 0;
  }
}




This will save the 14 bytes read from the serial lines into the array called data. After that it's up to you how you handle the Checksum. For that, just google "Arduino bitwise operators".

Let me know how it goes.

We currently only have one arduino to test with so we have to loop the Rx and Tx Pins together with A wire and resistor to get anything to display on the Serial Monitor

david_2018:
The description and images don't appear to match. You describe the data packet as $, two bytes source address, two bytes destination address, 4 bytes data, two bytes checksum, terminated with #. The flowchart shows the destination address as bytes 3 & 4, with the checksum at byte 8, which leaves only bytes 5, 6, and 7 for the 4 bytes of data, clearly a contradiction. The checksum in the flowchart also appears to be a single byte, not two bytes.
How are you suppose to reduce the input data from A1 to 3 bytes, and A2 to 2 bytes? Analog inputs always return an integer from 0 through 1023, which will work fine as a 4-byte value with leading 0's, but cannot be used directly for the shorter numbers.

I pretty sure to get the bytes down from 4 to 3 and 2 respectively, we have to have use bitWrite and get it to displayed a LSB to get it down, does that make sense?

seandebarra:
I pretty sure to get the bytes down from 4 to 3 and 2 respectively, we have to have use bitWrite and get it to displayed a LSB to get it down, does that make sense?

No, not really. From the description it sounds like you are sending the analog port data as the ASCII representation of a decimal number. That works fine with 4 ASCII characters, from 0000 to 1023, but 3 ASCII characters cannot go over 999, and 2 cannot go over 99. Unless the data is just for test purposes and serves no useful purpose, the instructor needs to give sufficient information on how to get from the analog input value to the desired data value being sent. (It is also possible that the data is being sent as an ASCII representation of a hexadecimal number, but that gets into the same problem).

The basic code for what you need to do is fairly simple, but the lack of precise details makes it impossible to produce the actual code. The webpage on serial input basics from reply #4 has code that will get you as far as receiving the packet of data from the serial input. Parsing the received data is fairly straightforward, except for the inconsistencies between your description of the data packet and the flowchart. Sending the data back is also fairly simple, once you have the data you want to send, a couple of hints on that part - a print statement in the code executes much, much faster than the actual time it takes to send the data over the serial line (the print function has a 64-byte buffer, as long as it is not full the print function returns quickly), and get in the habit of avoiding any use of String type variables, those can get you into trouble with the limited memory in an arduino.

david_2018:
Unless the data is just for test purposes and serves no useful purpose, the instructor needs to give sufficient information on how to get from the analog input value to the desired data value being sent.

Pretty sure we weren't given a way to to bring the data down from 4 bytes to 3 bytes and 2 bytes, just told to do it.

seandebarra:
Pretty sure we weren't given a way to to bring the data down from 4 bytes to 3 bytes and 2 bytes, just told to do it.

For a Uno, 2 bytes (16bits) is quite sufficient for the output of the ADC. The maximum decimal value is 1024 which is only 10bits wide. Simply assigning the value you get from the Analog pin to an an unsigned int (uint16_t) is enough. Of course, for transmission, you will have to find way of sending the 2 bytes of the unsigned integer. A struct is one possible solution (with either a union over the struct or some tricks with pointers).

6v6gt:
For a Uno, 2 bytes (16bits) is quite sufficient for the output of the ADC. The maximum decimal value is 1024 which is only 10bits wide. Simply assigning the value you get from the Analog pin to an an unsigned int (uint16_t) is enough. Of course, for transmission, you will have to find way of sending the 2 bytes of the unsigned integer. A struct is one possible solution (with either a union over the struct or some tricks with pointers).

Of course the binary data could be sent as two bytes, but that leads to confusion as to why the other two data values need 4 and 3 bytes respectively, since they could all be sent at 2-byte binary values.