Wednesday, April 20, 2011

Arduino Train Project for ECSE 421: Embedded System



So I just had the project demonstration for the ECSE 421 today, the project was a success, if it wasn't for the documentation issues, this project would have gotten 100% from the Professor. But we got penalized 10% for not stating that we have a door-lock-state, and 5% for not documenting the input and output properly.



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 board
const 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 starts
void 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 loop
void 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);
}
}
}

Thursday, June 17, 2010

My First 5V Regulated Power Supply


All thanks to Sparkfun! This website has very detailed instructions and great tutorials for beginners. After reading the tutorials for the power supply, I assembled this circuit without even need to look at the circuit diagram anymore coz the instruction is so clear I was able to remember it by heart.

Next project will be assembling my TinyUSB Programmer. And also my LCD! :D

Monday, May 10, 2010

Summer 2010

So I've been lazying around this early summer.

I shall resume my work soon, I'm still recovering/recuperating from the terrible semester I had last Winter.

So my pending projects are:

1. Arduino Motorshield from adafruit.

I've assembled this kit but still haven't test it with the bunch of motors I have right now. Reason being is that since I'm still new with Arduino, I find it REALLY hard to understand how to hack the library or maybe it's better for me to say to store the examples provided by ladyAda into the Arduino library in my computer. Tho I've figured it by now, so I shall be working on this as soon as possible.

Important Link: http://www.ladyada.net/make/mshield/

2. USBTiny AVR Programmer

Again from adafruit. So I bought the kit as one of the effort to learning how to program microcontrollers. This will be the second project following the first one. And this project will be about 1) assembling it and 2) learn how to use it.

Important Link: http://www.ladyada.net/make/usbtinyisp/

3. Learn Embedded Electronics

So this is the first kit I bought from Sparkfun Electronics. And honestly I really can't wait to learn this shit.

Important Link: http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=57

Other Stuff

Alongside these projects, since I have to improve on my programming skills, I've decided to learn PHP and MySQL as well this summer.

Cost: TIME

Now since the summer is only 4 months, which I only have 3.75 months left, I kinda have to be very wise with my time management, especially when I have a 6-credit-graded French course alongside until end of June.

God's willing, when the project is done or still in progress, I shall post up all my works progress and share the problems/difficulties I had when doing the project and how I manage to get around with it.

So when will I start working? Hurmm lemme see. I'm giving myself until this Friday to at least, HEAT UP THE SOLDERING IRON!

Sunday, December 27, 2009

LED - Interesting Observation

So today I was curious to know what's the voltage drop of LEDs with different colours.

As I made the connection, i.e the positive tip of multimeter to the positive lead of the LED and vice-versa, the LED lit up.

Astonished I was, but a moment later I realized that I just got electric shock from the connection my finger made with the close circuit.

Heh, good lesson for today.

Tomorrow's plan - GET YOUR ROBOT WORKS, WOMAN!

Saturday, December 19, 2009

Arduino Experimentation Kit from OOMLOUT

So tonight, I've officially "graduated" from all the 11 lessons from Arduino Experimental Kit from OOMLOUT.
In fact, this is the first Arduino kit I've ever had.



I found this kit is a very very good start for a beginner like me, who suddenly wakes up in the morning and decides to invest her savings to learning electronics during the xmas break.

All the instructions are very clear, with the aid of the colourful-printed-circuit diagrams. And also my technical background as an electrical engineering student helps me a lot in understanding stuff. However, there are still few hiccups along the way, for example, my temperature sensor heats up rapidly that it started to smell like smokes. It freaks the shit out of me as I thought I might have burnt my Arduino board for some reason. And the temperature recorded at my Serial Interface was almost 240 degree Celsius! The datasheet says that the maximum operation of the temperature sensor, TMP36 in this case, is only 150 degree Celcius! @_@


I dunno what had happen I'm not daring enough to redo the whole circuit again.

The second thing is that the relay is so sucky that it doesnt stick into the breadboard. You have to press it real hard to see the output of your circuit. It's a lil bit sad that I couldn't explore much about the relay coz apparently it is very interesting device, and most probably, very highly important in my future robot project. Or not.





So I totally recommend this kit for any beginner out there who wanna play with microcontrollers. Thanks to Oomlout for such a great kit! =)

First Entry!

So I'm basically done with this semester 2 days ago, hooray! =)

Now here begins my exploration for this amazing electronic world.. And the sole reason for the existence of this blog is because I read everywhere on the internet about open-sourcing, taking and giving it back to the others, and also documenting projects so that it would be beneficial to other people.

I hope my exploration in this field would benefit others in any ways. Cheers.