Hardware 19
Hardware
I changed the amount of light going to the photodiode to change the noise coming out the speaker. Play
around!
3.8: Using serial (optional)
If you have time or want to learn more about microcontrollers, the serial library is a good way to get more out
of your microcontroller.
In our analog sensor program, we assumed our sensor would go all the way from 0 to 1023. However, this is
generally not the case, sensors are often not as sensitive as we would like. In our program, we have no real
way of telling how sensitive it is, and our LED may not be lighting up as bright or dim as much as we really
want.
The serial library is a way to see the actual numbers the microcontroller is using, and to see exactly what it
is reading from the pin. This can also be helpful for seeing if the sensor isn’t working at all. The word serial
has to do with how the Galileo sends information back to the computer, which is via a serial port. A library is
a collection of code someone else has written, which makes our jobs easier.
The microcontroller needs to be told that we are using serial, which means we have to tell it to beginning in
setup:
scaledvalue=map(avalue,0,1024+1024,0,254);//Scaletheoutput
appropiately,Iguessedandcheckedtomakethesoundmoresensitive.
analogWrite(out,scaledvalue);//writethescaledvaluetotheoutput
pin
delay(map(avalue,0,1024+1024,0,100);
}
The number, 9600, tells the microcontroller the data rate of the serial, which has to be a certain number.
9600 should be the number you use, until you get into super-advanced microcontroller stu.
Then, we want to get information to print. In loop we can put commands such as:
Serial.print(“hello”);//printhello!
Serial.println(“hey”);//printheyand
startanewline
Serial.print(myVariable);//printthevalue
ofmyvariable
Serial.print(“\n”);//printanewline
voidsetup(){
Serial.begin(9600);
}