Let's Make Robots!

Code question with a line follow sensor...

So I am trying to add a encoder on one of my wheels so the robot knows how far it has gone/turned. I am using a simple IR line follower sensor and this on one of my gears:

Encoder_Wheel.jpg

The sensor can tell the diference between black and right obviously, and the sensor works well -I have had it blink a LED on and off and it worked great.

Now I am trying to get the picaxe to count the white spaces and am running into problems...

Here is the code I am using now:

let w1=0
main:
servo 2,75 'make the servo motor go
if pin3=1 then let w1=1+1 'input pin for line follow sensor (high = white)
end if
if w1=16 then goto onerev 'should be one full turn
goto main


onerev:
high 6 'turn on indicator led
low 2 'stop drive motor
pause 5000
low 6 'turn off indicator led
let w1=0
goto main

 

Here's the problem:

As the code cycles round and round, it might count a "white or high" space a few times not just once. As the gear turns and the sensor reads white and black the code cycles around... The gear could be on a white space for 3 or 4 cycles of code and thus the code will keep adding 1 to the w1 variable 3 or 4 times. Does anyone have any ideas as to count a high signal as only a 1 count until that pin goes low?

Did that make sense?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

It seems that some of my old posts are starting to resurface... I saw my old Vex/ picaxe test board bot go by a day or two ago. As I look at Walter now, it is hard to believe that I didn't know how to code a transistion from white to black. It seems so simple now. I guess this is why I get so annoyed by people asking questions about things that are so far ahead of where they are currently. -- I learned this stuff, step-by-step and I have the posts to prove it!

Oh, and by the way, a sensor to detect carpenters? Well, I do have a stud finder!

"... the difference between black and right  ...", now did you? That's bordering on racism. Or you invented one hell of a special sensor that deserves to be further documented. Is there a complementary "white and left" sensor? Does it detect Carpenters?

This is untested but I think you would do something like:

main:

setint %00001000, %00001000 ' enable input3 for interrupt on rising edge

...

goto main

interrupt: 'this must be the last subroutine defined! This is called whenever input3 goes to high.

w1 = w1 + 1 ' since this will only be called for rising edges you're safe to do it like this.

if w1 > 15 then

... 'update some revolution counter

w1 = 0

endif

return

The setint %00001000, %00001000 is not really that tricky - it just looks like a bad mother :-). it operates on all 8 input pins at once which is why there is a combination of 8 0's and 1's in both parameters - one for each input pin.

The first parameter determines how each input pin will be configured to interrupt. They can either interrupt on falling edge (going from high to low) which is marked by a 0 or in rising edge (going from low to high) which is marked by a 1. In the above example input3 has been set to rising edge while all other input pins are set to falling edge. So whenever input3 goes from low to high the picaxe will tell us - depending on the second parameter:

The "interrupt:" routine is the routine that gets called whenever an interrupt occurs. For some reason it must be defined as the very last routine in your program code.

The second parameter determines which input pins the picaxe should keep an eye on and notify your interrupt handler subroutine about. In the above example it is only input3 that has been enabled for interrupts.

Hope it helps, otherwise just stick to the solution TheCowGod came up with until you get into the whole interrupt groove - no need to rush things :-)

- Jimmy

Hi Chris,

You can definitely do it as described by TheCowGod, but you could also use the PICAXE's interrupt function by using the command "setint" (read about it in the PICAXE basic manual). I know this is more advanced stuff and might be a bit intimidating for the not so experienced programmers but ultimately when you get better and better at programming, interrupts will be the way you want to go :-).

The way interrupts work is basically that you tell the PICAXE to keep an eye on a certain number of pins and when one or more of these change from high to low or from low to high, the PICAXE will interrupt your program and redirect execution to a special subroutine you have made for handling interrupts. When your interrupt handler subroutine has finished its work - which could be to increment the revolution step counter, program execution will continue where it left off when the interrupt occured.

- Jimmy

 

 Hey guys thanks for the help. --I totally understand cowgod's theory I think it is great. I can't seem to figure out the code. You are a picaxe guy, right Jimmy? Any chance I could get a little snippet of solid code? --If it is not too much to ask. For the record, the setint command confused me.

www.rocketbrandcustom.com baby!!

Maybe have another variable to keep track of the current color you understand you're looking at, that way you can tell if the color has changed from what you had seen before.

 

if(pin3 != previously_seen_color) then

  // this is an edge transition, so count the change

   if pin3 = 1 then

      let w1 = w1 + 1

   end if

   previously_seen_color = pin3

end if

 

I don't know picaxe so consider the above more like pseudocode :) But hopefully you understand my idea.

Dan