1

I am trying to write one code that takes data from serial ie. 0x4E, 0x20, 0x--, 0x0D.

0x-- may be any hex in --

The user will input these hex one by one and raspi will take all of them in buffer. Whenever 1st, 2nd and 4th data is matched, the 3rd one will be printed in serial ie.

Serial.print(3rd); //Decimal value
else Serial.print() // may be 00

I was trying with this code:

byte buffer[4];
int cnt = 0;

void setup()
{
 Serial.begin(9600);
 Serial.println("Start");
}

void loop(void)
{

 // read max 12 bytes
 if ((cnt <= 4) && Serial.available()) buffer[cnt++] = Serial.read();

 if (cnt == 4)  // all bytes read...
 {
   // assume the arrays are the same
   boolean same = true;

   // the ans array holds the right answer
   byte ans[]={0x4E,0x20,0x01,0x0D};

   for (int i=0; (i<= 4) && same; i++) same = (buffer[i] == ans[i]);

   if (same)
   {
     Serial.println("Yes");
   } else {
     Serial.println("Enter another number");
   }
   Serial.flush();
   cnt = 0;
 }
  //Serial.print("Done");
 // do something  
}

But I was not able to do it. Anyone have a suggestion?

Edit: I have tried this state machine in Arduino : It accepts those data and according to it, it should bi color LED light. But not working properly. Not lighting up anything

//int currState;
//int reChk, data, confirm;
const int pingPin = 13;
int inPin = 12;

void setup()  
{
  Serial.begin(9600);
    // initialize digital pin 13 as an output.
  pinMode(32, OUTPUT);
  pinMode(34, OUTPUT); 

  digitalWrite(32, LOW);   
  digitalWrite(34, LOW);
  //currState = 0;
  //reChk = 0;
  //confirm = -1;


}

void loop() // run over and over
{
  static int currState = 0, reChk = 0, confirm = -1;
  int data;
  if(reChk == 0)
  {
    //ultrasonic();
    if(Serial.available())
    data = Serial.read();
    else
    currState = 5;
  }

  switch(currState)
  {

    case 0:
    reChk = 0;
    if(data == 78)
      currState = 1;
    else
      currState = 0;
    break;


    case 1:
    reChk = 0;
    if(data == 32)
    currState = 2;
    else
    {
      currState = 0;
      reChk = 1;
    }
    break;


    case 2:
    reChk = 0;
    switch(confirm)
    {
      case -1 :
      confirm = data;
      currState = 3;
      break;
      case 0 :
            digitalWrite(32, LOW);   
            digitalWrite(34, LOW);
            delay(10); 
      confirm = -1;
      currState = 0;
      break;
      case 1 :
            digitalWrite(34, HIGH);   
            digitalWrite(32, LOW);
            delay(10);
      confirm = -1;
      currState = 0;
      break;
      case 2 :
            digitalWrite(32, HIGH);   
            digitalWrite(34, LOW);
            delay(10); 
      confirm = -1;
      currState = 0;
      break;
      default :
      confirm = -1;
      reChk = 1;
      currState = 0;
    }


    case 3:
    reChk = 0;
    if(data == 20)
    {
      reChk = 1;
      currState = 2;
    }
    else
    {
      confirm = -1;
      currState = 0;
    }
    break;

    default:
    reChk = 0;
    currState = 0;
    confirm = -1;
  }
  delay(10);
}

void ultrasonic () //ultrasonic
{
      const int pingPin = 13;
      int inPin = 12;
      //raw duration in milliseconds, cm is the
      //converted amount into a distance
      long duration, cm;

      //initializing the pin states
      pinMode(pingPin, OUTPUT);
      //pinMode(greenLed, OUTPUT);
      //pinMode(redLed, OUTPUT); 

      //sending the signal, starting with LOW for a clean signal
      digitalWrite(pingPin, LOW);
      delayMicroseconds(2);
      digitalWrite(pingPin, HIGH);
      delayMicroseconds(5);
      digitalWrite(pingPin, LOW);

      //setting up the input pin, and receiving the duration in
      //microseconds for the sound to bounce off the object infront
      pinMode(inPin, INPUT);
      duration = pulseIn(inPin, HIGH);

      // convert the time into a distance
      cm = microsecondsToCentimeters(duration);

      if(cm > 50)
      {
      //printing the current readings to ther serial display
      Serial.print("5");
      }
      else
      {
      Serial.print("0");
      }
      //Serial.print("cm");
      //Serial.println();

      //checking if anything is within the safezone, if not, keep
      //green LED on if safezone violated, activate red LED instead
      //if (cm > safeZone)
      //{
      //  digitalWrite(greenLed, HIGH);
      //  digitalWrite(redLed, LOW);
      //}
      //else
      //{
      //  digitalWrite(redLed, HIGH);
      //  digitalWrite(greenLed, LOW);
      //}

delay(1000);
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
   return microseconds / 29 / 2;
}

migrated from electronics.stackexchange.com Nov 17 '14 at 20:31

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • how are you typing in the values? – geometrikal Nov 17 '14 at 19:00
  • 4E then in next input others one by one. it may be in decimal values of those numbers too – ninja.stop Nov 17 '14 at 19:04
  • as @Kitana said, Serial.read() takes one byte from buffer. If you enter 4E then buffer[0] = 4, buffer[1] = E and so on. Arduino isn't mind reader :)) – Martynas Nov 18 '14 at 11:00
  • It's rather unclear that this should have been migrated: although the target is an Arduino, the issue here is entirely generic to any serial-connected system. – Chris Stratton Nov 18 '14 at 19:20
  • actually a bit serial but also related to arduino as well – ninja.stop Nov 19 '14 at 8:14
1

It's unclear how the users input the hex numbers.

If i assume a simple serially attached terminal the user doesn't enter hex numbers but characters.

As an example when the user types 0, x, 4, E the user has only typed one hex number but already four bytes or characters. Instead the user has to type N, , whatever letter, Enter. (Depending on the OS or terminal you will get "CR", "LF" or "CR+LF" when you press enter)

I'm not familiar with C, but your code seems to have also a coding flaw: The 3rd byte will be checked for equality too. You would need something like:

boolean same = true;

if (buffer[0] == ans[0]) { same = false }
if (buffer[1] == ans[1]) { same = false }
if (buffer[3] == ans[3]) { same = false }

Only three tests not four.

  • say user gives 78320113 78 = 4E in hex and so on 32 = 20 01 = 01 13 = 0D one buffer will take this as input. it will check 1st 2 and last the 4th.if match found then Serial.println(the 3rd element ie. theresult will be 01 at serial monitor) Hopefully I am clear – ninja.stop Nov 17 '14 at 19:29
  • The users still enters eight characters and not four. Either you take my proposal and convert the hex codes to ASCII characters or you read in eight bytes and compare six of them with their ASCII equivalent. However i'm not shure if we're still on topic for this site. – Kitana Nov 17 '14 at 19:47
  • also break could help stop checking if it was != – Martynas Nov 18 '14 at 10:57
  • And e.g. FF in decimal is 255 so that is three characters – geometrikal Nov 19 '14 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.