Receiving Code

Back to Home

Back to Code Description

#include <Servo.h>

Servo hitech;
Servo traxx;
Servo trigger;

char HITECH = 2; 
char TRAXX = 2;
int x_pos = 90;
int y_pos = 90;
int trig_pos = 90;

int t_state = 0;
int trig_count = 0;
int trig_count_value = -1;

int trig_LED = 8;
char trig_val = '0';

int incomingByte;      // a variable to read incoming serial data into

// FSM
int state;
const int WAIT = 0;
const int X_REC = 1;
const int Y_REC = 2;
const int T_REC= 3;


void setup() {
  // initialize serial communication:
  Serial.begin(9600);

  hitech.attach (9);
  //  traxx.attach (10);
  trigger.attach (11);
  pinMode (trig_LED, OUTPUT);
}

void loop() {

  // Random use of string compares to turn on/off LEDs
  // PWM can be used for better results
  hitech.write(x_pos);
  traxx.write(y_pos);
  trigger.write(trig_pos);

  switch (t_state)
  {
  case 0:
    {
      if (trig_val == '1')
        t_state = 1;
    }
    break;
  case 1:
    {
      trig_pos = 130;
      trig_count ++;
      if (trig_count > 25)
        t_state  = 2;
    }
    break;
  case 2:
    {
      trig_pos = 90;
      trig_count = trig_count -1;
      if (trig_count <= 0)
      {
        if (trig_val == '1')
          t_state = 1;
        else
          t_state = 0;
      }
    }
    break;
  default:
    {
      t_state = 0;
    }
  }
  if (trig_val == '1')
  {
    digitalWrite(trig_LED, HIGH);
  }
  else
  {
    digitalWrite(trig_LED, LOW);
  }

  switch (HITECH){
  case '0':
    {
      if (x_pos > 5)
        x_pos -= 5;
    }
    break;
  case '1':
    {
      if (x_pos > 0)
        x_pos --;
    }
    break; 
  case '2':
    {
      x_pos = x_pos;
    }
    break;  
  case '3':
    {
      if (x_pos < 178)
        x_pos ++;
    }
    break;
  case '4':
    {
      if (x_pos < 174)
        x_pos += 5;
    }
    break;
  default:
    x_pos = 90;
    break;

  }

  switch (TRAXX){
  case '0':
    {
      if (y_pos > 80)
        y_pos -= 5;
    }
    break;
  case '1':
    {
      if (y_pos > 75)
        y_pos --;
    }
    break; 
  case '2':
    {
      y_pos = y_pos;
    }
    break;  
  case '3':
    {
      if (y_pos < 115)
        y_pos ++;
    }
    break;
  case '4':
    {
      if (y_pos < 110)
        y_pos += 5;
    }
    break;
  default:
    y_pos = 90;
    break;

  }

  switch (state) {
  case WAIT:
    {
      if (Serial.available () > 0)
      {
        incomingByte = Serial.read ();

        // If the sender is transmitting 'X', move to X_REC
        // and send confirmation
        if (incomingByte == 'X')
        {
          state = X_REC;
          Serial.print('X');
        }

        // If the sender is transmitting 'Y', move to Y_REC
        // and send confirmation
        else if (incomingByte == 'Y')
        {
          state = Y_REC;  
          Serial.print('Y');
        }

        else if (incomingByte == 'T')
        {
          state = T_REC;
          Serial.print('T');
        }
        // Lost sync, tell sender to revert to start
        else
          Serial.print('R');
      }
    }
    break;

  case X_REC:
    {
      if (Serial.available () > 0)
      {
        incomingByte = Serial.read ();

        // Receive X-value from sender, send confirmation
        if (incomingByte >= '0' && incomingByte <= '4')
        {
          HITECH = incomingByte;
          Serial.print (HITECH);
          state = WAIT;
        } 

        // Lost sync, revert back to WAIT
        else if (incomingByte != 'X')
        {
          Serial.print ('R');
          state= WAIT;
        }      

        // Sender is still sending 'X', reconfirm to move it ahead      
        else
          Serial.print('X');
      }
    }
    break;

  case Y_REC:
    {
      if (Serial.available () > 0)
      {
        incomingByte = Serial.read ();

        // Receive X-value from sender, send confirmation      
        if (incomingByte >= '0' && incomingByte <= '4')
        {
          TRAXX = incomingByte;
          Serial.print (TRAXX);
          state = WAIT;
        }

        // Lost sync, revert to WAIT
        else if (incomingByte != 'Y')
        {
          Serial.print ('R');
          state= WAIT;
        }   

        // Sender is still sending 'Y', reconfirm to move it ahead  
        else
          Serial.print('Y'); 
      }
    }
    break;
  case T_REC:
    {
      if(Serial.available () > 0 )
      {
        incomingByte = Serial.read ();

        if (incomingByte >= '0' && incomingByte <= '4')
        {
          trig_val = incomingByte;
          Serial.print (trig_val);
          state = WAIT;
        }
        else if (incomingByte == 'T')
        {
          Serial.print('T');
        }
        else
        {
          Serial.print('R');
          state = WAIT;
        }
      }
    }
    break;
  default:
    state = WAIT;
    break;
  }
  delay (10);
}

