You are on page 1of 7

/*

Arduino Starter Kit example


Project 4 - Color Mixing Lamp

This sketch is written to accompany Project 3 in the


Arduino Starter Kit

Parts required:
1 RGB LED
three 10 kilohm resistors
3 220 ohm resistors
3 photoresistors
red green and blue colored gels

Created 13 September 2012


Modified 14 November 2012
by Scott Fitzgerald
Thanks to Federico Vanzati for improvements

http://www.arduino.cc/starterKit

This example code is part of the public domain


*/

const int greenLEDPin = 9; // LED connected to digital pin 9


const int redLEDPin = 10; // LED connected to digital pin 10
const int blueLEDPin = 11; // LED connected to digital pin 11

const int redSensorPin = A0; // pin with the photoresistor with the red gel
const int greenSensorPin = A1; // pin with the photoresistor with the green gel
const int blueSensorPin = A2; // pin with the photoresistor with the blue gel

int redValue = 0; // value to write to the red LED


int greenValue = 0; // value to write to the green LED
int blueValue = 0; // value to write to the blue LED

int redSensorValue = 0; // variable to hold the value from the red sensor
int greenSensorValue = 0; // variable to hold the value from the green sensor
int blueSensorValue = 0; // variable to hold the value from the blue sensor

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
// set the digital pins as outputs
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
}

void loop() {
// Read the sensors first:

// read the value from the red-filtered photoresistor:


redSensorValue = analogRead(redSensorPin);
// give the ADC a moment to settle
delay(5);
// read the value from the green-filtered photoresistor:
greenSensorValue = analogRead(greenSensorPin);
// give the ADC a moment to settle
delay(5);
// read the value from the blue-filtered photoresistor:
blueSensorValue = analogRead(blueSensorPin);

// print out the values to the serial monitor


Serial.print("raw sensor Values \t red: ");
Serial.print(redSensorValue);
Serial.print("\t green: ");
Serial.print(greenSensorValue);
Serial.print("\t Blue: ");
Serial.println(blueSensorValue);

/*
In order to use the values from the sensor for the LED,
you need to do some math. The ADC provides a 10-bit number,
but analogWrite() uses 8 bits. You'll want to divide your
sensor readings by 4 to keep them in range of the output.
*/
redValue = redSensorValue / 4;
greenValue = greenSensorValue / 4;
blueValue = blueSensorValue / 4;

// print out the mapped values


Serial.print("Mapped sensor Values \t red: ");
Serial.print(redValue);
Serial.print("\t green: ");
Serial.print(greenValue);
Serial.print("\t Blue: ");
Serial.println(blueValue);

/*
Now that you have a usable value, it's time to PWM the LED.
*/
analogWrite(redLEDPin, redValue);
analogWrite(greenLEDPin, greenValue);
analogWrite(blueLEDPin, blueValue);
}
/*
Arduino Starter Kit example
Project 7 - Keyboard

This sketch is written to accompany Project 7 in the


Arduino Starter Kit

Parts required:
two 10 kilohm resistors
1 Megohm resistor
220 ohm resistor
4 pushbuttons
piezo

Created 13 September 2012


by Scott Fitzgerald

http://www.arduino.cc/starterKit

This example code is part of the public domain


*/

// create an array of notes


// the numbers below correspond to
// the frequencies of middle C, D, E, and F
int notes[] = {262, 294, 330, 349};

void setup() {
//start serial communication
Serial.begin(9600);
}

void loop() {
// create a local variable to hold the input on pin A0
int keyVal = analogRead(A0);
// send the value from A0 to the Serial Monitor
Serial.println(keyVal);

// play the note corresponding to each value on A0


if (keyVal == 1023) {
// play the first frequency in the array on pin 8
tone(8, notes[0]);
} else if (keyVal >= 990 && keyVal <= 1010) {
// play the second frequency in the array on pin 8
tone(8, notes[1]);
} else if (keyVal >= 505 && keyVal <= 515) {
// play the third frequency in the array on pin 8
tone(8, notes[2]);
} else if (keyVal >= 5 && keyVal <= 10) {
// play the fourth frequency in the array on pin 8
tone(8, notes[3]);
} else {
// if the value is out of range, play no tone
noTone(8);
}
}
/*
Arduino Starter Kit example
Project 8 - Digital Hourglass

This sketch is written to accompany Project 8 in the


Arduino Starter Kit

Parts required:
10 kilohm resistor
six 220 ohm resistors
six LEDs
tilt switch

Created 13 September 2012


by Scott Fitzgerald

http://www.arduino.cc/starterKit

This example code is part of the public domain


*/

// named constant for the switch pin


const int switchPin = 8;

unsigned long previousTime = 0; // store the last time an LED was updated
int switchState = 0; // the current switch state
int prevSwitchState = 0; // the previous switch state
int led = 2; // a variable to refer to the LEDs

// 600000 = 10 minutes in milliseconds


long interval = 600000; // interval at which to light the next LED

void setup() {
// set the LED pins as outputs
for (int x = 2; x < 8; x++) {
pinMode(x, OUTPUT);
}
// set the tilt switch pin as input
pinMode(switchPin, INPUT);
}

void loop() {
// store the time since the Arduino started running in a variable
unsigned long currentTime = millis();
// compare the current time to the previous time an LED turned on
// if it is greater than your interval, run the if statement
if (currentTime - previousTime > interval) {
// save the current time as the last time you changed an LED
previousTime = currentTime;
// Turn the LED on
digitalWrite(led, HIGH);
// increment the led variable
// in 10 minutes the next LED will light up
led++;

if (led == 7) {
// the hour is up
}
}

// read the switch value


switchState = digitalRead(switchPin);

// if the switch has changed


if (switchState != prevSwitchState) {
// turn all the LEDs low
for (int x = 2; x < 8; x++) {
digitalWrite(x, LOW);
}

// reset the LED variable to the first one


led = 2;

//reset the timer


previousTime = currentTime;
}
// set the previous switch state to the current state
prevSwitchState = switchState;
}

You might also like