EasyManua.ls Logo

Intel Galileo - Programming the Board; Automatic Software Reset

Intel Galileo
34 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Hardware 17
Hardware
analogWrite(myVariable,3);
...to write the value read from the sensor to pin 3. However, the range for analogRead is from 0 to 1023,
and the range for analogWrite is0 to 255. That means we have to make myVariable smaller in order to
make sure analogWrite has a value that makes sense of it.
One way to do this is to simply divide by four (1023/4 ~= 255 (do the division or get a calculator if you don’t
believe me). So we can write:
analogWrite(3,myVariable/4);
The slash (/) just tells the microcontroller to divide. A better way to do this is to use something called the
map function (http://arduino.cc/en/reference/map), for this example it would look something like this:
analogWrite(3,map(myVariable,0,1023,0,255));
The map function does the math for us, it’s general form is:
map(value, fromLow, fromHigh, toLow, toHigh);
It would be a wonderful world if we could just write...
The value is the value we want to scale, the “from” categories are the current range of the value, the “to”
categories are the desired range. The reason the map function is better is that if our analog sensor doesn’t
quite reach both extremes we can easily change a few numbers. This is similar to trying to normalize your
data in case you have learned this in your math or science class. Our total program will look like this:
voidsetup(){
pinMode(A0,INPUT);//ouranalogsensor
pinMode(3,OUTPUT);//ouranalogoutput(aLED)
}
voidloop(){
intmyVariable;//declaringthevariablewewilluse
myVariable=analogRead(A0);//readthevaluefromthesensor
analogWrite(3,map(myVariable,0,1023,0,255));//writeto
theLED
}
This is a longer program, so you will probably experience some syntax errors. Try “verify” often so you can
nd and x the bugs. Also notice the order of the program, what would happen if you mixed up the order of
anything? Try changing it around to see how it breaks.

Other manuals for Intel Galileo

Related product manuals