Back to Top

#include <Servo.h>

Servo hitech;
Servo traxx;
Servo trigger;

char HITECH = 2;  
char TRAXX = 2;
int x_pos = 90;
int y_pos = 90;
int trig_pos = 90;

int t_state = 0;
int trig_count = 0;
int trig_count_value = -1;

int trig_LED = 8;
char trig_val = '0';

int incomingByte;      // a variable to read incoming serial data into

// FSM
int state;
const int WAIT = 0;
const int X_REC = 1;
const int Y_REC = 2;
const int T_REC= 3;


void setup() {
  // initialize serial communication:
  Serial.begin(9600);

  hitech.attach (9);
  //  traxx.attach (10);
  trigger.attach (11);
  pinMode (trig_LED, OUTPUT);
}

void loop() {

  // Random use of string compares to turn on/off LEDs
  // PWM can be used for better results
  hitech.write(x_pos);
  traxx.write(y_pos);
  trigger.write(trig_pos);

  switch (t_state)
  {
  case 0:
    {
      if (trig_val == '1')
        t_state = 1;
    }
    break;
  case 1:
    {
      trig_pos = 130;
      trig_count ++;
      if (trig_count > 25)
        t_state  = 2;
    }
    break;
  case 2:
    {
      trig_pos = 90;
      trig_count = trig_count -1;
      if (trig_count <= 0)
      {
        if (trig_val == '1')
          t_state = 1;
        else
          t_state = 0;
      }
    }
    break;
  default:
    {
      t_state = 0;
    }
  }
  if (trig_val == '1')
  {
    digitalWrite(trig_LED, HIGH);
  }
  else
  {
    digitalWrite(trig_LED, LOW);
  }

  switch (HITECH){
  case '0':
    {
      if (x_pos > 5)
        x_pos -= 5;
    }
    break;
  case '1':
    {
      if (x_pos > 0)
        x_pos --;
    }
    break;  
  case '2':
    {
      x_pos = x_pos;
    }
    break;   
  case '3':
    {
      if (x_pos < 178)
        x_pos ++;
    }
    break;
  case '4':
    {
      if (x_pos < 174)
        x_pos += 5;
    }
    break;
  default:
    x_pos = 90;
    break;

  }

  switch (TRAXX){
  case '0':
    {
      if (y_pos > 80)
        y_pos -= 5;
    }
    break;
  case '1':
    {
      if (y_pos > 75)
        y_pos --;
    }
    break;  
  case '2':
    {
      y_pos = y_pos;
    }
    break;   
  case '3':
    {
      if (y_pos < 115)
        y_pos ++;
    }
    break;
  case '4':
    {
      if (y_pos < 110)
        y_pos += 5;
    }
    break;
  default:
    y_pos = 90;
    break;

  }

  switch (state) {
  case WAIT:
    {
      if (Serial.available () > 0)
      {
        incomingByte = Serial.read ();

        // If the sender is transmitting 'X', move to X_REC
        // and send confirmation
        if (incomingByte == 'X')
        {
          state = X_REC;
          Serial.print('X');
        }

        // If the sender is transmitting 'Y', move to Y_REC
        // and send confirmation
        else if (incomingByte == 'Y')
        {
          state = Y_REC;   
          Serial.print('Y');
        }

        else if (incomingByte == 'T')
        {
          state = T_REC;
          Serial.print('T');
        }
        // Lost sync, tell sender to revert to start
        else
          Serial.print('R');
      }
    }
    break;

  case X_REC:
    {
      if (Serial.available () > 0)
      {
        incomingByte = Serial.read ();

        // Receive X-value from sender, send confirmation
        if (incomingByte >= '0' && incomingByte <= '4')
        {
          HITECH = incomingByte;
          Serial.print (HITECH);
          state = WAIT;
        }  

        // Lost sync, revert back to WAIT
        else if (incomingByte != 'X')
        {
          Serial.print ('R');
          state= WAIT;
        }       

        // Sender is still sending 'X', reconfirm to move it ahead       
        else
          Serial.print('X');
      }
    }
    break;

  case Y_REC:
    {
      if (Serial.available () > 0)
      {
        incomingByte = Serial.read ();

        // Receive X-value from sender, send confirmation       
        if (incomingByte >= '0' && incomingByte <= '4')
        {
          TRAXX = incomingByte;
          Serial.print (TRAXX);
          state = WAIT;
        }

        // Lost sync, revert to WAIT
        else if (incomingByte != 'Y')
        {
          Serial.print ('R');
          state= WAIT;
        }    

        // Sender is still sending 'Y', reconfirm to move it ahead   
        else
          Serial.print('Y');  
      }
    }
    break;
  case T_REC:
    {
      if(Serial.available () > 0 )
      {
        incomingByte = Serial.read ();

        if (incomingByte >= '0' && incomingByte <= '4')
        {
          trig_val = incomingByte;
          Serial.print (trig_val);
          state = WAIT;
        }
        else if (incomingByte == 'T')
        {
          Serial.print('T');
        }
        else
        {
          Serial.print('R');
          state = WAIT;
        }
      }
    }
    break;
  default:
    state = WAIT;
    break;
  }
  delay (10);
}