|
For Christmas Im setting up a LEGO train on top of a narrow wall, so it has to
operate like a shuttle -- stop at each end and reverse motor direction. My first
attempt had a train regulator attached to a polarity switch, with the switch
being turned by a stepper motor I rigged up controlled by an RCX. When the train
reached a station it would hit a touch sensor and the RCX would pulse the
stepper motor, reversing the track polarity. This version works as a prototype,
but I dont have enough cords to reach the touch sensors I would have put at
each station in the full layout.
So now Im going to power the train motor directly from an RCX that rides along
as one of the cars. Ill use light sensors on each end of the train to look for
white tiles placed between the track rails at each station.
Heres my current program, which still uses touch sensors.
#define STATION1 SENSOR_1
#define STATION2 SENSOR_3
#define MOTOR OUT_B
#define __NOTETIME 8
#define __WAITTIME 10
task main()
{
#ifdef __RCX
SetSensor(STATION1, SENSOR_TOUCH);
SetSensor(STATION2, SENSOR_TOUCH);
// Only send a pulse of power to the motor
SetPower(MOTOR, OUT_HALF);
#endif
int count = 0;
OnFwd(MOTOR);
while(true)
{
if(STATION1 || STATION2)
{
reverseTrain();
count++;
if(count % 10 == 0) // Play a song every ten station stops
start playSong;
}
}
}
void reverseTrain()
{
Float(MOTOR);
Wait(100); // 1 second
Toggle(MOTOR);
On(MOTOR);
}
task playSong() // Jingle Bells
{
PlayTone(330,4*__NOTETIME);
Wait(4*__WAITTIME);
PlayTone(330,4*__NOTETIME);
Wait(4*__WAITTIME);
PlayTone(330,8*__NOTETIME);
Wait(8*__WAITTIME);
PlayTone(330,4*__NOTETIME);
Wait(4*__WAITTIME);
PlayTone(330,4*__NOTETIME);
Wait(4*__WAITTIME);
PlayTone(330,8*__NOTETIME);
Wait(8*__WAITTIME);
PlayTone(330,4*__NOTETIME);
Wait(4*__WAITTIME);
PlayTone(392,4*__NOTETIME);
Wait(4*__WAITTIME);
PlayTone(262,6*__NOTETIME);
Wait(6*__WAITTIME);
PlayTone(294,2*__NOTETIME);
Wait(2*__WAITTIME);
PlayTone(330,16*__NOTETIME);
Wait(16*__WAITTIME);
// I removed the rest of the song to save space
}
|
|
It works fine as it is, but I want to use light sensors. The quick way is to
adjust the if-statement to look at sensor percentage values, but Id like to
learn to use RCX events while Im at it.
Heres a new version of main(), but its untested.
#define STATION1 SENSOR_1
#define STATION2 SENSOR_3
#define MOTOR OUT_B
#define __NOTETIME 8
#define __WAITTIME 10
#define STATION 0 // event: reach a station
#define LOWERLIMIT 50
#define UPPERLIMIT 80
task main()
{
#ifdef __RCX
SetSensor(STATION1, SENSOR_LIGHT);
SetSensor(STATION2, SENSOR_LIGHT);
// Only send a pulse of power to the motor
SetPower(MOTOR, OUT_HALF);
SetEvent(STATION, STATION1 + STATION2, EVENT_TYPE_HIGH);
SetLowerLimit(STATION, LOWERLIMIT);
SetUpperLimit(STATION, UPPERLIMIT);
#endif
int count = 0;
OnFwd(MOTOR);
monitor(EVENT_MASK(STATION))
{
while(true) {}
}
catch
{
reverseTrain();
count++;
if(count % 10 == 0) // Play a song every ten station stops
start playSong;
}
}
|
|
Is this going to work? Also, is there anything better to put in the monitor
block other than a while-loop that does nothing?
Thanks!
|
|
Message has 3 Replies:
16 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
Active threads in Robotics
|
|
|
|