Sending Code

Back to Home

Back to Code Description


// Pins used for ADXL
const int groundpin = 18;             // analog input pin 4 -- ground
const int powerpin = 19;              // analog input pin 5 -- voltage
const int xpin = A3;                  // x-axis of the accelerometer
const int ypin = A2;                  // y-axis
const int zpin = A1;                  // z-axis (only on 3-axis models)

//Pins used for trigger and arm push-button and arm led
const int trigger = 8;
const int ARM_READ = 9;
const int ARM_LED = 10;

char ARM = '0';
int readValue;                        // Integer used to read values from the analog pins

// Constant parameters for the FSM
const int WAIT = 7;
const int CALC = 0;
const int SEND_T = 1;
const int SEND_TV = 2;
const int SEND_X = 3;
const int SEND_AX = 4;
const int SEND_Y = 5;
const int SEND_AY = 6;

// Character variables to store the characters transmitted via ASCII code
// on the Serial Monitor and the XBees
char READ;
char sent;

char state;

char X_send = '2';
char Y_send = '2';
char X_last = '2';
char Y_last = '2';
char trig_val = '0';
char trig_last = '0';


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

  // Provide ground and power by using the analog inputs as normal
  // digital pins.  
  pinMode(groundpin, OUTPUT);
  pinMode(powerpin, OUTPUT);
  pinMode(trigger, INPUT);
  digitalWrite(groundpin, LOW);
  digitalWrite(powerpin, HIGH);

  pinMode(ARM_READ, INPUT);
  pinMode(ARM_LED, OUTPUT);
  // Starting the FSM in CALC
  state = WAIT;
}

void loop()
{
  if (digitalRead (ARM_READ) == HIGH)
    ARM = '1';
  else
    ARM = '0';

  if (digitalRead (trigger) == HIGH)
    trig_val = '1';

  else
    trig_val = '0';


  // Read the values from the x-axis
  // Assign the magnitude to a value 0-4
  readValue = analogRead (xpin);      
  if (readValue < 460)
    X_send = '0';
  else if (readValue < 495)
    X_send =('1');
  else if (readValue < 512)
    X_send = ('2');
  else if (readValue < 560)
    X_send = ('3');
  else
    X_send = ('4');

  // Read the values from the y-axis
  // Assign the magnitude to a value 0-4    
  readValue = analogRead (ypin);     
  if (readValue < 460)
    Y_send = '0';
  else if (readValue < 500)
    Y_send =('1');
  else if (readValue < 530)
    Y_send = ('2');
  else if (readValue < 580)
    Y_send = ('3');
  else
    Y_send = ('4');

  if (ARM != '1')
    state = WAIT;  

  if (state != WAIT)
    digitalWrite (ARM_LED, HIGH);
  else
    digitalWrite (ARM_LED, LOW);

  // Begin FSM
  switch (state) {

  case WAIT:
    {
      if (ARM == '1')
        state = CALC;
      else
        state = WAIT;
      Serial.print("Waiting\n");
    }
    break;

  case CALC:
    {
      // If the values that would be sent are different than what was
      // sent last time, move to that state, beginning with X-direction
      if (trig_val != trig_last)
        state = SEND_T;
      else if (X_send != X_last)
        state = SEND_X;
      else if (Y_send != Y_last)
        state = SEND_Y;
      else
        state = CALC;
    }
    break;

  case SEND_T:
    {
      Serial.print('T');
      READ = Serial.read();
      if (READ == 'T')
        state = SEND_TV;
    }
    break;

  case SEND_TV:
    {
      Serial.print(trig_val);
      READ = Serial.read();

      // Confirmation received, can move to CALC, SEND_X or SEND_Y
      if (READ == trig_val)
      {
        trig_last = trig_val;
        if (X_send != X_last)
          state = SEND_X;
        else if (Y_send != Y_last)
          state = SEND_Y;
        else
          state = CALC;
      }

      else if (READ == 'R')
        state = CALC;
    }
    break;

  case SEND_X:
    {
      // Print 'X' and wait for the receiver to send back the confirmation
      Serial.print('X');
      READ = Serial.read();
      if (READ == 'X')
        state = SEND_AX;
    }
    break;

  case SEND_AX:
    {  
      // Send the value calculated above, wait for confirmation
      Serial.print(X_send);
      READ = Serial.read();

      // Confirmation received, can move to CALC or SEND_Y
      if (READ == X_send)
      {
        X_last = X_send;
        if (Y_send != Y_last)
          state = SEND_Y;
        else
          state = CALC;
      }

      // Sync between send & receive has been lost, revert to CALC
      else if (READ == 'R')
        state = CALC;
    }
    break;

  case SEND_Y:
    {
      // Send 'Y' and wait for confirmation
      Serial.print('Y');
      READ = Serial.read();
      if (READ == 'Y')
        state = SEND_AY;
    }
    break;

  case SEND_AY:
    {      
      // Send value for Y calculated above, wait
      // for confirmation
      Serial.print(Y_send);
      READ = Serial.read();

      // Confirmation successful, move to CALC
      if (READ == Y_send)
      {
        state = CALC;    
        Y_last = Y_send;
      }

      // Sync lost, revert to CALC
      else if (READ == 'R')
        state = CALC;
    }
    break;

    // default state should be CALC
  default:
    state = WAIT;
    break;
  }

  // Some delay might be required,
  // laptop freezes with overload when no delay is given
  delay (50);
}

Back to Top