User Guide
Encoders - Counting
Thecodebelowsimplycountsthenumberofchangesasviewedbyasinglechannelanalog
encoder.Thesuggestedencoder(TamiyaEncoderKit)usesadiscwith8xwhitesectionsand
8xcutouts.Thiswillprovideacountof16per360degreerotation.Itisuptoyoutooptimizeand
integrateitwithyourcode.Thecodealsosendscommandstothemotorstohavethemrotate.
intrawsensorValue=0;//variabletostorethevaluecomingfromthesensor
intsensorcount0=0;
intsensorcount1=0;
longcount=0;
voidsetup(){
inti;
for(i=5;i<=8;i++)
pinMode(i,OUTPUT);
Serial.begin(9600);
intleftspeed=255;//255ismaximumspeed
intrightspeed=255;
}
voidloop(){
analogWrite(10,255);
digitalWrite(12,LOW);
analogWrite(11,255);
digitalWrite(13,LOW);
delay(20);
rawsensorValue=analogRead(0);
if(rawsensorValue<600){//Minvalueis400andmaxvalueis800,sostatechancecanbedoneat600.
sensorcount1=1;
}
else{
sensorcount1=0;
}
if(sensorcount1!=sensorcount0){
count++;
}
sensorcount0=sensorcount1;
Serial.println(count);
}
RobotShopInc.www.RobotShop.com