skip to main |
skip to sidebar
After a few false starts, i finally got this thing working.
It needs calibrating more accurately, but i haven't had the time to do that properly..
There's the usual warning about Blogger, as follows:
NOTE: this blogger doesn't like 'less-than' signs - it thinks they're HTML thingies..
so where you see n"10 put n 'less-than' 10,
& where you see n""10 put n 'greater-than' 10.VERY IMPORTANT!!In the coding below there is a#include EEPROM.hstatement.Around the EEPROM.h there should be 'less-than' &'greater-than' symbols.these don't show up in these blog postings....AND - they disappear anything inside them as well, so....don't forget them!
/* * Project: Major_Project_DualSensors_multiWarning * Author: Jane-Maree Howard
* Date: Sunday 20/06/2010
* Platform: Arduino 18
* Purpose: To operate two different sensors - temperature & light - & record their response at intervals, giving different warnings if either response moves outside pre-set limits. Results are sent to the Serial Monitor(SM). * Operation:
Description: Connect one end of a CdS Photocell to 5V, the other end to Analog 0. Then connect a 10K resistor from Analog 0 to ground.
This forms the light-recording voltage-divider circuit.Connect one end of a Thermistor to 5V, the other end to Analog 1.
Then connect a 10K resistor from Analog 1 to ground.
This forms the temperature-recording voltage-divider circuit.
Connect the common cathode of a 3-colour LED to Ground,
& the Red, Green & Blue anodes through 150-Ohm current-limiting resistors
to digital-pins 12, 11, & 10 respectively.
Also connect one end of a buzzer to Ground,
& the other end through a 100-Ohm resistor to digital-pin 9.
These form the light-&-sound warning circuit.
Connect one end of a push-button switch to Ground,
& the other end to digital-pin 8.
Readings are all sent to EEPROM - the push-button switch controls the EEPROM-reading process.
Include: EEPROM.h library
Declare: 2 Analog pins & their respective reading variables; 5 Digital pins for monitoring & warning circuitry.
Setup(): Connect to SM; initialise Analog & Digital pins;
Loop(): the program loops through its sensor data-logging,
until the push-button switch is pressed.
The program then reads all recorded data from EEPROM
& send it to the SM
* Comments: I don't know what the thermistor & photocell ranges are, but on past results I'd say more than 0-255. Rather than mess about with Hi-Lo bytes, better put in the Mapping Function*/
#include EEPROM.h // needed for using EEPROM
byte photocellPin = 0; // the photocell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from photocell voltage-divider
byte thermistorPin = 1; // the thermistor and 10K pulldown are connected to a1 int thermistorReading; // the analog reading from thermistor voltage-divider
int eepromAddress = 0; // start at EEPROM address zero
byte boardLedPin = 13; // on-board LED blinks when any reading is takenbyte redLedPin = 12; // RED LED connected to digital pin 12byte grnLedPin = 11; // GREEN LED connected to digital pin 11byte bluLedPin = 10; // BLUE LED connected to digital pin 10
byte buzzPin = 9; // buzzer pin
byte buttonPin = 8; // digital pin for switch connected to ground.void setup(){ Serial.begin(9600); // connect to SM
/* initialise the Analog recording pins as inputs */
pinMode(photocellPin,INPUT); // photocell pin
pinMode(thermistorPin,INPUT); // thermistor pin
/* initialise the Digital warning pins as outputs */ pinMode(boardLedPin, OUTPUT); // on-board LED monitors readings pinMode(redLedPin, OUTPUT); // Red LED
pinMode(grnLedPin, OUTPUT); // Green LED
pinMode(bluLedPin, OUTPUT); // Blue LED
pinMode(buzzPin, OUTPUT); // buzzer
/* initialise the push-button pin as an input & set HIGH */ pinMode(buttonPin, INPUT); // push-button pin
digitalWrite(buttonPin, HIGH); // turn on internal pull up
/* turn off all LEDs & buzzer */
digitalWrite(boardLedPin, LOW); // turn off on-board LED digitalWrite(redLedPin, LOW); // turn off Red LED digitalWrite(grnLedPin, LOW); // turn off Green LED
digitalWrite(bluLedPin, LOW); // turn off bluLedPin
digitalWrite(buzzPin, LOW); // turn off buzzer
}//end setup()
void loop()
{// take readings until button pushed
while (digitalRead(buttonPin) == 1)
{
// read value on thermistor pin i.e. analog 1thermistorReading = analogRead(thermistorPin); EEPROM.write(eepromAddress,map(thermistorReading, 0, 1023, 0, 255));
eepromAddress++;
digitalWrite(boardLedPin, HIGH);
delay(150);
readingWarning(map(thermistorReading, 0, 1023, 0, 255)); digitalWrite(boardLedPin, LOW); delay(350); // about half-a-second between readings
// read value on photocell pin i.e. analog 0
photocellReading = analogRead(photocellPin); EEPROM.write(eepromAddress,map(photocellReading, 0, 1023, 0, 255));
eepromAddress++;
digitalWrite(boardLedPin, HIGH);
readingWarning(map(photocellReading, 0, 1023, 0, 255));
delay(150);
digitalWrite(boardLedPin, LOW);
delay(2500); // about 3 seconds before next readings
if (eepromAddress == 100)
eepromAddress = 0;
}//while()
Serial.println();
Serial.println("\tTemperature\tLight Level");
eepromAddress = 0;
while ( eepromAddress " 100)
{
Serial.print("\t\t");
Serial.print(int(EEPROM.read(eepromAddress))); //print temperature
eepromAddress++ ;
Serial.print("\t\t");Serial.println(int(EEPROM.read(eepromAddress))); //print light level
eepromAddress++ ;
}//while()
}//end loop()
/* function copes with readings outside pre-set limits */void readingWarning(int aReading)
{
if (aReading ""= 190)
{
blinkLEDnoise(grnLedPin,1800);
}//if()
if (aReading ""= 200)
{
blinkLEDnoise(redLedPin,2400);
}//if()if (aReading "= 180)
{
blinkLEDnoise(grnLedPin,1200);
}//if()
if (aReading "= 170){ blinkLEDnoise(bluLedPin,600);
}//if()
}//end readingWarning()/* function blinks LED & sounds buzzer */
void blinkLEDnoise(byte ledPin, int bTone){
tone(buzzPin,bTone,250);
for (byte n=0; n"10; n++){
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50); }//for(n++)
}//end blinkLEDnoise()
The breadboard's a little more complicated than previous stuff..
There's only a slight alteration to the software in Task 38.
Just means declaring a counter variable & incrementing it whenever the button is pushed & a reading is to be outputted.
You can see (readings 9-18) where i put my finger over the photocell, & later (27-31) where i just cupped the photocell in my hand..
/* check if the pushbutton is pressed.
if it is, the buttonState is HIGH, so..*/
if (buttonState == HIGH)
{
//..increment the reading counter &..
readNum++;
//..output the LDR reading to the SM
Serial.print("Reading number ");
Serial.print(readNum); // reading count
Serial.print(" is ");
Serial.println(photocellReading); // the raw analog reading
delay(100); // debounce delay
}// if..
This is the
Fritzing breadboard circuit.
Nothing complicated about it.
I do wish i could get the schematic to match the breadboard, but i still get a stupid tangle of unconnected components - very annoying!
The output "No output" shows the program is running. I won't bother next time; it's just for demo purposes..
When the button is pushed, the analog reading is sent to the Serial Monitor.
My reaction times aren't lightning fast, but even with a 'debounce' delay, it still gets a couple of readings out before my finger comes off the button.
/*
* Project: button_&_LDR_task_38
* Author: Jane-Maree Howard
* Date: Monday 29/03/2010
* Platform: Arduino 18
* Purpose: To send the reading from the LDR to the Serial Monitor
* whenever the button is pressed.
* The circuit:
* 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 the pushbutton to pin 2 from +5V, &
* a 10K resistor attached from pin 2 to ground
*/
int buttonPin = 2; // the number of the pushbutton pin
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
/* We'll send the LDR state to the Serial monitor */
Serial.begin(9600); // baud rate 9600 should be set on the SM
// initialize the pushbutton pin 2 as an input:
pinMode(buttonPin, INPUT);
}//end setup()
void loop()
{
// read the state of the pushbutton value..
buttonState = digitalRead(buttonPin);
// read the value of the photocell
photocellReading = analogRead(photocellPin); //i.e. analog 0
/* check if the pushbutton is pressed.
if it is, the buttonState is HIGH, so..*/
if (buttonState == HIGH)
{
//..output the LDR reading to the SM
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
delay(100); // debounce delay
}// if..
else
{
Serial.println("No output");
}//..else
}//end loop()
/*
* Project: Photocell_switch_task_28
* Author: Jane-Maree Howard
* Date: Friday 26/03/2010
* Platform: Arduino 18
* Purpose: To make a Photocell Switch for a LED.
* Operation: The circuit arrangement is the same as the ladyada
* LED dimmer, but this time the software has a trigger threshold
* for turning the LED on.
* The output of Analog 0 is measured as before, but instead
* of analogWrite() to the LED pin, digitalWrite is used to
* switch the LED on or off.
* We don't need to map the a0 output, since it's merely a trigger.
* Original description:
* 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 9 through a resistor to ground
*/
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
int LEDpin = 9; // connect Red LED to pin 09(PWM pin)
int darkLevel = 100; // threshold for LED trigger action
void setup(void)
{
// We'll send debugging information via the Serial monitor
Serial.begin(9600); // baud rate 9600 should be set on the SM
digitalWrite(LEDpin, LOW);// turn LED off
}//end setup()
void loop(void)
{
digitalWrite(LEDpin, LOW);// turn LED off
photocellReading = analogRead(photocellPin); //i.e. analog 0
Serial.print("Analog reading = ");
Serial.print(photocellReading); // the raw analog reading
//we now use our 'darkLevel' variable to trigger the LED ON.
if (photocellReading <>
As can be seen, it is fairly obvious from
the SM output when the trigger threshold is reached - i have
made the ON-message much more noticeable.
The actual threshold was set at 100, &
there's a reading of 105 just before the
reading of 80 that results in the LED turning on.
The only problem i see is my buffer resistor for the LED - it's too high, since the LED isn't very bright. That's probably due to the LED's duty cycle; it's mostly off, & stays on for only 0.5 sec, so it is rather dim..
..but it DOES work!