Friday, September 24, 2010

Public and Pinball

I wonder if I should make this blog public.
Anyway, I built a pinball machine a while ago, before my NXT Offroader or my latest dragon. It was inspired by this pinball machine on NXTlog (this links to the secondary project for my inspiration project, for some reason, the main one is gone). It took a long time but turned out really well. Pictures are worth a thousand words, so here's an image of it. Its features included manual flippers, lost ball detector, ball launcher, two scoring methods and lights. I decided to try putting Lego brick elements on their sides to make the playing field. I used many Duplo elements for the board, and the originally liked to break because they didn't connect firmly enough. I put a brace on the bottom of the machine and that worked very well to hold the part of the machine where the ball rolls together. See it in the second picture here. However, the top of the machine, the "walls" nearer to the NXT Intelligent Brick, were very weak and often fell apart. Its first scoring mechanism was the touch sensor and bumper at the top-right of the machine, and the second was the light sensor "box" on the left side of the machine. When one of the sensors that detect scoring was detected, the NXT sent messages to the Power Functions battery box to flash the lights near the corresponding scoring zone. The sensor that detected the ball falling down past the flippers was a RCX light sensor, the idea for which I took from a Robotics Invention System book. I programmed this machine in Not eXactly C...It took hours, but it ended up working pretty well. The program will be at the bottom of this post. Bye!






/* Not eXactly C Code for NXT Ultra Pinball Machine III.

Idea for putting this NXC program in the NXT-G information box from m1n1f1g. Thanks.

Coded by NatoNX. ><> */

#define lights1on HTPFSingleOutputPWM(S4, PF_CHANNEL_1, PF_OUT_A, PF_PWM_FWD7) //define macros
#define lights1off HTPFSingleOutputPWM(S4, PF_CHANNEL_1, PF_OUT_A, PF_PWM_FLOAT)
#define lights2on HTPFSingleOutputPWM(S4, PF_CHANNEL_1, PF_OUT_B, PF_PWM_FWD7)
#define lights2off HTPFSingleOutputPWM(S4, PF_CHANNEL_1, PF_OUT_B, PF_PWM_FLOAT)

int balls; //initiate variables
int score;
int threshold1;
int threshold2;
string display;

task main ()
{
SetSensorTouch(IN_1); //set sensors
SetSensorType(IN_2, IN_TYPE_REFLECTION);
SetSensorLight(IN_3);
SetSensorLowspeed(IN_4);
while (true)
{
OnFwd(OUT_BC, 100); //light up lights powered off of NXT
ClearScreen();
TextOut(20, LCD_LINE2, "NXT Ultra"); //beginning screen
TextOut(5, LCD_LINE3, "Pinball Machine");
TextOut(36, LCD_LINE4, "III");
TextOut(15, LCD_LINE6, "Press Orange");
TextOut(24, LCD_LINE7, "to Start");
while (ButtonPressed(BTNCENTER, true) == TRUE) //wait for orange button to be pressed...
{
}
while (ButtonPressed(BTNCENTER, true) == FALSE) //...and released
{
}
ClearScreen();
TextOut(0, LCD_LINE1, "Use the flippers"); //instructions
TextOut(0, LCD_LINE2, "to get the ball");
TextOut(0, LCD_LINE3, "to hit the black");
TextOut(0, LCD_LINE4, "part at the top");
TextOut(0, LCD_LINE5, "and into the box");
TextOut(0, LCD_LINE6, "on the left. You");
TextOut(0, LCD_LINE7, "have 3 launches.");
TextOut(0, LCD_LINE8, "Press orange.....");
while (ButtonPressed(BTNCENTER, true) == TRUE) //wait for orange button to be pressed...
{
}
while (ButtonPressed(BTNCENTER, true) == FALSE) //...and released
{
}
ClearScreen();
threshold1 = Sensor(IN_3) + 3; //sets thresholds for light sensors
threshold2 = Sensor(IN_2) - 50;
balls = 2;
score = 0;
while (balls > -1)
{
display = "Score: "; //score and ball display
strcat(display, NumToStr(score));
TextOut(0, LCD_LINE2, display);
display = "Balls Left: ";
strcat(display, NumToStr(balls));
TextOut(0, LCD_LINE5, display);
if (Sensor(IN_1)) //if bumper is pressed, add to score...
{
score += 100;
PlayTone(440, 500);
lights1on; //...and flash touch sensor lights
Wait(200);
lights1off;
}
if (Sensor(IN_3) > threshold1) //if score light sensor detects something, add to score...
{
score += 500;
PlayTone(640, 500);
lights2on; //...and flash light sensor lights
Wait(200);
lights2off;
}
if (Sensor(IN_2) < threshold2) //if drain light sensor detects something...
{
balls -= 1; //subtracts from ball count...
PlayFile("! Attention.rso"); //...and plays a sound
Wait(1000);
}
}
ClearScreen(); //if ball count reaches -1, the the game ends. Screen clears
PlayFile("! Startup.rso"); //ending display and sequence
TextOut(17, LCD_LINE2, "Final Score");
TextOut(0, LCD_LINE4, NumToStr(score));
TextOut(23, LCD_LINE6, "Thanks for");
TextOut(0, LCD_LINE7, "playing! Please");
TextOut(12, LCD_LINE8, "press orange.");
while (ButtonPressed(BTNCENTER, true) == FALSE)
{
}
}
}