You are on page 1of 2

/*

Analog Read Serial


Reads an analog input on pin 0, prints
the result to the serial monitor.
Attach the center pin of a potentiometer
to pin A0, and the outside pins to +5V and
ground.
This example code is in the public
domain.
*/
// the setup routine runs once when you
press reset:
void setup() {
// initialize serial communication at 9600
bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over
again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1);
// delay in between reads
for stability
}
/* Blink without Delay
// constants won't change. Used here to
set a pin number :
const int ledPin = 13;
// the number of
the LED pin
// Variables will change :
int ledState = LOW;
used to set the LED

// ledState

// Generally, you should use "unsigned


long" for variables that hold time
// The value will quickly become too large
for an int to store
unsigned long previousMillis = 0;
//
will store last time LED was updated
// constants won't change :
const long interval = 1000;
//
interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}

void loop()
{
// here is where you'd put code that
needs to be running all the time.
// check to see if it's time to blink the
LED; that is, if the
// difference between the current time
and last time you blinked
// the LED is bigger than the interval at
which you want to
// blink the LED.
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >=
interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and viceversa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the
variable:
digitalWrite(ledPin, ledState);
}
}

/*
Arrays
Demonstrates the use of an array to hold
pin numbers
in order to iterate over the pins in a
sequence.
Lights multiple LEDs in sequence, then in
reverse.
Unlike the For Loop tutorial, where the
pins have to be
contiguous, here the pins can be in any
random order.
The circuit:
* LEDs from pins 2 through 7 to ground
created 2006
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Array
*/
int timer = 100;
// The higher the
number, the slower the timing.
int ledPins[] = {
2, 7, 4, 6, 5, 3
};
// an array of pin numbers to which
LEDs are attached

int pinCount = 6;
// the number of
pins (i.e. the length of the array)
void setup() {
// the array elements are numbered from
0 to (pinCount - 1).
// use a for loop to initialize each pin as
an output:
for (int thisPin = 0; thisPin < pinCount;
thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 0; thisPin < pinCount;
thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = pinCount - 1; thisPin >=
0; thisPin--) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}

You might also like