Can I get some examples for sensor programming

So I have everything coded except for the sensors. I’ve watched all the video tutorials but can’t seem to get it right. I’m not sure what my issue is.
I think if I look at an example I can get it right. I’m sure I’m just having a placement or syntax issue.

So if anyone can just share with me how they coded their sensors I would really appreciate it.

Thanks in advance.

So here is a pretty generic bit of code but is applicable to any absolute sensors.

//perform robot action that waits for sensor on this line
while ( mySensor.value(units) < targetValue ){  // use < or > based on the direction you expect the value to be in
     wait( sensorUpdateDelay, appropriateTimeUnits);
}
//stop the robot action that was waiting for the sensor on this line

If you are using relative sensors you will be better off using a thread because of how they hook into interrupt. For absolute sensors, here are some common update rates:
Vision: 50HZ, Optical: 50HZ, distance (laser): 30HZ, distance (ultrasonic): 20HZ, rotation sensor: 100HZ, v5 IME: 100HZ.
If you are using analog sensors, keep in mind that the line tracker is only 50HZ. Other analog sensors are dependent on the brain’s DAC speed, which iirc is 6000HZ. The brain’s DSP is 1000HZ iirc (so for a quad encoder do not exceed 500HZ because of the two wires and simultaneous bit processing).

TL;DR: Different sensors have different maximum update rates. Be sure to respect those rates if you want consistent values.

Hope this helps; I’m always happy to see someone going for sensors!

2 Likes

VEXcode has lots of included example code.

4 Likes

I have looked at the example and did not work on the code

the sensor that we are use is a Bumper

So if you instantiate a new digital in on the three wire port that the bumper is connected to, you can check if the bumper value is 0 (pressed) or 1 (released). You can keep checking that value in a loop if you want your code to wait until the switch is pressed/released.

can explain the code and what it looks like .

So my previous post does explain how to write that code. I cannot give you the exact code, as this clearly would be a violation of G4 – your robot and its code must represent your team’s skill level.

I am happy to help you (as I have done above) but I cannot just give you the answer. If you have specific questions I may be able to answer those, but it would be great if you try to write some code and show me what you have so I can help you improve it rather than make it for you.

TL;DR see rule G4 if you’re wondering why I am not just giving you full code

Ok I’m on brack of school ans qhen i get back i will check that out