okay so here's what I think it would be
fun to try to do. You know I've got my
program where the button turns the
motor on, and then I touch it again to
turn the motor off. I'm thinking it would
be fun to try to adjust the motor speed
while it's running with the motor is running with the
potentiometer. So that means I need to
be calling startmotor while the
motor's running repeatedly with
different motor speeds. Here's how I'm
going to tackle that. first of all I'm
going to change this untilTouch to be
to be different. I
copied the code that Ineed into my buffer earlier so
I'm going to paste it back in. I'm
basically going to replace this untiltouch(button)
with this while loop which will repeat as long as
sensorvalue[button] is equal to 0. Inside the while loop, the variable j is
set to 0 because the RobotC language won't allow an empty while loop. J is a variable and you
you have to declare your variables.
I'm going to put a comment. if I do
slash star and then end with a slash
star everything in between is a comment.
it's good practice to put comments to
explain what's going on. so I've
got this. let's just see if this program
runs, if I can turn the motor on and off
first. so I'm going to compile and
download, and what I'm hoping is that
this program runs exactly like the
previous program but if it works then
we'll be able to I control the
motor with potentiometer. so let's see.
the programs running so I'm going to
press the switch to start the motor, and press
it again turn it off. first to start it,
press it again to turn it off. so my
program is working. so now I need to stop
and what I'm going to do is inside this
while loop while I'm waiting for the
button to be pressed to stop the motor
I'm going to keep calling
startmotor over and over.
well first I'm going to declare a
variable speed. then
I'm going to come down and here I'm
going say speed is equal to sensor value[potentiometer]
to a number between
0 and 4095
I need to know what 4095 / 127 is so
4095/127 is a little over 32
so if I do
4,095 divided by 33 I get 124 that'll
be good. I don't want to tell it to go
more than 127 and then I'm going to say
startmotor(testmotor,speed) using the speed that I
just calculated. think I can delete
this and so by continually calling startmotor, it's not really restarting the
motor every time, it's just adjusting the
speed. I think this might do it so
let's give it a whirl. so if I do
I've got a variable int j that I'm not
using anymore so I'm going to get rid of
it since the compiler didn't like that. and I'm
going to download it
and I click start now. notice how
potentiometer is at 4095, so when I press
the button 4095 divided by 33 which is about
124 so I think this motor is going to be
running close to full speed. so I'm going
to press it and it's running now. if it
works I should be able to turn it down
by adjusting the potentiometer. look at
that. it's working, so I'm turning the
potentiometer and the speed is getting
really slow. hopefully you can hear that
I can probably almost stop it and it is
stopped. now I can dial it back up.
alright that's cool.
so it works. that's it so I want you to
see if you can get this program to work
yourself so I'm going to stop the video
Không có nhận xét nào:
Đăng nhận xét