[Arduino Project] Analog Digital Reader

This is a simple project that I did with the LCD Shield to learn how to use it. Basically, you can use the buttons to scroll through the different pins and control the lcd display back light. Basically, this is a mini voltmeter. Good for testing out raw sensor readings.

Source Code as Follow:


/* Yu Hin Hau
 * LCD Shield Gadget
 * 7/31/2012
 */

//Initialize LCD
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//Remap Digital Pins on Shield
int digitalPin[] = {0,1,2,3,11,12,13};

//Initialize Variables
boolean led_on;
int delay_time;
int dIndex;
int aIndex;

void setup()
{
//Set LCD Display Size
lcd.begin(16,2);
lcd.clear();

//Control LED Backlight @ D10
pinMode(10, OUTPUT);
led_on = false;

//Digital Access Ports
pinMode(digitalPin[0], INPUT);
pinMode(digitalPin[1], INPUT);
pinMode(digitalPin[2], INPUT);
pinMode(digitalPin[3], INPUT);
pinMode(digitalPin[4], INPUT);
pinMode(digitalPin[5], INPUT);
pinMode(digitalPin[6], INPUT);

//Define Values
dIndex = 0;
aIndex = 1;
delay_time = 250;
}

void loop()
{
  //Set LED State
  backLight(led_on);

  //Read Shield Button from Analog 0 Pin
  double button = analogRead(0);

  //Set LCD Display Settings
  lcd.setCursor(0,0);
  String text = "Digital " + String(dIndex) + ": " + toState(digitalRead(digitalPin[dIndex])) + "       ";
  lcd.println(text);
  lcd.setCursor(0,1);
  String text2 = "Analog  " + String(aIndex) + ": " + toVoltage(analogRead(aIndex)) + "       ";
  lcd.println(text2);

  //Detect SELECT Button
  if(button >= 740 && button <= 800)
  {
  led_on = !led_on;
  backLight(led_on);
  delay(delay_time);
  }

  //Detect UP Button
  if(button >= 140 && button <= 150)
  {
  if(dIndex < 6)
  dIndex += 1;

  if(aIndex < 5)
  aIndex += 1;

  delay(delay_time);
  }

  //Detect DOWN Button
  if(button >= 320 && button <= 330)
  {
  if(dIndex >0)
  dIndex -= 1;

  if(aIndex >1)
  aIndex -= 1;

  delay(delay_time);
  }

  //Detect LEFT Button
  if(button >= 500 && button <= 510)
  {
  delay(delay_time);
  }

    //Detect LED Button
  if(button >= 0 && button <= 10)
  {
  delay(delay_time);
  }

}

//LED Back Light Control
void backLight(boolean led)
{
   if(led)
    digitalWrite(10, HIGH);
  else
    digitalWrite(10, LOW);
}

//Convert Analog to Voltage
String toVoltage(int reading)
{
  double voltage = reading*5.0/1023;
  int ones = voltage / 1;
  int tenth = (voltage - ones ) * 10;
  return String(ones) + "." + String(tenth) + " V";
}

//Convert Digital to State
String toState(int reading)
{
  if(reading == 0)
  return "LOW";
  else
  return "HIGH";
}
Advertisements

One thought on “[Arduino Project] Analog Digital Reader

  1. Pingback: [Arduino Project] Analog Digital Reader | 1 hobidest

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