----AJ----
Junior Member level 1
- Joined
- Mar 23, 2013
- Messages
- 15
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,401
Hi Guys,
I'm an absolute beginner so I've set myself a little task as follows:
I'm trying to start and stop a motor using a start button, a position sensor, and also incorporating a selector switch which will select either 1 rotation or 3 depending on its position i would also like the start button to override the 1 & 3 rotations if it still in the on position at the end of the cycle.
So in other words the 3 functions I'm trying to achieve are:
Selector switch = 1
Start button = 0,1,0
Motor Starts
Position Sensor = 0,1,0
Motor stops
Selector switch = 3
Start button = 0,1,0
Motor Starts
Position Sensor = 0,1,0,1,0,1,0
Motor Stops
Selector switch = 3
Start button = 1
Motor starts
position sensor = 10101010101010101010101010101................ etc etc
start button = 0
position sensor = 0,1,0
Motor Stops
This is the code i've written and all is well except for the fact that when I'm in 3 shot mode. So when the start button is continusly on it is overriding the count of 3 which is fine, but when its released the position sensor is still counting another 3 revolutions before stoping the motor
Here is whats happening:
Start Button = 1
Motor Starts
position sensor = 01010101010101010101............ etc etc
start button = 0
position sensor = 0101010 (this is the problem, its still counting 3 before stopping the motor, i want it to just count 1)
motor stops
I know its probably a really simple thing but i'm nearly pulling my hair out :?: any help would be appreciated.
I'm an absolute beginner so I've set myself a little task as follows:
I'm trying to start and stop a motor using a start button, a position sensor, and also incorporating a selector switch which will select either 1 rotation or 3 depending on its position i would also like the start button to override the 1 & 3 rotations if it still in the on position at the end of the cycle.
So in other words the 3 functions I'm trying to achieve are:
Selector switch = 1
Start button = 0,1,0
Motor Starts
Position Sensor = 0,1,0
Motor stops
Selector switch = 3
Start button = 0,1,0
Motor Starts
Position Sensor = 0,1,0,1,0,1,0
Motor Stops
Selector switch = 3
Start button = 1
Motor starts
position sensor = 10101010101010101010101010101................ etc etc
start button = 0
position sensor = 0,1,0
Motor Stops
This is the code i've written and all is well except for the fact that when I'm in 3 shot mode. So when the start button is continusly on it is overriding the count of 3 which is fine, but when its released the position sensor is still counting another 3 revolutions before stoping the motor
Here is whats happening:
Start Button = 1
Motor Starts
position sensor = 01010101010101010101............ etc etc
start button = 0
position sensor = 0101010 (this is the problem, its still counting 3 before stopping the motor, i want it to just count 1)
motor stops
I know its probably a really simple thing but i'm nearly pulling my hair out :?: any help would be appreciated.
Code:
unsigned char shots = 0;
void fire (void){
GPIO.B0 = 1; // Start the motor
TMR0 = 0; // Reset the counter (Timer0)
}
void main(void){
TRISIO = 0b0010110; // B1 = Trigger Input switch, B2 = Counter input (Timer0), B4 = Shots Selector Switch
CMCON0 = 7;
ANSEL = 0;
ADCON0 = 0;
OPTION_REG = 0b10111000;
TMR0 = 0;
GPIO.B0 = 0;
while (1){
if (GPIO.B4 == 0) shots = 3; else shots = 1; // select the number of counts before switch off motor
if (TMR0 == shots) GPIO.B0=0; // when counter = shots switch off motor
if (GPIO.B1 == 1) fire(); // trigger
}
}
Last edited: