This the original Blink 2 program (Task 11):
int ledPin13 = 13;
int ledPin12 = 12;
int del = 500;
void setup() {
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
}
void loop()
{
digitalWrite(ledPin13, HIGH);
digitalWrite(ledPin12, LOW);
delay(del);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin12, HIGH);
delay(del);
}
This is a variation where one LED blinks when the other one is off (Task 12):
int ledPin13 = 13;
int ledPin12 = 12;
int del = 500;
void setup() {
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
}
void loop()
{
digitalWrite(ledPin12, LOW);
delay(del);
digitalWrite(ledPin13, LOW);
delay(del);
digitalWrite(ledPin13, HIGH);
delay(del);
digitalWrite(ledPin13, LOW);
delay(del);
digitalWrite(ledPin12, LOW);
delay(del);
digitalWrite(ledPin12, HIGH);
delay(del);
}
This is a variation where one LED is mostly on, and the other mostly off (Task 13):
int ledPin13 = 13;
int ledPin12 = 12;
int del = 1000;
void setup() {
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
}
void loop()
{
digitalWrite(ledPin13, HIGH);
digitalWrite(ledPin12, LOW);
delay(del);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin12, HIGH);
delay(del/10);
}
This variation is like the last, but the LEDs are reversed (Task 14):
int ledPin13 = 13;
int ledPin12 = 12;
int del = 1000;
void setup() {
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
}
void loop()
{
digitalWrite(ledPin13, HIGH);
digitalWrite(ledPin12, LOW);
delay(del/10);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin12, HIGH);
delay(del);
}
This is a veriation where one LED blinks twise, then the other one blinks once (Task 16):
int ledPin13 = 13;
int ledPin12 = 12;
int del = 500;
void setup() {
pinMode(ledPin13, OUTPUT);
pinMode(ledPin12, OUTPUT);
}
void loop()
{
digitalWrite(ledPin12, LOW);
digitalWrite(ledPin13, LOW);
delay(del);
digitalWrite(ledPin13, HIGH);
delay(del);
digitalWrite(ledPin13, LOW);
delay(del);
digitalWrite(ledPin13, HIGH);
delay(del);
digitalWrite(ledPin13, LOW);
digitalWrite(ledPin12, LOW);
delay(del);
digitalWrite(ledPin12, HIGH);
delay(del);
}
Monday, March 15, 2010
Subscribe to:
Post Comments (Atom)
Hi Tim
ReplyDeleteJust looking at code like
digitalWrite(ledPin13, LOW);
delay(del);
digitalWrite(ledPin13, HIGH);
& thinking it could be replaced by a function like
void flashUp(int ledPin)
{
digitalWrite(ledPin, LOW);
delay(del);
digitalWrite(ledPin, HIGH);
}
which goes AFTER the loop() code
in which you could then write stuff like
flashUp(ledPin13);
flashUp(ledPin12);
& if you rearranged a bit of your code you could probably improve on the function i listed here & save even more bytes.
i saved about 20% on some code i wrote, just by using functions, & they're easy as..