2012年5月1日火曜日

小数点の表示

Arduinoではディスプレイに数字を自由に表示するのが難しい。
そこで小数点表示することを色々と調べた。

Sketch

// include the library code:
#include <LiquidCrystal.h>
// refer to  http://garretlab.web.fc2.com/arduino/reverse_lookup/index.html
// initialize the library with the numbers of the interface pins
//LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.home();
  Serial.begin(9600);
  char s[16];
  char t[16];
  //double val = 12.345678;
  double val = -12.345678;
  dtostrf(val, 5, 1, s);
  Serial.println(s);
  lcd.print(s);
  val = -12.345678;
  dtostrf(val, 5, 2, s);
  lcd.setCursor(8,0); 
  lcd.print(s);
 if( val>0) {
  Serial.print("+");
  Serial.println(s);
 }
 else{
   Serial.println(s);
 }
  sprintf(t, "Val = %s", dtostrf(val, 5, 10, s));
  Serial.println(t);
  lcd.setCursor(0,1); 
  lcd.print(t);
  dtostre(val, s, 3, DTOSTR_ALWAYS_SIGN|DTOSTR_PLUS_SIGN);
  Serial.println(s);
}
void loop() {
 
}
/*
  LiquidCrystal Library - display() and noDisplay()

 Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
 library works with all LCD displays that are compatible with the
 Hitachi HD44780 driver. There are many of them out there, and you
 can usually tell them by the 16-pin interface.
  This sketch prints "Hello World!" to the LCD and uses the
 display() and noDisplay() functions to turn on and off
 the display.

 The circuit:
 * LCD RS pin(pin 4) to digital pin 12
 * LCD Enable pin(pin 6) to digital pin 11
 * LCD D4 pin(pin 11)to digital pin 7
 * LCD D5 pin(pin 12) to digital pin 6
 * LCD D6 pin(pin 13) to digital pin 5
 * LCD D7 pin(pin 14) to digital pin 4
 * LCD R/W pin(pin 5) to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

 Library originally added 18 Apr 2008
 by David A. Mellis
 library modified 5 Jul 2009
 by Limor Fried (http://www.ladyada.net)
 example added 9 Jul 2009
 by Tom Igoe
 modified 22 Nov 2010
 by Tom Igoe
 This example code is in the public domain.
 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

0 件のコメント:

コメントを投稿