Arduino Test Sketches

Test Responding To Commands
I2C Scanner
Read Serial Data and Display on LCD

Test Responding To Commands

// ================================================================
// Test Responding To Commands
// ================================================================

#include <Wire.h>

#define BAUDRATE  9600

int  pos      = 0;             // servo position
int  posStart = 0;             // servo start posion
int  posEnd   = 0;             // servo end position
char cmd;                      // command

// ================================================================
// setup
// ================================================================

void setup()
{
   // initialize serial port

   Serial.begin(BAUDRATE);   
}

// ================================================================
// main loop
// ================================================================

void loop()
{
  // process serial input ----------------------------------------- 

  if (Serial.available())
  {
    // wait for the entire message to arrive ----------------------

    delay(200);           

    // get the command --------------------------------------------

    cmd = Serial.read();

    // return a command response-----------------------------------

    if (cmd == 'S')            // ping sweep
    {
      posStart = Serial.parseInt();
      posStart = constrain(posStart,0,179);
      posEnd   = Serial.parseInt();
      posEnd   = constrain(posEnd,0,179);
      Serial.print("S");
      Serial.print(posStart,DEC);
      Serial.print(",");
      Serial.println(posEnd,DEC);
      sendTestPingSweep(posStart,posEnd);
      Serial.println("x");
      Serial.flush();
    }
    else if (cmd == 'P')       // ping
    {
      Serial.print("p");
      Serial.println(124,DEC);
      Serial.println("x");
      Serial.flush();    
    }    
    else if (cmd == 'M')       // move servo
    {
      pos = Serial.parseInt();
      pos = constrain(pos,0,179);
      Serial.print("M");
      Serial.println(pos,DEC);
      Serial.println("x");
      Serial.flush(); 
    }
    else if (cmd == 'C')       // current state  
    {
      Serial.print("c");
      Serial.print(45,DEC);
      Serial.print(",");
      Serial.println(90,DEC);
      Serial.println("x");
      Serial.flush();
    }
    else                       // error
    {
      Serial.print("e");
      Serial.println(cmd,DEC);
      Serial.println("x");
      Serial.flush();       
    }

    // remove the remaining characters in the buffer (if any) -----

    emptySerialInputBuffer();
  }               
}

// ================================================================
// subroutines
// ================================================================

void sendTestPingSweep(int s, int e)
{
   for(int i = s; i<= e; i++)
   {
     Serial.print("s");
     Serial.print(i,DEC);
     Serial.print(",");
     Serial.println(i*10,DEC);
   }
}

void emptySerialInputBuffer()
{
   while(Serial.read() != -1);
   return;
}

I2C Scanner

// ----------------------------------------------------------------
// i2c_scanner
//
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
//
// This sketch tests the standard 7-bit addresses
// from 0 to 127. Devices with higher bit address
// might not be seen properly.
//
// Adapted to be as simple as possible by Arduino.cc user Krodal
//
// June 2012
// Using Arduino 1.0.1
// ----------------------------------------------------------------

#include <Wire.h>

void setup()
{
  Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}

void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 0; address <= 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println(" !");

      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknow error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(8000);           // wait 8 seconds for next scan
}

Read Serial Data and Display on LCD

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR      0x3F  // Define the PCF8574A I2C Address

#define BACKLIGHT_PIN 3

#define En_pin        2
#define Rw_pin        1
#define Rs_pin        0
#define D4_pin        4
#define D5_pin        5
#define D6_pin        6
#define D7_pin        7

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup()
{
  lcd.begin(20,4);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home();
  lcd.print("Test Input:");
  Serial.begin(9600);
}

void loop()
{
  // when characters arrive over the serial port...
  if (Serial.available())
  {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    //lcd.clear();
    // read all the available characters
    while (Serial.available() > 0)
    {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
  }
}