You are on page 1of 22

void setup()

// put your setup code here, to run once:



}

void loop()
// put your main code here, to run repeatedly:

}

/*
Blink
Turns on an LED on Ior one second, then oII Ior one second, repeatedly.

This example code is in the public domain.
*/

void setup()
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}

void loop()
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait Ior a second
digitalWrite(13, LOW); // set the LED oII
delay(1000); // wait Ior a second
}

/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor

This example code is in the public domain.
*/

void setup()
Serial.begin(9600);
pinMode(2, INPUT);
}

void loop()
int sensorValue digitalRead(2);
Serial.println(sensorValue, DEC);
}

/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor

This example code is in the public domain.
*/

void setup()
Serial.begin(9600);
}

void loop()
int sensorValue analogRead(A0);
Serial.println(sensorValue, DEC);
}

/*
Fade

This example shows how to Iade an LED on pin 9
using the analogWrite() Iunction.

This example code is in the public domain.

*/
int brightness 0; // how bright the LED is
int IadeAmount 5; // how many points to Iade the LED by

void setup()
// declare pin 9 to be an output:
pinMode(9, OUTPUT);
}

void loop()
// set the brightness oI pin 9:
analogWrite(9, brightness);

// change the brightness Ior next time through the loop:
brightness brightness IadeAmount;

// reverse the direction oI the Iading at the ends oI the Iade:
iI (brightness 0 ,, brightness 255)
IadeAmount -IadeAmount ;
}
// wait Ior 30 milliseconds to see the dimming eIIect
delay(30);
}
2. DIGITAL

/*
Button

Turns on and oII a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.


The circuit:
* LED attached Irom pin 13 to ground
* pushbutton attached to pin 2 Irom 5V
* 10K resistor attached to pin 2 Irom ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave http://www.0j0.org~
modiIied 28 Oct 2010
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin 2; // the number oI the pushbutton pin
const int ledPin 13; // the number oI the LED pin

// variables will change:
int buttonState 0; // variable Ior reading the pushbutton status

void setup()
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop()
// read the state oI the pushbutton value:
buttonState digitalRead(buttonPin);

// check iI the pushbutton is pressed.
// iI it is, the buttonState is HIGH:
iI (buttonState HIGH)
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else
// turn LED oII:
digitalWrite(ledPin, LOW);
}
}

/*
State change detection (edge detection)

OIten, you don't need to know the state oI a digital input all the time,
but you just need to know when the input changes Irom one state to another.
For example, you want to know when a button goes Irom OFF to ON. This is called
state change detection, or edge detection.

This example shows how to detect when a button or button changes Irom oII to on
and on to oII.

The circuit:
* pushbutton attached to pin 2 Irom 5V
* 10K resistor attached to pin 2 Irom ground
* LED attached Irom pin 13 to ground (or use the built-in LED on
most Arduino boards)

created 27 Sep 2005
modiIied 14 Oct 2010
by Tom Igoe

This example code is in the public domain.

http://arduino.cc/en/Tutorial/ButtonStateChange

*/

// this constant won't change:
const int buttonPin 2; // the pin that the pushbutton is attached to
const int ledPin 13; // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter 0; // counter Ior the number oI button presses
int buttonState 0; // current state oI the button
int lastButtonState 0; // previous state oI the button

void setup()
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}


void loop()
// read the pushbutton input pin:
buttonState digitalRead(buttonPin);

// compare the buttonState to its previous state
iI (buttonState ! lastButtonState)
// iI the state has changed, increment the counter
iI (buttonState HIGH)
// iI the current state is HIGH then the button
// wend Irom oII to on:
buttonPushCounter;
Serial.println("on");
Serial.print("number oI button pushes: ");
Serial.println(buttonPushCounter, DEC);
}
else
// iI the current state is LOW then the button
// wend Irom on to oII:
Serial.println("oII");
}
}
// save the current state as the last state,
//Ior next time through the loop
lastButtonState buttonState;


