Pages: [1]   Go Down
Author Topic: help using a encoder sensor  (Read 1203 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi, im new here and with the arduino ..i have little arduino programing knowledge . so i made a sort of reflective sensor using a phototransistor and ir led and i want it to count an encoder strip with black and white lines .i hav made a code:
 int count = 0 ;
void setup() {
  Serial.begin(9600);
}

void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(count, DEC);
if (sensorValue < 1000)
{
  count = count + 1;
  delay (1000);
}
else
{
  count = count + 0;
}
}


------------------------------------------------------------------------------
this counts the number of white lines it sees but there is one problem i want it to increase count one time when it sees a white line but now if i keep a white line on the sensor it keeps on counting? who can i limit one count per white line??
Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

How about something like:

Code:
uint8_t isWhite=0;
void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(count, DEC);
  if ((sensorValue < 1000) && !isWhite) {
    count = count + 1;
    isWhite = 1;
  } else if (isWhite) {
    isWhite = 0;
  }
}

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for posting the code i checked t but it doesnt work it keeps on counting even when im on the same strip of white?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes i think its also important to mention that its a white black white(and so on) strip .so when t sees a black strip the output is around 1022-1023..
Logged

Pages: [1]   Go Up
Jump to: