So I have been drooling over this open source micro controller called the arduino for 2 weeks, and last night I finally went out with talia and got one :) along with a whole bunch of goodies like LEDs, resistors, and transisters
I spent a good 2 hours playing around with it and getting some LEDs to turn on and pulse and stuff.
First I simply wrote a piece of code to turn on the on-board LED every second for 1 second, then turn it off for 2 seconds
Code:
// this will be the pin for the onboard led :)
int ledPin1 = 13;
void setup()
{
pinMode(ledPin1, OUTPUT);
}
void loop()
{
digitalWrite(ledPin1, HIGH); // turn the led on
delay(1000); // wait for 1S
digitalWrite(ledPin1, LOW); // turn the led off
delay(2000); // wait for 2S
}
Then I plugged in an external LED into the same pin as the on-board LED and watched it blink along with the on-board LED (no pics, sorry)
Then I wanted to plug a whole bunch of LEDs into a breadboard and blink them using a different pin than the on-board LED. This required the addition of a resister. In this test I had 3 LEDs, the first is connected to the on-board LED and the other two are connected to 2 other pins using resisters. I wanted to explore what the difference would be with different resisters so I had 1 LED connected in series with a 470 ohm resister and the other with a 4.7k resister
Code:
// this will be the pin for the onboard led :)
int ledPin1 = 13;
int LEDPin2 = 2;// this will be the led with a 470 ohm resister
int LEDPin3 = 4;// this will be the led with a 4.7k ohm resister
void setup()
{
// set the onboard led pin to an output pin
pinMode(ledPin1, OUTPUT);
pinMode(LEDPin2, OUTPUT);
pinMode(LEDPin3, OUTPUT);
}
void loop()
{
digitalWrite(ledPin1, HIGH); // turn the led on
// I want the new resister leds to blink out of phase with the onboard LED
digitalWrite(LEDPin2, LOW); // turn the led on
digitalWrite(LEDPin3, LOW); // turn the led on
delay(1000); // wait for 1S
digitalWrite(ledPin1, LOW); // turn the led off
digitalWrite(LEDPin2, HIGH); // turn the led on
digitalWrite(LEDPin3, HIGH); // turn the led on
delay(2000); // wait for 2S
}
The final thing I wanted to do was have an LED pulse its brightness. I did that using a pulse width modulated digital pin which alters its voltage based on the the value you give it.
code:
int PMWLEDPin = 3;
void setup()
{
pinMode(PMWLEDPin, OUTPUT);
}
void loop()
{
for(int i = 0;i <= 255; i = i + 10)
{
analogWrite(PMWLEDPin, i); // turn the led on
delay(10);
}
for(int i = 255;i >= 0; i = i - 10)
{
analogWrite(PMWLEDPin, i); // turn the led on
delay(10);
}
analogWrite(PMWLEDPin, 0);
delay(100);
}
I don't have pictures for that last one but it worked very nicely.
One of the things that I have noticed is that I have forgotten a LOT of my high school physics so I have to go and review all of that. If anyone can recommend a good webpage that goes over all of that basic stuff I would really appreciate it :)
As for the future, I have a couple projects in mind. I think I'm going to try and build a sentry gun from team fortress 2 :)
~(' ')~
No comments:
Post a Comment