Arduino Code

Back to Home

On this page, you will find links to two pages containing the codes used for both the sender, equipped with the accelerometer, and the receiver, controlling the servo motors.

Sending code

This code is used on the Arduino that has an accelerometer sending data in through the analog pins. The code analyzes the values along the X & Y axis and maps it certain values. The value mapping system equates the readings to values from 0 to 4.  What follows below is an example of the mapping system that we utilized for this project:

    // 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');

 

The Arduino’s analogRead returns values from 0 to 1023, with the accelerometer sending base values around 512.

The sender is also in control of the trigger, which is simply a pushbutton connected to a digital read pin at pin 8, as well as the arming mechanism, which was a dip-switch attached to another digital read pin, pin 9.

When the system is armed, the Arduino will send power to an LED connected to Pin 13. This is to help indicate to the user that the system is live, and the trigger is active.

The bulk of the code is taken up with a state machine that cycles through the wireless protocol that is used to relay the information from this Arduino board, dubber the sender, to the receiving Arduino. As the code breaks it down into comments, the sender analyzes the accelerometer, maps it the appropriate values and then determines if any change has taken place that needs to be sent to the receiver.

The protocol system that we used to guarantee the most accurate transfer of data was a system in which the receiver would duplicate whatever it received and send it back. In this system, the sender will not proceed to the next state in the FSM without receiving a duplicate of what it just transmitted. The following is a sample code of this system:

  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;

 

These two examples give a base understanding of how the sender analyzes data and sends it to the user. To see the entire code, please click here.

Receiving code

Similar to the sender, the bulk of the receiver’s code is comprised of the protocol for guaranteeing an accurate transfer of data. The receiver’s primary goal, however, is to take the data it receives from the sender and operate the servos accordingly. The different values, listed in the sender’s code section above (here), are taken in by the receiver and sent to the servos using the following code:

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;

  }

 

This code is working with the HiTech Servo, which is controlling the X-Axis. By using this system, the values are assigned  to:

 

 0 = Fast Speed to the Left

 1 = Slow Speed to the Left

 2 = Stationary

 3 = Slow Speed to the Right

 4 = Fast Speed to the Right

 

The variable x_pos is what is sent to the servo, so by decreasing/increasing the value, the servo will be moving left/right, respectively.

Regarding the protocol, the receiver uses very similar code as the sender. An example of this is below:

    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');
      }

These two examples give a broad understanding of what the receiver does. To see the full example of the code, click here.

Back to Home