231a-ab Lab 4

From CSclasswiki
Jump to: navigation, search

Simple Arduino Sketches

Blink

/*
 * MyBlink
 *
 * The basic Arduino example.  Turns on an LED on for 700 msec,
 * then off for 300 msec, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 13;                // LED connected to digital pin 13

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(700);                   // waits for 700 msec
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(300);                   // waits for 300 msec
}


Hello World

HelloWorld.jpg


Math

/*
 * Math is fun!
 *
 * http://www.ladyada.net/learn/arduino/lesson4.html
 */

int a = 5.25;
int b = 10.5;
int c = 20.75;

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  Serial.println("Here is some math: ");

  Serial.print("a = ");
  Serial.println(a);
  Serial.print("b = ");
  Serial.println(b);
  Serial.print("c = ");
  Serial.println(c);

  Serial.print("a + b = ");       // add
  Serial.println(a + b);

  Serial.print("a * c = ");       // multiply
  Serial.println(a * c);
  
  Serial.print("c / b = ");       // divide
  Serial.println(c / b);
  
  Serial.print("b - c = ");       // subtract
  Serial.println(b - c);
}

void loop()                     // we need this to be here even though its empty
{
}

Navigation

  • Click here to go back to main page.