What is wrong with this it doesn't work please help

The point here is that I have to skip 1st, 2nd, 3rd byte that I receive and only use 4th and 5th byte and combine them to get the sensor value. The values are received after I transmit 0x11, 0x01, 0x01, 0xED to the sensor. I want to make serial monitor to display the combined value every second. please look at the code
and it does not work properly

#include <Servo.h>
#include <SoftwareSerial.h>

int interval=20;

byte Pattern={0x11, 0x01, 0x01, 0xED};
Servo first;
Servo second;

int index;
int angle=0;
int state=0;
SoftwareSerial mySerial(10,11);
unsigned Temp;
unsigned long Value;
unsigned Data={};

void setup() {
Serial.begin(9600);
while(!Serial){
};
mySerial.begin(9600);
first.attach(2);

}
void Servo_Control(int index){
if (index==0){
for (angle=0; angle<=180; angle++){
first.write(angle);
delay (15);
}
}
else {
for (angle=180; angle>=0; angle–){
first.write(angle);
delay (15);
}
}}

void loop() {
// put your main code here, to run repeatedly:
index=0;
state=0;
Servo_Control(0);

for (int i=0; i<4;i++){
mySerial.write(Pattern*);}*
while(mySerial.available()){

  • Data[state]=mySerial.read();*
  • switch (state)*
  • {*
  • case 0:*
  • if (Data[0]==0x16){state++;}*
  • else {state=0;}*
  • break;*
  • case 1:*
  • if(Data[1]==0x05){state++;}*
  • else {state=0;}*
  • break;*
  • case 2:*
  • if(Data[2]==0x01){state++;}*
  • else {state=0;}*
  • break;*
  • case 3:*
  • state++; break;*
  • case 4:*
  • state++;break;*
  • case 5:*
  • state++;break;*
  • case 6:*
  • state++;break;*
  • case 7:*
  • state=0;*
  • Value=Data[3]<<8|Data[4];*
  • Serial.print(millis()/1000); Serial.print(" ");*
  • Serial.println(Value, DEC); index++; break;*
  • default:*
  • state=0;*
  • break;*
  • }*
  • if(index==400)*
  • break;*

}
Servo_Control(1);
delay(10000);
}

You have previously been asked to post your code in code tags. Why are you not doing it?
Please edit your opening post and correct it by doing so.

And there are many errors, some pretty stupid.
Don’t you read your code as you write it?

default  // No colon here

break  // no semicolon here

Value=Data[3]<<8|Data[4];  // You did not declare 'Data' as an array.
///////// 'if' statements with no opening bracket, no semicolon after each 'state++'
  if Data[0]==0x16){state++}
    else {state=0;}
    break;
    case 1:
    if Data[1]==0x01){state++}
    else {state=0;}
    break;
    case 2:
    if Data[2]==0x01){state++}
    else {state=0;}
/////////

Also:-

  • You have a break that’s not within a loop or switch

and so on, and so on …

Edit: And you’re cross-posting:- I have coding error and I want to know what is wrong

Don't just edit the code that you posted in your opening post as suggestions are made. That makes comments in the replies look stupid.

I see you've now re-written your whole opening post.
Edit: And I see that every few minutes you go back and edit it again. Just leave it and post any changes in a reply.

And if you're going to edit at all, add those bloody code tags as asked.

[code]code tags look like this[/code]

OldSteve:
I see you've now re-written your whole opening post.
Edit: And I see that every few minutes you go back and edit it again. Just leave it and post any changes in a reply.

On that basis I am not going to bother with this Thread.

...R

Robin2:
On that basis I am not going to bother with this Thread.

...R

. . . and for multiple cross-posts and thread vandalism, neither is the OP for a couple of days.

unsigned Data[]={};

Bad Things happen when you write to this.

oqibidipo:

unsigned Data[]={};

Bad Things happen when you write to this.

I spotted that. It's a beauty, isn't it. :smiley: