Pages: [1]   Go Down
Author Topic: problem regarding serial print  (Read 3479 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 39
Well done is better than well said." --Benjamin Franklin
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys I should not start this new thread but problem is different
In my previous post i asked about reading data at 10 khz and with everybody help(especially Mr. Graynomad hats off to you!) we are able to solve it
But in the code we are using state machine to check the header("CAAC1F") but if i put any serial print inside those state it does not respond it keep on stuck on first state and also when we put some special character the behavior of the port changes(some time the port transmits and some times it does not arbitrarily )  
Code:
const int CLOCK_PIN     =     2;
const int DATA_PIN      =     4;
const int PACKET_BITS   =     1024;
const int PREAMBLE_LEN  =     6;
const int DATA_LEN      =     ((PACKET_BITS / 4) - PREAMBLE_LEN);

enum {
    PRE_0,    PRE_1,    PRE_2,    PRE_3,    PRE_4,    PRE_5,    DATA
} states;    

byte     preamble [] = {0x0C,0x0A,0x0A,0x0C,0x01,0x0F};
char     hexAsciiVals[] = "0123456789ABCDEF";
byte     state = PRE_0;
byte     nibble;
int      nibble_count = 0;
byte     firstval;
//const char  header = "CAAC1F";

void setup() {
  pinMode(CLOCK_PIN, INPUT);
  pinMode(DATA_PIN, INPUT);
  Serial.begin(57600);
}

void loop() {

    nibble = getNibble();
    //nibble_count++;
    
    
    switch (state) {    
        case PRE_0:
//            if (nibble_count > PREAMBLE_LEN + DATA_LEN) {
//                // we've read 32 bytes and still not found a match
//                // for the preamble so skip a clock pulse
//                while (digitalRead(CLOCK_PIN) == LOW);
//                while (digitalRead(CLOCK_PIN) == HIGH);
//                nibble_count = 0;
//            } else
//            {
//                firstval = 12;
//                Serial.write(hexAsciiVals[firstval]);
                
                state = (nibble == preamble[0]) ? PRE_1 : PRE_0;
              //  Serial.write(hexAsciiVals[nibble]);
//            }
            break;
        
        case PRE_1:
            
           // Serial.write (hexAsciiVals[firstval]);
           // Serial.print(hexAsciiVals[12]);
           // Serial.print(hexAsciiVals[nibble]);
            state = (nibble == preamble[1]) ? PRE_2 : PRE_0;    
            //Serial.write (hexAsciiVals[nibble]);
            break;
            
        case PRE_2:
          //  Serial.print(hexAsciiVals[nibble]);
            state = (nibble == preamble[2]) ? PRE_3 : PRE_0;
            //Serial.write (hexAsciiVals[nibble]);
            break;

        case PRE_3:
          //  Serial.print(hexAsciiVals[nibble]);
            state = (nibble == preamble[3]) ? PRE_4 : PRE_0;
        //    Serial.write (hexAsciiVals[nibble]);
            break;

        case PRE_4:
           // Serial.print(hexAsciiVals[nibble]);
            state = (nibble == preamble[4]) ? PRE_5 : PRE_0;
           // Serial.write (hexAsciiVals[nibble]);
            break;
        
        case PRE_5:
           // Serial.print(hexAsciiVals[nibble]);
    //        Serial.print(202, HEX);
  //          Serial.print(172, HEX);
        //    Serial.println();
            state = (nibble == preamble[5]) ? DATA : PRE_0;
            Serial.print("$");
            //   nibble_count = 0;
            break;
            
        case DATA:
           //   Serial.print("CAAC1F");
            Serial.write(hexAsciiVals[nibble]);
            if (nibble_count == DATA_LEN) {
                // all done, start again
                state = PRE_0;
                nibble_count = 0;
             //   Serial.print("\n");
           //     Serial.print(hexAsciiVals[12]);
                
            }
            
            nibble_count++;
            //Serial.println();
            break;
    }
    
  //  delay(10);
}

byte getNibble() {

    byte val = 0;
    
    for (byte bit_count = 0; bit_count < 4; bit_count++) {
        while (digitalRead(CLOCK_PIN) == HIGH);
        while (digitalRead(CLOCK_PIN) == LOW);
        val <<= 1;
        val |= digitalRead(DATA_PIN);
    }
    return (val &= 0x0F);
}
Any idea??? smiley-confuse
Logged

Thanx & Regards

Niladri

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Could it be that the time taken to do these additional steps takes you out of sync with your data stream?  Perhaps some status LEDs would give you some debugging information without detracting too much from the speed of your routine?  The only issue of course is at that speed you're not going to see too much, but you might turn it on and off as you successfully interpret each preamble string?

Geoff
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Offline Offline
Newbie
*
Karma: 0
Posts: 39
Well done is better than well said." --Benjamin Franklin
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK i will try that but what the port response?
Still i need to program it every time to make it transmit just powering it up does not make it transmit(Tx LED is not glowing) smiley-confuse
Logged

Thanx & Regards

Niladri

Pages: [1]   Go Up
Jump to: