David Klein
Arduino board

My First Arduino Sketch

08 June 2009

Check out the Arduino. It’s pretty cool.

A “sketch” is a little program. This sketch turns on and off the LED when you push the button. Simple but still entertaining.

#define LED 13 // LED pin
#define BUTTON 7 // Button pin
int buttonVal = 0;
int ledVal = 0;

void setup() {
  pinMode(LED, OUTPUT); // LED is an ouput
  pinMode(BUTTON, INPUT); // button is an input
}

void loop() {
  buttonVal = digitalRead(BUTTON); // store button state
  ledVal = digitalRead(LED); // store LED state
  if (buttonVal == HIGH && ledVal == HIGH) {
    digitalWrite(LED, LOW); // turn LED off
    delay(500);
  }

  if (buttonVal == HIGH && ledVal == LOW) {
    digitalWrite(LED, HIGH); // turn LED on
    delay(500);
  }
}
     
← back to journal