Scaling a value to 8 bits

Below is a snippet of code from the Reference section under the heading of Functions. I am not having any trouble understanding the use of functions (at least, not today) but I don’t’ understating the line that says if is changing the value to an 8 bit value by dividing it by 4. How does this work? I can come up with a lot of values that dividing them by 4 doesn’t reduce them to 8 bit

Another example
This function will read a sensor five times with analogRead() and calculate the average of five readings. It then scales the data to 8 bits (0-255), and inverts it, returning the inverted result.

int i;
int sval;

for (i = 0; i < 5; i++){
sval = sval + analogRead(0); // sensor on analog pin 0
}

sval = sval / 5; // average
sval = sval / 4; // scale to 8 bits (0 - 255)
sval = 255 - sval; // invert output
return sval;
}

256 * 4 = 1024, the max value of the internal 10 bit adc

the result of analogRead is 10 bits, dividing by 4 is the equivalent of shifting right 2 spots dropping the bottom 2 bits and leaving the top 8.

Thanks guys! This is where having experience helps. I don’t have enough experience with the Arduino (or C in general) to immediately think about the output from the analogRead(). Makes complete sense now.

Another thing; what took you guys so long, this message was up for all of about 10 mintues!

Another thing; what took you guys so long, this message was up for all of about 10 mintues!

Sorry, but this forum is stuck on one of the slowest and flacky servers known to man or beast.

Lefty

for the record I responded in < 4min

Troll maybe, but I am installing windows fresh on my laptop so not a bunch to do while babysitting

Osgeld,

Doing a system rebuild is my favorite reading time, you really need to be around to answer the few prompts that come up but you sure can't do anything but watch the thermometer.

Of course, you know the other place where all you can do is sit there so you just as well read or, if you are properly connected, respond to dumb questions on the forums.

Jim, K6JMG

well I got a dirt cheap decent laptop, with no ac adapter on ebay, so the hp brick I am using is 2.2 amps and the machine with full backlight, full speed, DVD-rom and hard drive thrashing + cardbus wifiG adapter is pulling 2.7 amps peak

SO I am semi stuck here in front of 2 computers doing all the FUN update stuff making sure my brick doesnt melt the carpet. Its holding steady, but I dont feel safe enough to go run off and play xbox for a few hours, and Id like to flip this thing back on ebay pretty quick before the xmas frenzy is over (it was a parts only laptop, ive got it in "good" shape and flawless performance, 4 amp brick is in the mail...)

Good job!! Hope you make a few bucks on it.

Jim, K6JMG