Go Down

Topic: help using a encoder sensor (Read 1 time) previous topic - next topic

adilmalik

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??

RuggedCircuits

How about something like:

Code: [Select]

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

adilmalik

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?

adilmalik

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..

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy