Documentation wasn't my responsibility in this part of the project. Since I already did all the project by myself without a single help from any of the other 6 team members, I shall say I've done my very best, and I'm happy with it.
Yes this project is supposed to be a group project of 7 people, which I find it quite ridiculous from the beginning of the semester. Too big of a group for such a small project. Obviously this group is definitely my least favourite group I've ever work with - poor communication, lack of leadership, non-participating team members - that's what you should expect from a big group working on a small project.
So to make things short, 4 days before the final demonstration, I took the responsibility to work on this project alone as there has been some problems with the initial board that we were actually planning to implement our Train project on. And because I was one lucky person who happen to not have any exam on that week.
And I came up with this very simple project, implemented on the Arduino Duemilanove board which I bought 2 years ago. It's just implementing the basic train operation, move, arrive at next station, open door, close door, and start moving again.

And to meet the purpose of this course, I added 2 interrupts, real time of course, 1 is when the train is moving, and another is when the door is closing. I told you it's a simple project.
Since I do not have a physical train set, I chose to display the output of all the train activity by displaying it on Serial display, by using a serial port (USB). Every train activity status will be sent to the computer to be displayed, some LED's to indicate doors activity, alarm signal, and break signal, and last but not least, my favourite piezo speaker that beeps and make "cute" melody.
The 2 interrupts are implemented by the pushbutton. When pushbutton A is pushed, an alarm signal was sent out, and when pushbutton B is pushed when the door is closing, it means there is an obstruction that occurs.
Since I'm a loud and proud supporter of the open source community, I'm putting up my code here as part of my duties as someone who benefits greatly from this awesome community.
What I am really happy about this project is, that how it makes me realize my potential and my ability to do things beyond my comfort zone. I remember 2 years ago when I get this Arduino kit from adafruit, I was VERY VERY scared to try new things. I basically just google around and look for a project that people has done and look up people's code and just copy and modify them. I'm just scared to make mistakes. I'm so scared to do every thing by myself. I am scared that no one will help me to solve my problems.
After completing this project, I realize I am not that person anymore. And right now, I have so much in my mind of what I wanted to do. I hope this blog will be updated with more cooler projects in this summer. In mind right now: Photovoltaic project (Robot has to wait)
Last but not least, this project made me realized how every single thing that I do (or at least try) to improve myself, from attending piano class (at the age of 22) to learning programming MATLAB, it really actually helps me in many ways. For example, while coding for the piezo speaker, I was trying to figure the notes for the melody. Thank God I learned piano and I know how to search for those melody by heart. And of course, I would never remember how to use a while loop from COMP 202, if I haven't learned MATLAB basic programming over the winter break last Dec 2010.
All in all, it pays to be a nerd. Or at least to be someone who hunger for knowledge and self-improvement.
Check out the video of the project on youtube.
CODE//initializing the pins on the microprocessor boardconst int emergencyLED = 8;const int doorLED = 3;const int doorLock = 4;const int emergencyButton = 2;const int speakerPin = 11;const int obstruction = 13;int closeTime = 0;//setting up the type of pins//initialize the red LED (door locked) ON when project startsvoid setup(){Serial.begin(9600);pinMode(obstruction,INPUT);pinMode(speakerPin,OUTPUT);pinMode(doorLED,OUTPUT);pinMode(doorLock,OUTPUT);pinMode(emergencyLED,OUTPUT);pinMode(emergencyButton,INPUT);digitalWrite(doorLock,HIGH);}//This is the nasty infinite loopvoid loop(){train_move();next_station();door_opened();close_door();doorClosed();}void doorClosed(){Serial.println("******************************"); Serial.println("Doors are now closed");Serial.println("******************************"); digitalWrite(doorLock, HIGH);digitalWrite(doorLED, LOW);delay(1500);}void train_move(){Serial.println("******************************"); Serial.println("The train is moving");Serial.println("******************************"); delay(700);int emerg = 0;int count = 0;int reset = 0;digitalWrite(doorLock, HIGH);while(emerg == 0){count = count +1;Serial.println(count);if (digitalRead(emergencyButton) == LOW){reset =1;emergencyOn();while(reset == 1){if (digitalRead(emergencyButton) == LOW){emergencyOff();reset =0;}}}else if (count >999){emerg =1;}}emerg =0;}emergencyOn(){digitalWrite(emergencyLED,HIGH); Serial.println("******************************"); Serial.println("Emergency Button Activated");Serial.println("******************************"); delay(500);train_stop();Serial.println("******************************************* *********"); Serial.println("Please be patient while we are fixing the problems.");Serial.println("******************************************* *********"); }emergencyOff(){Serial.println("******************************"); Serial.println("Problems have been fixed.");Serial.println("******************************"); digitalWrite(emergencyLED,LOW); delay(1000);Serial.println("******************************"); Serial.println("The train is moving");Serial.println("******************************"); delay(1000);reset =0;}void train_stop(){Serial.println("******************************"); Serial.println("The train is stopped");Serial.println("******************************"); digitalWrite(doorLock,HIGH);delay(500);}void next_station(){Serial.println("******************************"); Serial.println("Train arrived at next station");Serial.println("******************************"); digitalWrite(doorLock,HIGH);delay(1500);}void door_opened(){Serial.println("******************************"); Serial.println("Doors are opened");Serial.println("******************************"); digitalWrite(doorLED,HIGH);digitalWrite(doorLock,LOW);delay(2000);}void close_door(){Serial.println("******************************"); Serial.println("Doors are closing");Serial.println("******************************"); delay(1000);closeTime =0;int block =0;while(block==0){doorSound();Serial.println(closeTime);closeTime = closeTime +1;if (digitalRead(obstruction) == LOW){Serial.println("Doors are blocked!");block =2;}else if (closeTime > 6){block =1;}}if (block == 2){announce();digitalWrite(doorLock, LOW);digitalWrite(doorLED, HIGH);delay(500);close_door();}block =0;}void announce(){int length = 9;char notes[] = "bgae eabg";int beats[] = {1,1,1,1,1,1,1,1,1};int tempo = 350;for (int i =0; i{if (notes[i] == ' '){delay(beats[i]*tempo);//rest}else{playNote(notes[i], beats[i]*tempo);delay(tempo/2);}}Serial.println("*****************************************" ); Serial.println("Please do not block the door. Thank you.");Serial.println("*****************************************" ); delay(500);}void doorSound(){int length = 1;char notes[] = "c";int beats[] = {1};int tempo = 400;for (int i =0; i{playNote(notes[i], beats[i]*tempo);delay(tempo/2);}}void playTone(int Tone, int duration){for (long i =0; i {digitalWrite(speakerPin, HIGH);delayMicroseconds(Tone);digitalWrite(speakerPin, LOW);delayMicroseconds(Tone);}}void playNote(char Note, int duration){char names[] = {'c','d','e','g','a','b'};int tones[] = {1915, 1700, 1519, 1275, 1136, 1014};for (int i =0; i<8; i++){if (names[i]==Note){playTone(tones[i], duration);}}}