Arduino Programming examples

First example of having Arduino store a number of button presses.


* Button
* by Greg Saul
* Incriments a counter by one each time a button on 7 is pressed.

int ledPin = 13;                // choose the pin for the LED
int inputPin = 7;               // choose the input pin (for a pushbutton)
int val = 0;                    // variable for reading the pin status
int counter = 0;                // store the number of button presses
boolean buttonDown = false;     // a flag that stores if the button is beeing pressed down or not
boolean isLightOn = false;      // a flag that stores if the led is on or off

void setup() {
pinMode(ledPin, OUTPUT);      // declare LED as output
pinMode(inputPin, INPUT);     // declare pushbutton as input
digitalWrite(ledPin, LOW);    // turn LED OFF
Serial.begin(9600);            // opens serial port, sets data rate to 9600 bps


void loop(){
val = digitalRead(inputPin);  // read input value

// check if we have just pressed the button down, and if we have incriment the counter
//  by one and set the button down flag to on
if (val == HIGH && buttonDown == false) {      // check if the input is HIGH and the button is not already been held down
buttonDown = true;           // set the button flag to down
counter = counter +1;        // add one to the counter
Serial.println(counter);     //send the current counter value to the computer

//if we are no longer pressing the button down set the buttonDown flag to up
if (val == LOW){
buttonDown = false;  // set the buttonDown value to up

// if we pressed the button 10 or more times toggle the led.
if(counter >= 10){
toggleLight(); // turn led off and on
counter = 0;


// Toggle the led on and off.
void toggleLight(){

// if the led is already on turn it off and rember that it's off.
isLightOn = !isLightOn;  // swap the isLightOn on if it's off and off it's on


Second example of having arduino read a analogue input and drive a analogue output


* AnalogInput AnalogOutput
* by Greg Saul
* Reads a brightness value from the photo resistor on pin 3
* and tranlates it to a brightness value of a led on pin 11.

int LDRPin = 3;    // select the input pin for the LDR (photoCell)
int ledPin = 11;   // select the pin for the LED, will need to be a PWM pin
int val = 0;       // variable to store the value coming from the sensor

void setup() {
pinMode(ledPin, OUTPUT);  // declare the ledPin as an OUTPUT
Serial.begin(9600);       // start comunication with the computer

void loop() {
val = analogRead(LDRPin);    // read the value from the sensor range 0 – 1024
//Serial.println(val);       // Send val to the pc, can be seen using Serial Monitor
analogWrite(ledPin, val/4);  // write val to the PWM output analogWrite values from 0 to 255


Sorry for the ugly post I will try and get the <code> tags working on wordpress or the ability to upload .pde’s

Let me know if anyone has any questions about the code.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s