/* Photocell simple testing sketch.
Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 11 through a resistor to ground
For more information see www.ladyada.net/learn/sensors/cds.html */
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int LEDbrightness; //
void setup(void) {
// We'll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
// LED gets brighter the darker it is at the sensor
// that means we have to -invert- the reading from 0-1023 back to 1023-0
photocellReading = 1023 - photocellReading;
//now we have to map 0-1023 to 0-255 since thats the range analogWrite uses
LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
analogWrite(LEDpin, LEDbrightness);
delay(100);
}
This program reads the amount of light shining on the LDR/Photocell and sends it to the serial port so the human can read it
This blog follows my attempts to create things using the Arduino Duemilanove board and components
Monday, May 3, 2010
Task 23: How many Arduino's are there?
check out wikieducator.org page with just some of the few arduino types listed
Task 21: Blink with Serial output
int ledPin = 13; // LED pin
int del = 200; // Starting Delay
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Start serial connection
Serial.println('Ready'); // let the human know the machine is ready
}
void loop() {
Serial.println('Blink'); write to the serial port
digitalWrite(ledPin, HIGH); // set the LED on
delay(del);
digitalWrite(ledPin, LOW); // set the LED off
delay(del);
}
int del = 200; // Starting Delay
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Start serial connection
Serial.println('Ready'); // let the human know the machine is ready
}
void loop() {
Serial.println('Blink'); write to the serial port
digitalWrite(ledPin, HIGH); // set the LED on
delay(del);
digitalWrite(ledPin, LOW); // set the LED off
delay(del);
}
Task 20: from 10ms to 1 sec then back down again
int ledPin = 13; // LED pin
int del = 10; // Starting Delay
bool dirUp = false; // are we increasing or decreasing
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); // set the LED on
delay(del); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(del); // wait for a second
if( dirUp ) {
if((del++) == 1001) {
del = 1000;
dirUp = false;
}
} else {
if((del--) == 9) {
del = 10;
dirUp = true;
}
}
}
int del = 10; // Starting Delay
bool dirUp = false; // are we increasing or decreasing
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); // set the LED on
delay(del); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(del); // wait for a second
if( dirUp ) {
if((del++) == 1001) {
del = 1000;
dirUp = false;
}
} else {
if((del--) == 9) {
del = 10;
dirUp = true;
}
}
}
Task 19: blinking from 100ms to 1sec.
int ledPin = 13; // LED pin
int del = 100; // Starting Delay
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); // set the LED on
delay(del); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(del); // wait for a second
if((del++) == 1001) { del = 100; }
}
int del = 100; // Starting Delay
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, HIGH); // set the LED on
delay(del); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(del); // wait for a second
if((del++) == 1001) { del = 100; }
}
Task 17: for-loop and 5 blinks then 1
int ledPin[2] = {13, 12}; // array of LEDs
int del = 500; // Delay
int led = 0; // which LED is to be turned on
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin[0], OUTPUT);
pinMode(ledPin[1], OUTPUT);
}
void loop()
{
for(int i = 0; i >= 5; i++) {
digitalWrite(ledPin[1], HIGH); // set the LED on
delay(del); // wait for a second
digitalWrite(ledPin[1], LOW); // set the LED off
delay(del); // wait for a second
}
digitalWrite(ledPin[0], HIGH); // set the LED on
delay(del); // wait for a second
digitalWrite(ledPin[0], LOW); // set the LED off
delay(del); // wait for a second
}
int del = 500; // Delay
int led = 0; // which LED is to be turned on
void setup() {
// initialize the digital pin as an output:
pinMode(ledPin[0], OUTPUT);
pinMode(ledPin[1], OUTPUT);
}
void loop()
{
for(int i = 0; i >= 5; i++) {
digitalWrite(ledPin[1], HIGH); // set the LED on
delay(del); // wait for a second
digitalWrite(ledPin[1], LOW); // set the LED off
delay(del); // wait for a second
}
digitalWrite(ledPin[0], HIGH); // set the LED on
delay(del); // wait for a second
digitalWrite(ledPin[0], LOW); // set the LED off
delay(del); // wait for a second
}
Tuesday, March 16, 2010
Arduino Keywords
setup()
This is the function that tells the arduino how to set its self up.
loop()
This is what the arduino is to do while it is running.
analogRead(pinnumber)
read an analog value (0 -> 1023) from the pinnumber given
Serial.print()
print ASCII to the serial
Serial.println()
Print ASCII to the serial port but also add a line break after
pinMode(pinnumber, mode)
pinnumber = the number of the digital pin on the arduino board.
mode = Can be ether 'INPUT' for receving data or 'OUTPUT' for sending data.
digitalRead(pinnumber)
pinnumber = the number of the digital pin on the arduino board.
This is the function that tells the arduino how to set its self up.
loop()
This is what the arduino is to do while it is running.
analogRead(pinnumber)
read an analog value (0 -> 1023) from the pinnumber given
Serial.print()
print ASCII to the serial
Serial.println()
Print ASCII to the serial port but also add a line break after
pinMode(pinnumber, mode)
pinnumber = the number of the digital pin on the arduino board.
mode = Can be ether 'INPUT' for receving data or 'OUTPUT' for sending data.
digitalRead(pinnumber)
pinnumber = the number of the digital pin on the arduino board.
Subscribe to:
Posts (Atom)