#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
uint16_t prevVal;
void setup() {
ADCSRA |= ((1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0)); // Set the ADC clock to 16 MHz / 128 = 125 kHz
ADMUX |= (1 << REFS0); // Set the ref. voltage to Vcc (5V)
ADCSRA |= (1 << ADEN); // Activate the ADC
ADCSRA |= (1 << ADSC);
lcd.begin(16,2);
}
void loop() {
unsigned long timeElapsed = millis();
getADC(timeElapsed);
// getAnalog(timeElapsed);
}
void getADC(const unsigned long timeElapsed) {
uint16_t val = read_adc(0); // Read the value through the ADC
dispStats("ADC", val, timeElapsed);
}
void getAnalog(const unsigned long timeElapsed) {
uint16_t val = analogRead(A0); // Read the value with analogRead
dispStats("Analog", val, timeElapsed);
}
void dispStats(const String prefix, const uint16_t val, const unsigned long timeElapsed) {
if (prevVal != val) {
prevVal = val;
lcd.setCursor(0,0);
String mess = prefix + " Raw: ";
lcd.print(mess);
lcd.print(val);
lcd.setCursor(0,1);
lcd.print("Time: ");
lcd.print(timeElapsed);
}
}
uint16_t read_adc(uint8_t channel) {
ADMUX &= 0xE0; // Delete MUX0-4 bits
ADMUX |= channel & 0x07; // Sets in MUX0-2 the value of the new channel to be read
ADCSRB = channel & (1 << 3); // Set MUX5 value
ADCSRA |= (1 << ADSC); // Start conversion
while (ADCSRA & (1 << ADSC)); // Wait for the conversion to finish
return ADCW;
}