Hello, i’m having a problem… If someone could help, i would greatly appreciate it. In the user routines.c file, i need to make pwm01 turn on for 1 second and turn off for another second, then turn on for another second, and so on and so forth. I want it to be on p3_sw_Trig… Does anyone know how i would do it? Please help… thank you very much.
Here’s some untested code that should do what you’re asking, basically this code toggles pwm01 between 0 and 127 every 44 loops, and makes pwm01 go back to off if the button is not pressed.
A static byte causes the compiler to not delete the data in a variable after it leaves a function, and “loops++” tells it to add 1 to the variable and store it back into itself, essentially saying loops = loops + 1
void code()
{
static byte loops = 0; // which loop are we on?
if (p3_sw_trig != 0)
{
loops++; // add 1 to the loop counter
if (loops <= 44) {
pwm01 = 127; // motor off
} else if (loops <= 88) {
pwm01 = 0; // motor on
} else {
loops = 0; // go back to 0
}
} else {
pwm01 = 127; // switch not on, motor is off!
}
}
First of all, I’m getting confused which one you are – ChrisR_522 or jax1488? And now programmr? jax1488 was asking the same question in this thread, but he thanked me for answering ChrisR_522’s question in this thread. No biggie, but multiple names and duplicate questions are confusing at best and wastes time for sure.
Tom Line’s answer here is a good starting point. Wrap that if block inside another if that checks for the trigger. Follow each true/false branch by yourself (make a flowchart if needed) to make sure it works.
And tell us all why your robot is doing this – I think I speak for everybody here that we’re curious what the robot is going to do.
It’s me using two members’ names because i didnt have my own… sorry if i confused you. I’d like to thank you for the code given above. Theres only one thing though, it says syntax error…
void code()
{ IT SAYS THIS IS AN ERROR!!!
static byte loops = 0; // which loop are we on?
Do you know what could possibly be the problem? Thank You.
try
void code (void)
hey sorry to keep bothering you guys, but i still says theres a syntax error at that same spot.
It would be very helpful to know what the error is…in fact, you should ALWAYS post the error.
Looks like, though, you might have a function without a declaration. So open up a header file (your team’s, if you have your own library, but user_routines.h will work) and add in:
void code(void);
Or whatever you decide to use for the function’s name.
this is the error i get:
“G:\2005 est pulse\user_routines.c:231:Error: syntax error”
Ah yes, the ever helpful “syntax error”…
Try replacing “byte” with “unsigned char”. They mean the same thing.
I don’t know if byte works with MPLAB.
Also, if you get a syntax error on the last line of a file, make sure to put an extra blank line at the end of the code.
Oh. Hehe. byte… yea, that’s supposed to be a char. (VB6 vs. C…)
IT STILL SAYS : “G:\2005 est pulse\user_routines.c:232:Error: syntax error”
void code()
{ THIS IS LINE 232 HELP PLEASE…
static unsigned char loops = 0; // which loop are we on?
if (p3_sw_trig != 0)
{
loops++; // add 1 to the loop counter
if (loops <= 44) {
pwm01 = 127; // motor off
} else if (loops <= 88) {
pwm01 = 0; // motor on
} else {
loops = 0; // go back to 0
}
} else {
pwm01 = 127; // switch not on, motor is off!
}
}
Someone please help me try to get this issue resolved. Thank You
You can’t have two else statements like that.
It’s
if
else if
else
If you need it longer, it’s
if
else if
else if
else if
else
Look for a missing semicolon or close brace on the line immediately before what you’ve shown us. If you don’t see anything wrong, consider attaching the entire user_routines.c file so we can look through it.
THANKS TO ALL FOR HELPING ME! YOU GUYS ARE GREAT!
Look at the code again.
It’s lost its indentation, so it’s hard to see, but the code is perfectly fine.
The second else belongs to the first of two if’s.
I’m guessing the problem is not in line 232, but somewhere before that line.
The compiler was expecting something like a semicolon at the end of a previous line, but didn’t complain about it until it found the start brace in line 232, which it wasn’t expecting.
Look over the code just before “void code()”.
THANK YOU TO ALL WHO HELPED ME… IT WORKS NOW!!! THANK YOU AGAIN!