So this is the most basic thing that I could find for the Arduino. It is actually a useful tool because as you go on to more complex things, the time will come when you are wondering, "Did I just burn out my board? How can I find out if it is evening working???" I have used this code to do just that. Trouble shoot an issue and figure out if the Arduino and Computer are even communicating. This first bit of codec came directly from the book.
// Example 01 : Blinking LED from the Arduino Book
#define LED 13 // LED connected to
// digital pin 13
void setup()
{
pinMode(LED, OUTPUT); // sets the digital
// pin as output
}
void loop()
{
digitalWrite(LED, HIGH); // turns the LED on
delay(1000); // waits for a second
digitalWrite(LED, LOW); // turns the LED off
delay(1000); // waits for a second
}
-----------------------------------------
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This code was found in the Arduino Software
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
What I find interesting is the differences. One uses int led = 13. The other uses define LED 13. I am still not sure what the difference is but I am sure that it will become more important later, and hopefully I will begin to get it.But what can you do with it. I can make it blink fast and slow and make it stay on or blink in uneven pattern. I can also put a few lights there and then alternate them. It is kind of fun.
So the code for making a two led blink pattern, I just used cut and paste to add another LED on pin 12. Otherwise, the code is the same as above.
// 2 Blinking LED's - Modified by Philip Leete
#define First_Led 12 //First_Led is connected to pin 12
#define Second_Led 13 //Second_LED is connected to pin 13
void setup()
{
pinMode(First_Led, OUTPUT); //This sets both leds as outputs
pinMode(Second_Led, OUTPUT);
}
void loop()
{
digitalWrite(First_Led, HIGH); // turns the First LED on
delay(100); // waits for the given time, in miliseconds
digitalWrite(First_Led, LOW); // turns the First LED off
delay(100);
digitalWrite(Second_Led, HIGH); // turns the Second LED on
delay(100);
digitalWrite(Second_Led, LOW); // turns the Second LED off
delay(100);
}
No comments:
Post a Comment