I am experimenting with Arduino Uno and trying different implementations of analogread() function.
When I upload the following code to mcu ;
int analogpin = 0;
byte val = 0;
void setup() { Serial.begin(9600);
}
void loop() {
val = analogRead(analogpin); Serial.println(val,DEC);
delay(100);
}
and connect from serial monitor I can see the voltage from pin 0. My concern is that it sends the read data 10 times a second(delay(100)) and when I try to upload another code Arduino interface returns error because the device is constantly busy. Any idea how to get over this ?
analogRead returns an int (= 2 bytes) that will not fit in one byte. This can corrupt your sketch especially the variables around val (e.g. analogpin).
Please post the error message you get when the upload fails...
My concern is that it sends the read data 10 times a second(delay(100)) and when I try to upload another code Arduino interface returns error because the device is constantly busy.
To the Arduino, 10 times a second leaves an eternity between sends.
Of course, you could make the time between sends larger, by making the sending of data faster. 9600 baud is pretty slow.
What does your concern about sending so much data have to do with a problem with analogRead?
From serial monitor I read 8 bit values (0-255), when I connect analogpin to 3.3V on board it fluctuates around 172-174. I suppose this is because internal adc is not that good.
If analogread() returns 2bytes this corresponds to 16bit of information, it states that the mcu has max 10bit adc, how is this possible ?
If analogread() returns 2bytes this corresponds to 16bit of information, it states that the mcu has max 10bit adc, how is this possible ?
Because there is no ten bit native data type on the AVR microcontroller, the next largest size must be used to store the ten bit result.
when I connect analogpin to 3.3V on board it fluctuates around 172-174.
You're missing precision here - you're only taking the least significant eight bits
(3.3 / 5) * 1023 = 675 (or thereabouts), which is more than you can store in a single byte.
The 172 you see is the result of the truncation of the most significant bits.