Wednesday, February 6, 2013
Beginning Arduino: Control LED
Arduino is quite famous not as a open source hardware. One basic example here:
LED blinking
int led = 13;
void setup() {
// initialization
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}
Basically the idea is delay(1000) which is one second and turn the pin led to High/low
If add a user to control this:
int led = 13;
int keyF = 11; // user button switch ==> faster
int keyS = 12; // user button switch ==> slower
#define MAX_SELECT 4
int tperiod[MAX_SELECT] = {1000, 500, 250, 125}; // blink rates: 1/2sec,1/sec,2/sec,4/sec
int tselect;
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(keyF, INPUT);
pinMode(keyS, INPUT);
tselect = 1;
}
void loop() {
if (ledState == HIGH)
ledState = LOW;
else
ledState = HIGH;
digitalWrite(led,ledState);
delay(1000);
if (readUserKeyF()) tselect = blinkFaster();
if (readUserKeyS()) tselect = blinkSlower();
}
int blinkFaster(void) {
if (tselect < MAX_SELECT-1)
return (tselect+1);
else
return tselect;
}
int blinkSlower(void) {
if (tselect > 0)
return (tselect - 1);
else
return tselect;
}
int readUserKeyF(void) {
if (digitalRead(keyF) == LOW) return 1;
else return 0;
}
int readUserKeyS(void) {
if (digitalRead(keyS) == LOW) return 1;
else return 0;
}
Line connection as:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment