I Need a help in this code
I am trying to assign the serial.write(OutData[i]) to byte data[]. Can anyone help me assign it.
Here is my code
#define ECHOPIN 11// Pin to receive echo pulse
#define TRIGPIN 12// Pin to send trigger pulse
int distance=0;
byte OutData[] = {0x05, 0x03, 0x04, 0x43, 0x65, 0xBA, 0x7D, 0x04, 0xE8};
#include <Crc16.h>
Crc16 crc;
void setup()\
{
Serial.begin(9600);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
digitalWrite(ECHOPIN, HIGH);
}
void loop() {
digitalWrite(TRIGPIN, LOW); // Set the trigger pin to low for 2uS
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH); // Send a 10uS high to trigger ranging
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW); // Send pin low again
distance = pulseIn(ECHOPIN, HIGH,26000); // Read in times pulse
distance= distance/58;
Serial.print(distance, HEX);
OutData[3] = 0x00;
OutData[4] = 0x00;
OutData[5] = 0x41;
OutData[6] = distance;
for (int i = 0; i <= sizeof(OutData); i++) {
Serial.write(OutData[i]);
byte data[] = OutData[i];
crc.clearCrc();
for(byte i=0;i<9;i++)
{
Serial.print("byte ");
Serial.print(i);
Serial.print(" = ");
Serial.println(data[i]);
crc.updateCrc(data[i]);
}
unsigned short value = crc.getCrc();
Serial.print("crc = 0x");
Serial.println(value, HEX);
Serial.println("The crc Check of the byte array");
//Modbus
value = crc.Modbus(data,0,9);
Serial.print("Modbus crc = 0x");
Serial.println(value, HEX);
while(true);
}
int calcrc(char *ptr, int count)
{
int crc;
char i;
crc = 0;
while (--count >= 0)
{
crc = crc ^ (int) *ptr++ << 8;
i = 8;
do
{
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
} while(--i);
}
return (crc);
}
My Error is :
C:\Users\system3\AppData\Local\Temp\arduino_modified_sketch_307616\sketch_dec13a.ino: In function 'void loop()':
sketch_dec13a:34:25: error: initializer fails to determine size of 'data'
byte data[] = sizeof(OutData);
^
sketch_dec13a:34:25: error: array must be initialized with a brace-enclosed initializer
sketch_dec13a:60:1: error: a function-definition is not allowed here before '{' token
{
^
sketch_dec13a:77:1: error: expected '}' at end of input
}
^
Multiple libraries were found for "Crc16.h"
Used: C:\Users\system3\Documents\Arduino\libraries\Crc16-master
exit status 1
initializer fails to determine size of 'data'
Serial.read()call in your code (and this method does not have a parameter, I think). Also you should include the complete error message, including information about the problematic line.