Go Down

Topic: Controlling multiple LEDs using LDRs to turn them on and off (Read 716 times) previous topic - next topic

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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

D6equj5

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...
Code: [Select]
#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);
}
}


Thank you
D6

sterretje

Oh, we can tell or point you in the right  direction. There are two (or more) articles on the web about interrupt driven analogReads for arduino; one by Nick Gammon if I'm not mistaken. You can do a search (not posting from a pc at the moment so I  don't have my references at  hand).

Question is if it's the right approach. And seeing delays in your code, there are more than likely far better approaches.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

D6equj5

Fantastic, it's a shame you couldn't help although thank you for the link suggestion, it'll be very helpful sterretje,

I shall search for Nicks article.
As an aside, I found a couple of ldr modules in the workshop (probably from eBay and/or China, which output digital (onboard Schottky and pot for adjusting the trigger level) and so I'll use those in the meantime. The code is for an interactive poster and so most of it is display and formatting with delays to make it more child-friendly. After a month or so, the component parts will be broken down for use in other projects.
Thanks again
D6
Thank you
D6

sterretje

Just in case you can't find them

http://www.glennsweeney.com/tutorials/interrupt-driven-analog-conversion-with-an-atmega328p

https://www.gammon.com.au/adc; search the page for the keyword 'interrupt'; it's at about a 1/3 of the page


And as I mentioned, if you get rid of the delay around your tone functions, you probably don't need interrupts. You can use a small statemachine and a millis() based timing (see e.g. Using millis() for timing.  A beginners guide)

Good luck.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

D6equj5

Thanks Sterretje,
The tone function is a throw-away to make the thing beep at passers-by and probably won't be used in that format so that can be discounted. All other "delays" are required by hardware.

Anyway, the ldr/digital modules are glued into position now so I'll code them this morning.
D6

Thank you
D6

sterretje

No, delay() is not required by hardware. There is a NewPing library that allows you to use the HC_SR04 without having to wait for its reply.

For the tone, you can use a statemachine in combination with a millis() based timing; no delays required.

But your major problem will happen when you call displayoptions(). Indeed, with the approach of using while loops, you will need interrupts. Get rid of the while loops in there.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

D6equj5

No you are indeed wrong - Newping does not quite work correctly and it DOES (if you read newping.h etc have delay() built-in.

Other hardware used in this project also requires delays.

I tried to figure how some members on here get posting levels so very hight - I figured it - you just have to have the last word on every posting. Go on, post the last one ........................... then I'll refute it and you can justify then I'll refute again.

Drop it, I DO NEED TO USE DELAY for various parts of my project.
I have found a way to get my analogue ldr to produce a digital output
Thank you
thank you
thank you.
Have I thanked you enough for your help?
end.
pip pip
Thank you
D6

Go Up