// turns on the LED every Iour button pushes by
// checking the modulo oI the button push counter.
// the modulo Iunction gives you the remainder oI
// the division oI two numbers:
iI (buttonPushCounter 4 0)
digitalWrite(ledPin, HIGH);
} else
digitalWrite(ledPin, LOW);
}

}

/*
Analog input, analog output, serial output

Reads an analog input pin, maps the result to a range Irom 0 to 255
and uses the result to set the pulsewidth modulation (PWM) oI an output pin.
Also prints the results to the serial monitor.

The circuit:
* potentiometer connected to analog pin 0.
Center pin oI the potentiometer goes to the analog pin.
side pins oI the potentiometer go to 5V and ground
* LED connected Irom digital pin 9 to ground

created 29 Dec. 2008
ModiIied 4 Sep 2010
by Tom Igoe

This example code is in the public domain.

*/

// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin 9; // Analog output pin that the LED is attached to

int sensorValue 0; // value read Irom the pot
int outputValue 0; // value output to the PWM (analog out)

void setup()
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}

void loop()
// read the analog in value:
sensorValue analogRead(analogInPin);
// map it to the range oI the analog out:
outputValue map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor:
Serial.print("sensor " );
Serial.print(sensorValue);
Serial.print("\t output ");
Serial.println(outputValue);

// wait 10 milliseconds beIore the next loop
// Ior the analog-to-digital converter to settle
// aIter the last reading:
delay(10);
}

/*
Analog Input
Demonstrates analog input by reading an analog sensor on analog pin 0 and
turning on and oII a light emitting diode(LED) connected to digital pin 13.
The amount oI time the LED will be on and oII depends on
the value obtained by analogRead().

The circuit:
* Potentiometer attached to analog input 0
* center pin oI the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to 5V
* LED anode (long leg) attached to digital output 13
* LED cathode (short leg) attached to ground

* Note: because most Arduinos have a built-in LED attached
to pin 13 on the board, the LED is optional.


Created by David Cuartielles
ModiIied 4 Sep 2010
By Tom Igoe

This example code is in the public domain.

http://arduino.cc/en/Tutorial/AnalogInput

*/

int sensorPin A0; // select the input pin Ior the potentiometer
int ledPin 13; // select the pin Ior the LED
int sensorValue 0; // variable to store the value coming Irom the sensor

void setup()
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}

void loop()
// read the value Irom the sensor:
sensorValue analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program Ior sensorValue~ milliseconds:
delay(sensorValue);
// turn the ledPin oII:
digitalWrite(ledPin, LOW);
// stop the program Ior Ior sensorValue~ milliseconds:
delay(sensorValue);
}

/*
Mega analogWrite() test
This sketch Iades LEDs up and down one at a time on digital pins 2 through 13.
This sketch was written Ior the Arduino Mega, and will not work on previous boards.

The circuit:
* LEDs attached Irom pins 2 through 13 to ground.
created 8 Feb 2009
by Tom Igoe
This example code is in the public domain.
*/

// These constants won't change. They're used to give names
// to the pins used:
const int lowestPin 2;
const int highestPin 13;


void setup()
// set pins 2 through 13 as outputs:
Ior (int thisPin lowestPin; thisPin highestPin; thisPin)
pinMode(thisPin, OUTPUT);
}
}

void loop()
// iterate over the pins:
Ior (int thisPin lowestPin; thisPin highestPin; thisPin)
// Iade the LED on thisPin Irom oII to brightest:
Ior (int brightness 0; brightness 255; brightness)
analogWrite(thisPin, brightness);
delay(2);
}
// Iade the LED on thisPin Irom brithstest to oII:
Ior (int brightness 255; brightness ~ 0; brightness--)
analogWrite(thisPin, brightness);
delay(2);
}
// pause between LEDs:
delay(100);
}
}

