|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Program a motor to turn on and off every other second.....
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.
|
|
#2
|
|||
|
|||
|
Re: Program a motor to turn on and off every other second.....
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 Code:
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!
}
}
Last edited by GRS : 28-01-2008 at 13:17. |
|
#3
|
|||
|
|||
|
Re: Program a motor to turn on and off every other second.....
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. |
|
#4
|
||||
|
||||
|
Re: Program a motor to turn on and off every other second.....
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. |
|
#5
|
|||
|
|||
|
Re: Program a motor to turn on and off every other second.....
try
void code (void) |
|
#6
|
||||
|
||||
|
Re: Program a motor to turn on and off every other second.....
hey sorry to keep bothering you guys, but i still says theres a syntax error at that same spot.
|
|
#7
|
||||
|
||||
|
Re: Program a motor to turn on and off every other second.....
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. |
|
#8
|
||||
|
||||
|
Re: Program a motor to turn on and off every other second.....
this is the error i get:
"G:\2005\test pulse\user_routines.c:231:Error: syntax error" |
|
#9
|
|||
|
|||
|
Re: Program a motor to turn on and off every other second.....
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. |
|
#10
|
|||
|
|||
|
Re: Program a motor to turn on and off every other second.....
Oh. Hehe. byte.. yea, that's supposed to be a char. (VB6 vs. C...)
|
|
#11
|
||||
|
||||
|
Re: Program a motor to turn on and off every other second.....
IT STILL SAYS : "G:\2005\test 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 |
|
#12
|
||||
|
||||
|
Re: Program a motor to turn on and off every other second.....
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 |
|
#13
|
|||||
|
|||||
|
Re: Program a motor to turn on and off every other second.....
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.
|
|
#14
|
||||
|
||||
|
Re: Program a motor to turn on and off every other second.....
THANKS TO ALL FOR HELPING ME! YOU GUYS ARE GREAT!
|
|
#15
|
|||
|
|||
|
Re: Program a motor to turn on and off every other second.....
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()". |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Turn off outlining? | Justin M. | Inventor | 11 | 01-02-2007 09:16 |
| How fast does the Solenoids turn on and off? | Generalx5 | Robotics Education and Curriculum | 2 | 26-01-2007 01:09 |
| Anyone know how fast the Solenoids on the MINI RC can turn on and off? | Generalx5 | Control System | 1 | 06-01-2007 07:39 |
| Program machine to turn and stop | CharlieWilken | Programming | 1 | 19-02-2004 12:50 |
| hanging off off other robots that are hanging off the horiz bar | Andrew Dahl | Rules/Strategy | 15 | 12-01-2004 19:14 |