help! Am really stuck!!!!

Have been trying for a week to get digital input while in autonomous mode. After the Getdata line in void User_autonomous_Code we have

if (rc_dig_in06 == 0) pwm01=255: else pwm01=127;

We have the light sensor hooked up to digital input #6. It appears to be working and we have adjusted sensitivity on it. We have also swapped out the sensor. Simple lines like pwm01=255; work fine in our program. I have tried moving this line in all places in the program but it doesn’t help.

My students want to write code to read wheel speed using this sensor, but I am frustrated I can’t get input.

Any ideas would be very much appreciated!!!

I dont know if your code is correct or not, but I do know that in the past, if you set a pwm output to 255 then all sorts of weird and bizarre things happened.

I think the pwm output SW that is buried in the code where we dont see it uses pwm values of 255 to define the start of the data stream, so if you set a pwm to 255, the output stream starts over, putting variables where you didnt intend them to go.

I havent heard that this is no longer true - but it really did make the bot totally wack out if you did it last year, so its possible this is why its not doing what you want.

We set the pwms to 255 ( isn’t this the max allowable speed?) in regular code and I thought it worked OK. I’ll change this and try it , though.

lat year you had to be careful to limit them to 254 max - Im not sure its the same with the new RC, but unless an expert pipes-in and says otherwise, its worth a try.

There are a couple of things wrong with the line as you posted it. The value 255 is invalid and there should be no colon after “254”. It really should be:

if (rc_dig_in06 == 0) 

or if you really like to run things together:

if (rc_dig_in06 == 0) pwm01=254; else pwm01=127;

To help you debug it add:

printf("input 6 = %d
", (int) rc_dig_in06 );

You can at least see and confirm the functioning of the light sensor.

If you’d like to send your code, PM me and I’ll help you debug the problem further.


Corrected my error (thanks Dav). I wasn’t thinking straight this morning.

Your code looks fine (except for the “:” but I assume that is a typo)

Digital inputs are “IN” by default. Is there any chance you initialized that bit for output and forgot it? That is PORTB6, which is one of the “change of state” interrupt inputs.

Does your light sensor have active outputs? I.e. is it a banner sensor? If you are using analog output device (e.g. an off-the-shelf photo-reflex sensor) you need to be aware of the type of input you are driving. It turns out the PIC processor has a variety of input types that depend upon which input pin you are using. In particular, port B pins are TTL and not suitable for analog inputs. Other bits are “Schottky” and are suitable for direct drive by an analog input. Anyway, that is a minor issue to be addressed after you figure out the basic code.

umm… no. There should be a semicolon after the 254 - it is the end of an instruction. the correct format is this:

if(rc_dig_in06 == 0)
    pwm01 = 254;

(you can always rearrange that however you wish with tabs, spaces, and line breaks)

Remember, the if-else statement is a control structure, and thus does not count as part of a single instruction. The two instructions here are “pwm01 = 254” and “pwm01 = 127”, each of which needs to be ended with a semicolon as shown.

You may have been confused with the colon because of the if-else shorthand notation - for instance, the code below does the exact same thing as the longer if-else below:

pwm01 = ( rc_dig_in06 ? 127 : 254 );

if you are new to programming, though, stick with the longer if-else statement, as it is easier to read and use.

Thanks for the ideas. My colon was a typo. I meant a semi-colon which works fine in all of the other statements we have. The light sensor we are using is the same one as last year which worked great with PWBASIC and the old controller. I guess I was wondering if we were doing something stupid ( which the 255 sounds like it is ).

255 is one of those snakes

with fangs

that dont really stand out until it bites you :c)