/*
Calibration

Demonstrates one technique Ior calibrating sensor input. The
sensor readings during the Iirst Iive seconds oI the sketch
execution deIine the minimum and maximum oI expected values
attached to the sensor pin.

The sensor minimum and maximum initial values may seem backwards.
Initially, you set the minimum high and listen Ior anything
lower, saving it as the new minimum. Likewise, you set the
maximum low and listen Ior anything higher as the new maximum.

The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0
* LED attached Irom digital pin 9 to ground

created 29 Oct 2008
By David A Mellis
ModiIied 4 Sep 2010
By Tom Igoe

http://arduino.cc/en/Tutorial/Calibration

This example code is in the public domain.

*/

// These constants won't change:
const int sensorPin A0; // pin that the sensor is attached to
const int ledPin 9; // pin that the LED is attached to

// variables:
int sensorValue 0; // the sensor value
int sensorMin 1023; // minimum sensor value
int sensorMax 0; // maximum sensor value


void setup()
// turn on LED to signal the start oI the calibration period:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);

// calibrate during the Iirst Iive seconds
while (millis() 5000)
sensorValue analogRead(sensorPin);

// record the maximum sensor value
iI (sensorValue ~ sensorMax)
sensorMax sensorValue;
}

// record the minimum sensor value
iI (sensorValue sensorMin)
sensorMin sensorValue;
}
}

// signal the end oI the calibration period
digitalWrite(13, LOW);
}

void loop()
// read the sensor:
sensorValue analogRead(sensorPin);

// apply the calibration to the sensor reading
sensorValue map(sensorValue, sensorMin, sensorMax, 0, 255);

// in case the sensor value is outside the range seen during calibration
sensorValue constrain(sensorValue, 0, 255);

// Iade the LED using the calibrated value:
analogWrite(ledPin, sensorValue);
}

/*
Fading

This example shows how to Iade an LED using the analogWrite() Iunction.

The circuit:
* LED attached Irom digital pin 9 to ground.

Created 1 Nov 2008
By David A. Mellis
ModiIied 17 June 2009
By Tom Igoe

http://arduino.cc/en/Tutorial/Fading

This example code is in the public domain.

*/


int ledPin 9; // LED connected to digital pin 9

void setup()
// nothing happens in setup
}

void loop()
// Iade in Irom min to max in increments oI 5 points:
Ior(int IadeValue 0 ; IadeValue 255; IadeValue 5)
// sets the value (range Irom 0 to 255):
analogWrite(ledPin, IadeValue);
// wait Ior 30 milliseconds to see the dimming eIIect
delay(30);
}

// Iade out Irom max to min in increments oI 5 points:
Ior(int IadeValue 255 ; IadeValue ~ 0; IadeValue -5)
// sets the value (range Irom 0 to 255):
analogWrite(ledPin, IadeValue);
// wait Ior 30 milliseconds to see the dimming eIIect
delay(30);
}
}

/*

Smoothing

Reads repeatedly Irom an analog input, calculating a running average
and printing it to the computer. Keeps ten readings in an array and
continually averages them.

The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0

Created 22 April 2007
By David A. Mellis dammellis.org~

http://www.arduino.cc/en/Tutorial/Smoothing

This example code is in the public domain.


*/


// DeIine the number oI samples to keep track oI. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size oI the readings array.
const int numReadings 10;

int readings|numReadings|; // the readings Irom the analog input
int index 0; // the index oI the current reading
int total 0; // the running total
int average 0; // the average

int inputPin A0;

void setup()

// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
Ior (int thisReading 0; thisReading numReadings; thisReading)
readings|thisReading| 0;
}

void loop()
// subtract the last reading:
total total - readings|index|;
// read Irom the sensor:
readings|index| analogRead(inputPin);
// add the reading to the total:
total total readings|index|;
// advance to the next position in the array:
index index 1;

// iI we're at the end oI the array...
iI (index ~ numReadings)
// ...wrap around to the beginning:
index 0;

// calculate the average:
average total / numReadings;
// send it to the computer (as ASCII digits)
Serial.println(average, DEC);
}

You might also like