#define CHANNELS 4 #define state_def 0 #define state_raw 1 #define state_cal 2 #define state_minmax 3 #define state_last 4 /* enum State { // running state def, raw, cal, minmax, laststate, };*/ #define only_sum 1 #define only_minmax 2 char state = state_def; bool hold = 0; int input[CHANNELS]; int min1[CHANNELS]; int max1[CHANNELS]; long time1[CHANNELS]; unsigned int cal_in[CHANNELS]; // 170 unsigned int cal_out[CHANNELS]; // 5000 unsigned int divider[CHANNELS]; // 1000 x.### char channel = 1; // AIN selected to display unsigned long sum1[CHANNELS]; long now1, prev, start1, end1; long period = 500000; int max_dt = 0; int min_dt = 9999; int ticks = 0; long time_display; void init_cal() { cal_in [0] = 1023; cal_out[0] = 5000; divider[0] = 1000; cal_in [1] = 170; cal_out[1] = 5000; divider[1] = 1000; cal_in [2] = 1023; cal_out[2] = 4950; divider[2] = 1000; cal_in [3] = 1023; cal_out[3] = 4950; divider[3] = 1000; } /* http://www.dfrobot.com/wiki/index.php?title=Arduino_LCD_KeyPad_Shield_%28SKU:_DFR0009%29 LiquidCrystal Library - Hello World 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 shows the time. The circuit: * LCD RS pin to digital pin 12 * LCD Enable pin to digital pin 11 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 * LCD R/W pin to ground * LCD VSS pin to ground * LCD VCC pin to 5V * 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 */ // include the library code: #include #include "lcd1602_key.h" // initialize the library with the numbers of the interface pins //LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // RS E d4 d5 d6 d7 LiquidCrystal lcd(8, 9, 4, 5, 6, 7); char str[20]; void reset1(char what) { for (char i = 0; i < CHANNELS; i++) { if (what & only_sum) sum1[i] = 0; if (what & only_minmax) { max1[i] = -1; min1[i] = 9999; } } } void setup() { // Serial.begin(115200*8); // Serial.begin(921600); Serial.begin(115200); // Serial.begin(460800); // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. // lcd.backlight(); // Serial.println("create char..."); for (char c = 0; c < 8; c++) { uint8_t buf[8]; // memset(buf,0,8); for (char i = 0; i < 8; i++) buf[i] = i <= c ? 255 : 0; lcd.createChar(c, buf); } lcd.setCursor(8, 0); lcd.write(8); lcd.write(1); lcd.write(2); lcd.write(3); lcd.write(4); lcd.write(5); lcd.write(6); lcd.write(7); // lcd.print("hello, world!"); lcd.setCursor(0, 1); lcd.print(F("#: Min Avg Max")); delay(2000); start1 = prev; reset1(-1); time_display = 0; init_cal(); Serial.println(F("setup finished")); prev = micros(); } char prev_key = btnNONE; void loop() { // Serial.println("---loop---"); now1 = micros(); if (!hold) { // cli(); for (char i = 0; i < CHANNELS; i++) { // int a = analogRead(i); // input[i] = a; input[i] = analogRead(i); } end1 = now1; ticks++; // sei(); int dt = now1 - prev; if (dt > max_dt) max_dt = dt; if (dt < min_dt) min_dt = dt; for (char i = 0; i < CHANNELS; i++) { int a = input[i]; if (a > max1[i]) { max1[i] = a; lcd.setCursor(11, 1); lcd.write(8); } if (a < min1[i]) { min1[i] = a; lcd.setCursor(6, 1); lcd.write('_'); } sum1[i] += dt * (long)a; } prev = now1; } // lcd.setCursor(4, 0); lcd_key = read_LCD_buttons(); if (prev_key != lcd_key) { switch (lcd_key) // depending on which button was pushed, we perform an action { case btnRIGHT: // reset maxmin { // lcd.print("RIGHT "); reset1(-1); prev = micros(); start1 = prev; break; } case btnLEFT: { // lcd.print("LEFT "); hold = !hold; lcd.setCursor(12, 0); if (hold) lcd.print(F("HOLD")); else { lcd.print(F(" ")); reset1(only_sum); prev = micros(); start1 = prev; ticks = 0; } break; } case btnUP: { // lcd.print("UP "); channel++; if (channel >= CHANNELS)channel = 0; break; } case btnDOWN: { // lcd.print("DOWN "); channel--; if (channel < 0)channel = CHANNELS - 1; break; } case btnSELECT: { // lcd.print("SELECT"); state = state + 1; if (state == state_last) state = 0; lcd.setCursor(12, 0); if (state == state_def) lcd.print(F(" RUN")); if (state == state_raw) lcd.print(F(" RAW")); if (state == state_cal) lcd.print(F(" CAL")); break; } case btnNONE: { // lcd.print("NONE "); break; } } // switch prev_key = lcd_key; } if ((now1 - time_display) > period) { // display processing // if ((now1 = micros()) - start1 > period) { Serial.print(F("------ ")); Serial.print(now1); Serial.print(F(" ")); Serial.print(start1); // Serial.print(F(" dt=")); // Serial.print(dt); Serial.print(F(" maxdt=")); Serial.print(max_dt); Serial.print(F(" mindt=")); Serial.print(min_dt); Serial.print(F(" ticks=")); Serial.println(ticks); // Serial.print("A1="); // Serial.print(input[1]); // Serial.print(" sum1="); // Serial.println(sum1[1]); Serial.print(F("A=")); Serial.print(input[channel]); Serial.print(F(" sum=")); Serial.print(sum1[channel]); long avg = sum1[channel] / (end1 - start1); long volt = (long long) sum1[channel] * cal_out[channel] / cal_in[channel] / (end1 - start1); int div1 = divider[channel]; Serial.print(F(" avg=")); Serial.print(avg); Serial.print(F(" volt=")); Serial.println(volt); Serial.print(F("start1=")); Serial.print(start1); Serial.print(F(" end1=")); Serial.println(end1); // lcd.setCursor(11, 0); // lcd.print((unsigned char)channel); if (state == state_def) { long min1v = (long) min1[channel] * cal_out[channel] / cal_in[channel]; long max1v = (long) max1[channel] * cal_out[channel] / cal_in[channel]; // sprintf(str, "%4d %2d.%03d ", (int)avg, (int)(volt / 1000), volt % 1000); sprintf(str, "%4d %2d.%03d ", input[channel], (int)(volt / div1), (int)volt % div1); str[11] = 0; lcd.setCursor(0, 0); // lcd.print(str); Serial.println(str); // sprintf(str, "%1d:%4d %4d %4d", channel, min1[channel], input[channel], max1[channel] ); sprintf(str, "%1d:%2d.%03d %2d.%03d ", channel, (int)(min1v / div1), (int)min1v % div1, (int)(max1v / div1), (int)max1v % div1 ); lcd.setCursor(0, 1); // lcd.print(str); Serial.println(str); } if (state == state_raw) { // sprintf(str, "%4d %2d.%03d ", (int)avg, (int)(volt / 1000), volt % 1000); sprintf(str, "%4d %2d.%03d ", input[channel], (int)(volt / div1), (int)volt % div1); str[11] = 0; Serial.println(str); lcd.setCursor(0, 0); // lcd.print(str); // sprintf(str, "%1d:%4d %4d %4d", channel, min1[channel], input[channel], max1[channel] ); sprintf(str, "%1d:%4d %4d %4d", channel, min1[channel], (int)avg, max1[channel] ); lcd.setCursor(0, 1); // lcd.print(str); } if (!hold) { reset1(only_sum); max_dt = 0; min_dt = 32767; ticks = 0; start1 = now1; } time_display = now1; } delay(1); }