Arduino Forum

Using Arduino => Programming Questions => Topic started by: HelloArduinoWorld on Jul 09, 2018, 06:18 pm

Title: Read two messages with different timings
Post by: HelloArduinoWorld on Jul 09, 2018, 06:18 pm
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?
Title: Re: Read two messages with different timings
Post by: Delta_G on Jul 09, 2018, 06:31 pm
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. 
Title: Re: Read two messages with different timings
Post by: HelloArduinoWorld on Jul 09, 2018, 07:44 pm
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);
Title: Re: Read two messages with different timings
Post by: PaulS on Jul 09, 2018, 10:02 pm
I see nothing in your code that cares about timing. Why don't I?
Title: Re: Read two messages with different timings
Post by: HelloArduinoWorld on Jul 09, 2018, 11:37 pm
if i knew how to. i wouldn't be posting this question.
Title: Re: Read two messages with different timings
Post by: PaulS on Jul 09, 2018, 11:39 pm
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?
Title: Re: Read two messages with different timings
Post by: sherzaad on Jul 10, 2018, 07:54 am
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"?
Title: Re: Read two messages with different timings
Post by: Robin2 on Jul 10, 2018, 08:49 am
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
Title: Re: Read two messages with different timings
Post by: HelloArduinoWorld on Jul 10, 2018, 10:54 pm
Thank you all. Solved it
Title: Re: Read two messages with different timings
Post by: Robin2 on Jul 11, 2018, 09:47 am
Thank you all. Solved it
It is usual to share your solution for the benefit of other readers.

...R
Title: Re: Read two messages with different timings
Post by: HelloArduinoWorld on Jul 11, 2018, 05:19 pm
The same concept you told me about in your reply. i just did that and it worked.
Title: Re: Read two messages with different timings
Post by: Robin2 on Jul 11, 2018, 07:36 pm
Thanks for the feedback. I had imagined from your Reply #8 that you had found a very different solution.

...R