OK, so I’ve gone through the first three tutorials and had a bit of a play with the UNO. It’s pretty easy for the first three lessons … and I anticipate that the remaining lessons will be similarly easy.
Lesson 1 – Blink..
Here we are hooking up the UNO to a 470 ohm resistor and LED and passing HIGH/LOW instructions to pin 13 through native functional loop.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
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(1500); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1500); // wait for a second
}
The code is pretty simple. Initialise the variable “led” as an integer and assign the value 13. This is the pin-out on the UNO that we connect the LED to.
setup then sets the pinMode of led (13) to be OUTPUT … that is, to receive signals.
The loop function sends a HIGH instruction to pin 13, waits 1500 milliseconds (1 and a half seconds) and then sends a LOW instruction to pin 13, waits another 1500 milliseconds and repeats.
Lesson 2 – Light Chaser
The breadboard is populated with 8 LED and 8 470 ohm resistors. The LED are connected to pins 6 through to 13 on the UNO and the ground rail of the breadboard is connected to the GND pin of the UNO.
The sketch simply creates an array containing the pin locations of each of the LED to be turned on and off in sequence within a for loop. Initially, the array contains each LED once and two for loops drive the sequence, one incrementing the pin, the second decrementing the pin.
The final version of the sketch has the array containing the entire increment/decrement sequence and a single for loop that does the driving.
int ledCount = 15;
int ledPins[] = {6, 7, 8, 9, 10, 11, 12, 13, 12, 11, 10, 9, 8, 7};
int ledDelay = 75;
void setup() {
for(int thisLed = 0; thisLed < ledCount; thisLed++){
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
for (int thisLed = 0; thisLed < ledCount; thisLed++){
digitalWrite(ledPins[thisLed], HIGH);
delay(ledDelay);
digitalWrite(ledPins[thisLed],LOW);
}
/*
for (int thisLed = ledCount-1; thisLed > 0; thisLed--){
digitalWrite(ledPins[thisLed], HIGH);
delay(ledDelay);
digitalWrite(ledPins[thisLed],LOW);
}*/
}
Lesson 3 – Momentary Switch
Lesson 3 adds a momentary switch with a 10k ohm resistor into the breadboard and some instructions in the sketch to handle the button press.
int ledCount = 14;
int ledPins[] = {6, 7, 8, 9, 10, 11, 12, 13, 12, 11, 10, 9, 8, 7};
int ledDelay = 75;
int buttonPin = 2;
void setup() {
for(int thisLed = 0; thisLed < ledCount; thisLed++){
pinMode(ledPins[thisLed], OUTPUT);
}
pinMode(buttonPin, INPUT);
}
void loop() {
for (int thisLed = 0; thisLed < ledCount; thisLed++){
digitalWrite(ledPins[thisLed], HIGH);
while(digitalRead(buttonPin) == HIGH) {
delay(10);
}
delay(ledDelay);
digitalWrite(ledPins[thisLed],LOW);
}
/*
for (int thisLed = ledCount-1; thisLed > 0; thisLed--){
digitalWrite(ledPins[thisLed], HIGH);
delay(ledDelay);
digitalWrite(ledPins[thisLed],LOW);
}*/
}
No comments:
Post a Comment