Tuesday, 23 April 2013

Arduino Lesson 1 - The Very Basics

Please note that I still have not finished my Copter game. I think I need to ask Mr Patel or Mr Hussain for help as an important part of the game doesn't seem to work...

OK, from now on I'm going to post on my blog more frequently...

So, last lesson a few lessons ago we looked at the very basics of Arduino programming. We learnt how to make the simplest program you can write, and then we ran out of time and didn't do anything else.

Below is an incredibly basic Arduino program (mm-hmm, I did write it all from scratch...):

PLEASE CORRECT ANYTHING HIGHLIGHTED IN RED (OR NOT, FOR THAT MATTER) IF YOU KNOW IT'S INCORRECT!

Begin code:
/*
 * Basic stuff, init!
 */

int ledPin = 13; // "ledPin" is a variable that stores the number 13 - pin 13 on an Arduino Uno is connected to a built-in LED

void setup() {
/* This is the method/function which contains all the preliminary
 * setup code, e.g. defining the pins that are used in the
 * program.
 */

  digitalWrite(ledPin, OUTPUT);
  /* The "digitalWrite" method/function is used to tell the Arduino
   * that the first value in the brackets (in this case ledPin,
   * i.e. 13) is the number of a pin which is used in the program.
   * The second parameter (value in the brackets) tells the Arduino
   * whether we want to control current flow through a pin or we
   * want to test if there is current flowing through a pin.
   */

}

void loop() {
/* This is the method/function which runs over and over again -
 * it loops and loops and loops and keeps looping until the
 * program is ended/terminated.
 */

  pinMode(ledPin, HIGH);
  /* This translates to "turn pin 13 (ledPin) HIGH" - run current
   * through it. This will turn on the built-in LED, which is
   * connected to pin 13.
   */

  delay(500);
  /* Wait 500 milliseconds (half a second) before running the
   * next line of code:
   */

  pinMode(ledPin, LOW);
  /* Yep, you guessed it - this turns the LED OFF. So this
   * program will cause the LED to flash on and off once every
   * second.
   */

  delay(500);
  /* Wait 500 milliseconds before re-running loop() and thereby
   * turning the LED on.
   */

}

End code.

No comments:

Post a Comment