|
Re: How do I program this sensor?
To clarify, this is a continuous loop of rope and you're only ever running the motor in one direction, correct? There's a few problems in your code. It will compile, but probably won't do what you think. I can't comment on whether it will work in your application cause I can't quite decipher what your apparatus is doing. Here's what I've noticed:
1. You mean rc_dig_in04, NOT, digital_io_04. You use digital_io_04 to define port 4 as an output or input. once you've defined it as an input, you read its value from rc_dig_in04.
2. I think you're going to want to wrap your entire bit of code there in an if(pwm03 != 127) statement. If you don't, your code will be counting up as long as rc_dig_in04 is 1. Even if the motor isn't running and just sitting there.
3. I don't think you want this in Process_Data_From_Local_IO. Unless you're pretty certain about the output of the sensor, you might have to deal with a "bouncing" issue where the input value bounces between 0 and 1 as the sensor decides whether it sees something or not. Process_Data_From_Local_IO loops fast enough that it might get confused. Aside from which, changing the pwm03 value won't have any effect until the next time your Default_Routine runs anyways, so I'd put all of this in there.
4. After you set pwm03 to 127, the tape is still sitting in the sensor, and the sensor is still reading 1. So when you start up the motor again, the sensor will still say 1, and you'll instantly stop. You should add in code that waits until rc_dig_in04 goes back to 0 before you start checking for it to be 1 again.
5. Finally, your giant if-else chain for count is really tailor made for for a switch-case statement. You might want to change it to that to simplify your code a bit.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.
Lone Star Regional Troubleshooter
|