Basic Arduino Code

To use the Arduino as a ‘spinal cord’ it must have code to receive messages from the ‘brain’ and activate the muscles/motors.

What follows is very basic Arduino code that will allow you to drive a skidsteer COTSBot.  A COTSBot with a left and right track or left and right wheels that are controlled be two independent DC motors. 

To use the code install the Arduino programming environment (http://www.arduino.cc/en/Main/software), create a new project, paste the code into the project, verify it, and up load it.

Once the code is uploaded to the Arduino you can run any program that makes a serial connection to the Arduino and have it send basic forward, left, right, back, and stop commands to drive the robot.  See, for example, our basic path1 program written in Processing (http://cotsbots.wordpress.com/basic-design/netbook/basic-processing-code/).

The code:

/*
 * Very basic arduino code for COTSBots with a skidsteer platform.
 * Assumes a left and right track controlled by a DC motor.
 * Note that left/right is completely arbitrary.
 *
 * This program allows the ARDUINO to receive and implement the following commands:
 * F – forward
 * B – backward
 * L – left
 * R – right
 * S – stop
 *
 *Created by Terence Soule, University of Idaho
 */

const int leftDrivePin = 6;      // the pin that the left track is attached to
const int rightDrivePin = 5;      // the pin that the right track is attached to
const int leftDirectionPin = 7;   // the pin controlling the direction of the left track
const int rightDirectionPin = 4;  // the pin controlling the direction of the right track
/* For direction pins HIGH is forward, LOW is reverse
 * For speed this program only uses HIGH, which is full speed.
 * To use slower speeds the command
 * digitalWrite(leftDrivePin,HIGH);
 * can be replaced with
 * analogWrite(leftDrivePin,N);
 * where N is a number between 0 and 255, 0 is slowest (stop) and 255 is fastest.
 * However, depending on the motor power, batteries, etc. low speeds may cause the
 * DC motors to stall.
 */
void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // set the drive and direction pins to be output pins
  pinMode(leftDrivePin, OUTPUT);
  pinMode(rightDrivePin, OUTPUT);
  pinMode(leftDirectionPin, OUTPUT);
  pinMode(rightDirectionPin, OUTPUT);
}

void loop() {
  char input;

  // check if data has been sent from the computer:
  if (Serial.available()) {
    // read the most recent character
    input = Serial.read();
    // switch based on the character
    switch(input){
      case ‘F':  // forward
        digitalWrite(leftDirectionPin,HIGH);
        digitalWrite(leftDrivePin, HIGH);
        digitalWrite(rightDirectionPin,HIGH);
        digitalWrite(rightDrivePin, HIGH);
        break;
      case ‘B':  // backwards
        digitalWrite(leftDirectionPin,LOW);
        digitalWrite(leftDrivePin, HIGH);
        digitalWrite(rightDirectionPin,LOW);
        digitalWrite(rightDrivePin, HIGH);
        break;
      case ‘S':  // stop
        digitalWrite(leftDrivePin, LOW);
        digitalWrite(rightDrivePin, LOW);
        break;
      case ‘L':  // left
        digitalWrite(leftDirectionPin,HIGH);
        digitalWrite(leftDrivePin, HIGH);
        digitalWrite(rightDirectionPin,LOW);
        digitalWrite(rightDrivePin, HIGH);
        break;
      case ‘R':  // right
        digitalWrite(leftDirectionPin,LOW);
        digitalWrite(leftDrivePin, HIGH);
        digitalWrite(rightDirectionPin,HIGH);
        digitalWrite(rightDrivePin, HIGH);
        break;
      default:    // if character not recognized then stop
        digitalWrite(leftDrivePin, LOW);
        digitalWrite(rightDrivePin, LOW);
        break;
    }
  }
}

3 Responses to Basic Arduino Code

  1. Juan says:

    Thank you so much for sharing this code!

  2. Lord Corwin says:

    Thanks. Well written and easy to follow.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s