Skip to content
Dave Klein
Writing

URL copied to clipboard

My first Arduino sketch

Check out the Arduino here: http://www.arduino.cc. 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.

//my first awesome sketch
#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);
  }
}

Chat about this?

Send a comment