This exercise is based on http://www.ladyada.net/learn/sensors/cds.html
Since I tried photosensor with analog input,this tutorial going to explain how to build a circuit and program for photosensor with digital input.
The circuit for photosensor with digital input is same as what we made for Boe-Bot.
We will use Capacitor and measure the pulses that are sent from it with digital pin.
Actually, if we use analog input for photosensor, it is going to be 5 time easier than do a same thing with digital input. Especially, the program will be more complex. However, it is good to learn.
Requirements
Photosensor
Capacitor(I use a capacitor took out from Boe-Bot)
Wires
Arduino
Circuit
Program
This program read a interval of signals that send from capacitor as a darkness of the room.
More detail go http://www.ladyada.net/learn/sensors/cds.html
Basically, photosensor is a variable resistor so that changes the amount of electric supply to a capacitor by the darkness, so that interval of the signal from the capacitor will change.
int photoPin = 2; // declare valiable
int val;
void setup(void)
{
Serial.begin(9600); // setup serial speed
}
void loop(void)
{
val = Reading(); // this cord is very similar with GOSUB that we used in Boe-Bot
Serial.println(val);//send back data from Arduino
delay(100);
}
//the following cord is similar with "Subroutine"
int Reading()
{
int time=0;
int pin = 2;
pinMode(pin, OUTPUT); //setup pin . This step is important.
digitalWrite(pin, LOW); //set the pin to ground. This step is also important.
pinMode(pin,INPUT);//set the pin to read a signal
while (digitalRead(pin)==0)//make a timer
{
time++;
if(time==30000)//in case nothing is connected
{
break;//return to "void loop"
}
}
return time;//return the value to variable "Reading"
}
0 件のコメント:
コメントを投稿