Hi There
I am currently using the Arduino UNO to read three ADC channels.
I am making use of the Arduino IDE but not the analogRead() function.
The code works well, but my problem is, the order in which the data is being printed to the serial monitor differs from the order of the code, e.g. instead of displaying 0-1-2 it displays 2-0-1. The last value is always being printed first, irrespective of the order
Any advice please.
Francois
void loop() {
ADMUX = 0b11100000; // select channel 0
delay(10); //delay 1 second
ADCSRA |= (1 << ADSC); //start single conversion
analogData = ADCH; //store data in analogData variable
float volt = (analogData/255.0)*1.1;
Serial.print(volt);
Serial.print(" ");
ADMUX = 0b11100001; // select channel 1
delay(10); //delay 1 second
ADCSRA |= (1 << ADSC); //start single conversion
analogData = ADCH; //store data in analogData variable
float volt1 = (analogData/255.0)*1.1;
Serial.print(volt1);
Serial.print(" ");
ADMUX = 0b11100010; //select channel 2
delay(10); //delay 1 second
ADCSRA |= (1 << ADSC); //start single conversion
analogData = ADCH; //store data in analogData variable
float volt2 = (analogData/255.0)*1.1;
Serial.println(volt2);
You must wait for the conversion to complete, it will not be available until the ADSC bit goes
low in the ADCSRA register:
ADCSRA |= (1 << ADSC); //start single conversion
while (ADCSRA & (1<<ADSC)) {} // wait for completion
analogData = ADCH; //store 8 bit data in analogData variable
You can look to see how this is done in the source code for analogRead for instance (you have the
sources, its an open-source project)...