sterretje:
How instantly? I'm quite sure that moving a hand over a LDR is in the order of a 0.5 to 1 second; that is ages for the microcontroller.
Interrupt routines should be short. Do something, read a few registers and set a flag and out. Next in loop you test the flag.
If you show your code, we will be able to advise better.
OK so "instantly" in this instance is subjective and perhaps "straight away" would have been better.
Oh - code - so no one can tell me how to get an analog pin to work as an interrupt through an ldr without seeing my code first? Bit weird, or perhaps you imagine, without even seeing my code, I have made errors and what on earth do I think I need an interrupt for?
Meh, ok, pick the bones out of this then...
#include <SPI.h>
#include <SD.h>
#include <TM1637Display.h> //4 digit display
#include "DHT.h"
#include <Wire.h> // Comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
//LedControl lc = LedControl(12, 11, 10, 1); //these are the pins on Arduino for the max7219 module
File myFile;
int tempC, rh;
#define DHTPIN 5 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x3F, 20, 4); // Set the LCD I2C address
//HC_SR04
#define trigPin 8
#define echoPin 7
#define MAX_DISTANCE 150 //cm
#define soundPin 6
// 4 digit display
#define CLK 2
#define DIO 3
TM1637Display display(CLK, DIO);
int ldr1 = 0;
int ldr2 = 1;
int covered = 50; //ldr values
int uncovered = 250; //ldr values
String msgln1;
String msgln2;
String NextMeet;
int TempOffset;
// setup ldr options system
boolean option1 = false;
boolean option2 = false;
boolean option3 = false;
boolean option4 = false;
String op1l1, op1l2, op1l3, op1l4;
String op2l1, op2l2, op2l3, op2l4;
String op3l1, op3l2, op3l3, op3l4;
String op4l1, op4l2, op4l3, op4l4;
void setup() { //run setup once
Serial.begin(9600);
// /////////////////SD Setup ////////////
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);
//SD.begin(4);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
readSDSettings();
// debugging
Serial.println("In RAM Memory");
Serial.print("msgln1=");
Serial.println(msgln1);
Serial.print("msgln2=");
Serial.println(msgln2);
Serial.print("NextMeet=");
Serial.println(NextMeet);
Serial.print("TempOffset=");
Serial.println(TempOffset);
// ///////////////SD Setup ///////////////
pinMode(ldr1, INPUT); // declare the LDR as an INPUT
pinMode(ldr2, INPUT);
pinMode(ldr3, INPUT);
pinMode(ldr4, INPUT);
pinMode(trigPin, OUTPUT); //Ping sensor setup
pinMode(echoPin, INPUT); //Pint sensor setup
dht.begin(); // start temperature sensor
lcd.begin(); // initialize the lcd for 16 chars 2 lines
lcd.backlight(); // finish with backlight on
lcd.clear();
}
void loop() { //run continuously
float h = dht.readHumidity();
float t = dht.readTemperature();
tempC = (int)t;
rh = (int)h;
// HC_SR04
int duration, distance;
digitalWrite(trigPin, LOW); // Added this line
//delayMicroseconds(2); // Allow to set
digitalWrite(trigPin, HIGH);
//delayMicroseconds(10); // Allow to set
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance<50){
noTone(soundPin);
// play on soundPin, note, duration ms:
tone(soundPin, 1023, 100);
delay(200);
noTone(soundPin);
tone(soundPin, 823, 100);
delay(300);
noTone(soundPin);
tone(soundPin, 1023, 100);
delay(200);
noTone(soundPin);
tone(soundPin, 823, 100);
delay(300);
noTone(soundPin);
}
// LCD
//readSDSettings;
lcd.setCursor(0, 0);
lcd.print(msgln1.substring(1, 20)); //22 character in the string inc " at each end
lcd.setCursor(0, 1);
lcd.print(msgln2.substring(1, 20)); // only the middle 20 chars needed
lcd.setCursor(0, 2);
lcd.print("Temperature: ");
lcd.setCursor(15, 2);
lcd.print("c");
lcd.setCursor(1, 3);
lcd.print("Next Meet ");
lcd.print(NextMeet);
lcd.setCursor(13, 2);
lcd.print(tempC-TempOffset); // calibrated with off-set to show correct temperature
// 7segment 4 digit led display
display.setBrightness(0x0f);
uint8_t segto;
display.showNumberDec(distance, true);
segto = 0x80 | display.encodeDigit((distance / 100) % 10);
}
void readSDSettings(){
char character;
String settingName;
String settingValue;
myFile = SD.open("poster.txt");
if (myFile) {
while (myFile.available()) {
character = myFile.read();
while((myFile.available()) && (character != '[')){
character = myFile.read();
}
character = myFile.read();
while((myFile.available()) && (character != '=')){
settingName = settingName + character;
character = myFile.read();
}
character = myFile.read();
while((myFile.available()) && (character != ']')){
settingValue = settingValue + character;
character = myFile.read();
}
if(character == ']'){
//Debuuging Printing
Serial.print("Name:");
Serial.println(settingName);
Serial.print("Value :");
Serial.println(settingValue);
// Apply the value to the parameter
applySetting(settingName,settingValue);
// Reset Strings
settingName = "";
settingValue = "";
}
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening poster.txt");
}
}
void applySetting(String settingName, String settingValue) {
if(settingName == "msgln1") {
msgln1 = settingValue;
}
if(settingName == "msgln2") {
msgln2 = settingValue;
}
if(settingName == "NextMeet") {
NextMeet = settingValue;
}
if(settingName == "TempOffset") {
TempOffset = settingValue.toInt();
}
if(settingName == "op1l1") {
op1l1 = settingValue;
}
if(settingName == "op1l2") {
op1l2 = settingValue;
}
if(settingName == "op1l3") {
op1l3 = settingValue;
}
if(settingName == "op1l4") {
op1l4 = settingValue;
}
if(settingName == "op121") {
op2l1 = settingValue;
}
if(settingName == "op2l2") {
op2l2 = settingValue;
}
if(settingName == "op2l3") {
op2l3 = settingValue;
}
if(settingName == "op2l4") {
op2l4 = settingValue;
}
if(settingName == "op3l1") {
op3l1 = settingValue;
}
if(settingName == "op3l2") {
op3l2 = settingValue;
}
if(settingName == "op3l3") {
op3l3 = settingValue;
}
if(settingName == "op3l4") {
op3l4 = settingValue;
}
if(settingName == "op4l1") {
op4l1 = settingValue;
}
if(settingName == "op4l2") {
op4l2 = settingValue;
}
if(settingName == "op4l3") {
op4l3 = settingValue;
}
if(settingName == "op4l4") {
op4l4 = settingValue;
}
}
void displayoptions() {
lcd.setCursor(0,0);
while(option1){
lcd.print(op1l1);
lcd.setCursor(1,0);
lcd.print(op1l2);
lcd.setCursor(2,0);
lcd.print(op1l3);
lcd.setCursor(3,0);
lcd.print(op1l4);
}
while(option2){
lcd.print(op2l1);
lcd.setCursor(1,0);
lcd.print(op2l2);
lcd.setCursor(2,0);
lcd.print(op2l3);
lcd.setCursor(3,0);
lcd.print(op2l4);
}
while(option3){
lcd.print(op3l1);
lcd.setCursor(1,0);
lcd.print(op3l2);
lcd.setCursor(2,0);
lcd.print(op3l3);
lcd.setCursor(3,0);
lcd.print(op3l4);
}
while(option4){
lcd.print(op4l1);
lcd.setCursor(1,0);
lcd.print(op4l2);
lcd.setCursor(2,0);
lcd.print(op4l3);
lcd.setCursor(3,0);
lcd.print(op4l4);
}
}