Assignment 4: Practicing Programming


Assignment 4: Practicing Programming

Goal of this Assignment: Practice the basics of programming
Assignment 4: Practicing Programming

Module: Basics in Programming Lesson
Goals
  
 
DIYDoll1

Photocell:
/* 
Adapted from
http://playground.arduino.cc/Learning/PhotoResistor
Simple test of the functionality of the photo resistor

Connect the photoresistor one leg to pin 0, and pin to +5V
Connect a resistor (around 10k is a good value, higher
values gives higher readings) from pin 0 to GND. 

----------------------------------------------------

           PhotoR     10K
 +5     o---/\/\/--.--/\/\/---o GND
                   |
 Pin A0 o-----------

----------------------------------------------------
*/

int lightPin = A0;  //define a pin for Photo resistor (input)
int ledPin = 13; // the pin for the led (output)

void setup()
{
    Serial.begin(9600);  //Begin serial communication
    pinMode( lightPin, INPUT );
    pinMode( ledPin, OUTPUT );
}

void loop()
{
    int lightValue = analogRead( lightPin );
    Serial.println( lightValue ); //Write the value of the photoresistor to the serial monitor.

    // if there is not enough light
    if ( lightValue < 400 )
       // turn on the LED
       digitalWrite( ledPin, HIGH );
    else
       // otherwise, turn it off
       digitalWrite( ledPin, LOW );

    delay(10); //short delay for faster response to light
}
Button:
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for 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 of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
  
DIYDoll2

Practice
 
DIYDoll3