Thursday, March 11, 2010

LED project...


My plan for the LED project was to create an electronic dice. I found an example on the internet and tryed to base mine off that. To get the dice to act realisticly, the lights formation would have to be randomized. There was a page on the arduino website which was really useful = http://www.arduino.cc/en/Reference/Random Then i worked out the formation and how it would work. The LEDs would be grouped so that when the random number was called, each LED would not need to be callled individually. The groups were 1, 2, 3 & 4. Code:
int pinLeds1 = 10; //2 corner LEDs
int pinLeds2 = 9; //2 outer middle LEDs
int pinLeds3 = 8; //Other 2 corner LEDs
int pinLed4 = 7; //middle LED
int buttonPin = 6; //Button input reader
int buttonState; //On or off (HIGH/LOW)
int rGen; //random generator variable
int time = 4000; //hold for 4 seconds.

void setup () {
pinMode (pinLeds1, OUTPUT);
pinMode (pinLeds2, OUTPUT);
pinMode (pinLeds3, OUTPUT);
pinMode (pinLed4, OUTPUT);
pinMode (buttonPin, INPUT);
randomSeed(analogRead(0));
}

void loop() {
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH){ //if the button has been pressed
rGen = random(1, 7); //rGen=random number between 1 and 6.

if (rGen == 1){ //light up centre light.
digitalWrite (pinLed4, HIGH);
delay (time);
}

if (rGen == 2){ //Light up 2 corner lights.
digitalWrite (pinLeds1, HIGH); delay (time);
}
if (rGen == 3){ //Light up 2 corner and middle lights.
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLed4, HIGH);
delay (time);
}
if (rGen == 4){ //Light up all 4 corners.
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
delay (time);
}

if (rGen == 5){ //Light up all 4 corners plus middle light.
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds3, HIGH);
digitalWrite (pinLed4, HIGH);
delay (time);
}
if (rGen == 6){ //light up all except middle.
digitalWrite (pinLeds1, HIGH);
digitalWrite (pinLeds2, HIGH);
digitalWrite (pinLeds3, HIGH);
delay (time);
}
} //else if not pressed and time has ended turn all off.
digitalWrite (pinLeds1, LOW);
digitalWrite (pinLeds2, LOW);
digitalWrite (pinLeds3, LOW);
digitalWrite (pinLed4, LOW);
}//end.

At run time, something was not right and nothing lit up at all. Because I was basing mine on one i found on the internet, i went back and looked through what the example had. The bread board i was using was a completely different set up. The example had a ground row that all the resistors were connected too. Mine did not. After checking all the wires and LEDs were connected in the right pattern and formation, I went to check through my code. As this is very low level code it would have been easy to spot any errors, but there appeared to be none. With no other resources avalible to me, I had to show what I had created in class, even though it did not work.

After borrowing another bread board from Adam which had a ground row in it, i was able to re wire up my project and test it! My excitement faded when only a few of the LED lit up, but i quickly realised it was because several of my resistors were ones that I found lying around home, and were not the right level to allow much power to the LEDs. I replaced my ones from home with 7 X 220 ohm resistors that I got from Ashley, reset the Arduino, uploaded the code once again and this time SUCCESS!!! The LEDs light up perfectly and in the formation that was originally intended.

So my main issues for this project were: Lack of basic knowledge (this stuff is all still quite new to me), lack of correct equipment, and fiddly small components!!! (My eyes hurt so much). Other than this, I am very pleased with the final outcome.

Even if all the LEDs are green :P


No comments:

Post a Comment