Go Down

Topic: Read two messages with different timings (Read 386 times) previous topic - next topic

HelloArduinoWorld

Hello,
So i am trying to read two can messages with different timing and save and compare them without using delays. how do i do it?

Delta_G

#1
Jul 09, 2018, 06:31 pm Last Edit: Jul 09, 2018, 06:33 pm by Delta_G
Using the technique from the "Blink Without Delay" example.

If you want a more detailed answer you'll have to ask a much more detailed question.  You didn't really give me much to go on there. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

HelloArduinoWorld

#2
Jul 09, 2018, 07:44 pm Last Edit: Jul 09, 2018, 08:24 pm by HelloArduinoWorld
Here is my code. . i have two devices with the same can id but are on different can networks. my difference is either -voltage1 or -voltage2. please help out.

Code: [Select]
#include <mcp_can.h>
#include <SPI.h>
float Voltage1;
float Voltage2;


const int SPI_CS_PIN1 = 9; // select pin 9
const int SPI_CS_PIN2 = 10 ; // select pin 10


MCP_CAN CAN1 (SPI_CS_PIN1);
MCP_CAN CAN2 (SPI_CS_PIN2);                                     // Set CS pin

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

  if (CAN_OK != CAN1.begin(CAN_500KBPS) || CAN_OK != CAN2.begin(CAN_500KBPS) )              // init can bus : baudrate = 500k
  {
    Serial.println("CAN BUS Shield init fail");
    Serial.println(" Init CAN BUS Shield again");
    delay(100);
  }
  Serial.println("CAN BUS Shield init ok!");

}


void loop()
{

  unsigned char len1 = 0;//
  unsigned char buf1[8] = {00, 00, 00, 00, 00, 00, 00, 00};
  unsigned char len2 = 0;
  unsigned char buf2[8] = {00, 00, 00, 00, 00, 00, 00, 00};

 

  if (CAN_MSGAVAIL == CAN1.checkReceive())        // check if data coming
  {
    CAN1.readMsgBuf(&len1, buf1);
    unsigned int canId1 = CAN1.getCanId();

    if (canId1 == 768)
    {
      unsigned int x1 = buf1[2]; //Select bit
      unsigned int y1 = buf1[3];
      unsigned int z1 = x1 * 256 + y1; //concatenate
      Voltage1 = z1 * 0.002; //Convert obtained decimal value to voltage
       Serial.println(Voltage1);


   }
   
  }
 
 
  if (CAN_MSGAVAIL == CAN2.checkReceive())
  {
    CAN2.readMsgBuf(&len2, buf2);
    unsigned int canId2 = CAN2.getCanId();

    if (canId2 == 768)
    {
      unsigned int x2 = buf2[2]; //Select bit
      unsigned int y2 = buf2[3];
      unsigned int z2 = x2 * 256 + y2; //concatenate
      Voltage2 = z2 * 0.002; //Convert obtained decimal value to voltage
       Serial.println(Voltage2);


    }

  }
  }

  float difference = Voltage1 - Voltage2;
  Serial.println(difference);

PaulS

I see nothing in your code that cares about timing. Why don't I?
The art of getting good answers lies in asking good questions.

HelloArduinoWorld

if i knew how to. i wouldn't be posting this question.

PaulS

You are pulling messages. How can you know when to pull them? There will be nothing to pull, if a device doesn't have a message for you to pull. Why do you think that "as-fast-as-they-become-available" isn't the right way to read them?
The art of getting good answers lies in asking good questions.

sherzaad

Here is my code. . i have two devices with the same can id but are on different can networks. my difference is either -voltage1 or -voltage2. please help out.
your code is not waiting for BOTH "Voltage1" and "Voltage2" to be available... easy enough to fix

just to confirm: does the voltage difference be "Voltage1 - Voltage2" or can it also be "Voltage2 - Voltage1"?

Robin2

So i am trying to read two can messages with different timing and save and compare them without using delays. how do i do it?
You have not really described your requirement clearly and I suspect if you did you would see yourself how to do it.

I wonder (and I may be completely wrong) if you want to compare the most recent values from each input. You could do that by having a variable that records when a new value arrives for each input - something like this pseudo code
Code: [Select]
if a value arrives at inputA
    newInputA = true

if a value arrives at inputB
   newInputB = true

if (newInputA == true and newInputB == true)
   diff = valueA - valueB
   newInputA = false
   newInputB = false
}


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

HelloArduinoWorld


Robin2

Thank you all. Solved it
It is usual to share your solution for the benefit of other readers.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

HelloArduinoWorld

The same concept you told me about in your reply. i just did that and it worked.

Robin2

Thanks for the feedback. I had imagined from your Reply #8 that you had found a very different solution.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up